400 http status code on api request

crspy
HOBBY

8 months ago

Whenever I make a request to railway api from localhost, I get cors error:

Access to fetch at 'https://backboard.railway.com/graphql/v2' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'https://railway.app' that is not equal to the supplied origin. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

does anyone no how to fix it? or do i just need to run it from https

0 Replies

crspy
HOBBY

8 months ago

Figured out the cors issue, but im getting 400 http codes when trying to create a service. Can I get any help? I'm using urql to make graphql requests from the client

URQL provider:

"use client"

import { PropsWithChildren } from "react"
import { toast } from "react-toastify"
import { Client, Provider, cacheExchange, fetchExchange } from 'urql'


export const ResellerProvider = ({ children }: PropsWithChildren) => {
    const client = new Client({
        url: 'https://backboard.railway.com/graphql/v2',
        exchanges: [cacheExchange, fetchExchange],
        fetchOptions: () => {
            return {
                mode: "no-cors",
                headers: { authorization: `Bearer ${process.env.RAILWAY_TOKEN}` },
            };
        },
    })

    return (

            {children}

    )
}

component:

import { graphql } from 'gql.tada'
import { useMutation } from "urql"

const serviceCreate = graphql(`
    mutation CreateService($service: CreateServiceInput!) {
        createService(input: $service) {
            id,
        }
    }
`)

export const DeploymentCard = () => {
    const [createResult, createService] = useMutation(serviceCreate)

    // form logic

    const onCreateSubmit = (values: z.infer) => {
        startTransition(async () => {
            await createService({
                service: {
                    name: `${reseller?.owner?.name}'s Bot`,
                    projectId: process.env.RAILWAY_PROJECTID,
                    variables: [
                        {
                            key: "DISCORD_TOKEN",
                            value: botToken!,
                        },
                        {
                            key: "SELLIX_API_KEY",
                            value: "${{SELLIX_API_KEY}}" // attempt to use shared env variable in project
                        }
                    ]
                }
            })
            if (!!createResult.error || !createResult.data?.createService.id) {
                console.log("err: ", createResult.error?.message, "\n", createResult.error?.response) // undefined
                console.log("data: ", createResult.data) // undefined
                toast.error("Unable to create deployment environment")
                return
            }
            // Other railway graphql requests that never run because return happens above
        })
    }
    return (
        <div>
          {/* Render form */}
        </div>
    )
}

I tried providing minimal code so its easier to see the graphql request code. i hope i didnt leave anything out


crspy
HOBBY

8 months ago

when i make the request from postman, I get the following response with a status 400 Bad Request

{
    "errors": [
        {
            "message": "Problem processing request",
            "traceId": "2224571512345608770"
        }
    ]
}

crspy
HOBBY

8 months ago

400 http status code on api request


8 months ago

is this next?


crspy
HOBBY

8 months ago

yes


crspy
HOBBY

8 months ago

i realized, i had the mutation name as serviceCreate instead of createService 💀


crspy
HOBBY

8 months ago

changed it and now i dont get 400 errors


crspy
HOBBY

8 months ago

I get this now tho with status 200:

{
    "errors": [
        {
            "message": "Not Authorized",
            "locations": [
                {
                    "line": 2,
                    "column": 5
                }
            ],
            "path": [
                "serviceCreate"
            ],
            "extensions": {
                "code": "INTERNAL_SERVER_ERROR"
            },
            "traceId": "1684849550405239909"
        }
    ],
    "data": null
}

crspy
HOBBY

8 months ago

just generated a new api key, and still get error. The project ID is the one from the url parameter when i go to dashboard in the request


8 months ago

where have gone in railway to get an api key?


crspy
HOBBY

8 months ago

Project settings > tokens > create


crspy
HOBBY

8 months ago

Should I go to account settings > tokens instead?


8 months ago

yes


crspy
HOBBY

8 months ago

I still get the same error


crspy
HOBBY

8 months ago

from token generated through https://railway.app/account/tokens


crspy
HOBBY

8 months ago

nvm


crspy
HOBBY

8 months ago

something is cached somewhere causing the error on one client but not the other


8 months ago

are you deploying a service client side?


crspy
HOBBY

8 months ago

on postman its working. nextjs cached the request data from my previous request via urql (with the wrong token), which caused it to not work


