Hello all. I have a frustrating problem. I'm just ...
# prisma-whats-new
j
Hello all. I have a frustrating problem. I'm just getting started with graphcool, but am struggling to even request from the API. With the following request, I get
400 (Bad Request)
with a
CORS
error.
Copy code
return fetch(API_ENDPOINT_URL, {
	method: 'post',
	headers: {
		Accept: 'application/json',
		'Content-Type': 'application/json',
	},
	body: "{query: 'query { allUsers { id name } }'}",
})
When I add
mode: 'no-cors'
, the request headers are immutable (see https://stackoverflow.com/questions/38156239/how-to-set-the-content-type-of-request-header-when-using-fetch-api) so I get a
415 (Unsupported Media Type)
error because the
Content-Type
is
text/plain
.
Copy code
return fetch(API_ENDPOINT_URL, {
	method: 'post',
	mode: 'no-cors',
	headers: { // This does nothing
		Accept: 'application/json',
		'Content-Type': 'application/json',
	},
	body: "{query: 'query { allUsers { id name } }'}",
})
How can I get up and running? Can't seem to find these issues documented anywhere.
n
@jpfm hey there 👋
j
hi 🙂
n
what happens when you leave out
Accept
all together?
j
if i have`mode: 'no-cors'` the whole
headers
object gets ignored
j
Copy code
return fetch(API_ENDPOINT_URL, {
		method: 'post',
		headers: {
			'Content-Type': 'application/json',
		},
		body: "{query: 'query { allUsers { id name } }'}",
	})
400 bad request
because of cors
Copy code
Fetch API cannot load <https://api.graph.cool/simple/v1/>.... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<http://localhost:3000>' is therefore not allowed access. The response had HTTP status code 400. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
that is, with
JSON.stringify
j
ok that works
i'm still confused though 🙂
why was it throwing a cors error?
and also, i copied from the "Generate Code" button on the playground and that fails too
n
😕
apparently we're not handling bad requests correctly
j
ah
i couldn't find any other similar issues - i can't be the first 😐
strange
anyway thanks a lot for your help 🙂
n
messages on Slack are very short lived 😞
but I'm sorry, where did you find this generate code button?
in the Console?
j
https://console.graph.cool/{my app name}/playground
in the bottom right of the query editor there's a
GENERATE CODE
button. opens a modal with some copypasta
n
yea thanks! 🙂
j
👍