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
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
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"
}
]
}
8 months ago
is this next?
i realized, i had the mutation name as serviceCreate instead of createService 💀
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
}
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?
8 months ago
yes
from token generated through https://railway.app/account/tokens
something is cached somewhere causing the error on one client but not the other
8 months ago
are you deploying a service client side?
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
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"
}
mutation
mutation ServiceDelete {
serviceCreate(
input: {
name: "Crspy EST Bot"
projectId: ${projectId}
variables: [
{
"Sellix": "Item"
}
]
}
) {
id
}
}
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
8 months ago
can't really help you there, if it works in postman that means it's a code issue
on the graphql playground, it shows type as
type EnvironmentVariable {
key: String!
value: String!
}
8 months ago
I'm not sure
8 months ago
you said it worked in postman, so what format did you use?
8 months ago
ah gotcha
input CreateServiceInput {
name: String
projectId: String!
variables: [EnvironmentVariableInput!]
}
input EnvironmentVariableInput {
key: String!
value: String!
}
my graphql schema for the input
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
8 months ago
please look at the message i sent
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?
8 months ago
I really hope not
8 months ago
oof