crspy
HOBBY

8 months ago

so im getting 400 codes with the serviceCreate mutation whenever i specify environment variables. How should I add environment variables to a service after I make it?


8 months ago

variables should be -

{
    "key": "value" 
}

crspy
HOBBY

8 months ago

Ahh ok


crspy
HOBBY

8 months ago

mutation

mutation ServiceDelete {
    serviceCreate(
        input: {
            name: "Crspy EST Bot"
            projectId: ${projectId}
            variables: [
                {
                "Sellix": "Item"
                }
            ]
        }
    ) {
        id
    }
}

crspy
HOBBY

8 months ago

response:

{
    "errors": [
        {
            "message": "Syntax Error: Expected Name, found String \"Sellix\".",
            "locations": [
                {
                    "line": 8,
                    "column": 17
                }
            ],
            "extensions": {
                "code": "GRAPHQL_PARSE_FAILED"
            },
            "traceId": "7459155247986771905"
        }
    ]
}

8 months ago

it's not an object inside an array, it's just an object, also why service delete lol


crspy
HOBBY

8 months ago

currently trying to get it to work in nextjs without env variables


crspy
HOBBY

8 months ago

i get 400 codes on every request through nextjs (with gql.tada and urql)


crspy
HOBBY

8 months ago

but the same requests work in postman


crspy
HOBBY

8 months ago

1309426171015991300
1309426171380760600


crspy
HOBBY

8 months ago

idk, copied that from postman. servicedelete was just there


8 months ago

can't really help you there, if it works in postman that means it's a code issue


crspy
HOBBY

8 months ago

on the graphql playground, it shows type as

type EnvironmentVariable {
    key: String!
    value: String!
}

crspy
HOBBY

8 months ago

is that just wrong?


8 months ago

I'm not sure


8 months ago

you said it worked in postman, so what format did you use?


crspy
HOBBY

8 months ago

worked in postman without the variables


crspy
HOBBY

8 months ago

havent gotten it to work anywhere with variables


8 months ago

ah gotcha


crspy
HOBBY

8 months ago

input CreateServiceInput {
    name: String
    projectId: String!
    variables: [EnvironmentVariableInput!]
}

input EnvironmentVariableInput {
    key: String!
    value: String!
}

my graphql schema for the input


crspy
HOBBY

8 months ago

kinda new to graphql, how could I model an object for the env variables if its supposed to be like:

{ "key": "value" }

8 months ago

mutation ServiceCreate($name: String, $projectId: String!, $environmentId: String!, $source: ServiceSourceInput, $branch: String, $variables: EnvironmentVariables) {
  serviceCreate(
    input: {name: $name, projectId: $projectId, environmentId: $environmentId, source: $source, variables: $variables, branch: $branch}
  ) {
    id
    name
  }
}
{
  "name": "Service Name",
  "projectId": "f8ec0264-db85-444b-b034-cb7a8f6f8ee4",
  "environmentId": "ebcff3a2-92f4-4033-930a-184f4354b033",
  "source": {
    "repo": "brody192/hello-world"
  },
  "branch": "main",
  "variables": {
    "TEST": "HELLO"
  }
}
type ServiceConfig = {
  name: string;
  projectId: string;
  environmentId: string;
  source: {
    repo: string;
  };
  branch: string;
  variables: Record;
}

8 months ago

this works, obviously replace with your ids and what not


crspy
HOBBY

8 months ago

what is the EnvironmentVariables input type?


8 months ago

please look at the message i sent


crspy
HOBBY

8 months ago

💀


crspy
HOBBY

8 months ago

i swear that bottom part wasnt there


crspy
HOBBY

8 months ago

do you have any ideas how cors is configured on railway's api? I believe my 400 error stems from something cors related


8 months ago

hopefully you are not trying to call the railway API from the client side right?


crspy
HOBBY

8 months ago

i dont think so…


crspy
HOBBY

8 months ago

hold up…


8 months ago

I really hope not


crspy
HOBBY

8 months ago

💀


crspy
HOBBY

8 months ago

I forgot to add a "use server"


crspy
HOBBY

8 months ago

to the top of the file


crspy
HOBBY

8 months ago

so it was running on client


8 months ago

oof


400 http status code on api request - Railway Help Station