graphql-request
graphql-request copied to clipboard
Getting error but error message contains the actual data I am looking for
When using the following code:
const query = gql
query tenantContexts {
tenantContexts(cloudIds: ["${cloudId}"]) {
cloudId
hostName
}
}
`;
const {tenantContexts} = await request('https://api.atlassian.com/graphql', query).then((data) => console.log(data)); // tenantContexts contains cloudId and hostName `
I get an error 200 but the error contains my data and no real error message:
{ "response": { "error": "{\"data\":{\"tenantContexts\":[{\"cloudId\":\"xxxx\",\"hostName\":\yyy\"}]},\"extensions\":{\"gateway\":{\"request_id\":\"zzz\",\"crossRegion\":false,\"edgeCrossRegion\":false,\"deprecatedFieldsUsed\":[]}}}", "status": 200, "headers": {} }, "request" : { "query": "\n query hostNameForCloudId {\n tenantContexts(cloudIds: [\"xxxx"]) {\n cloudId\n hostNam e\n }\n }\n " } }
Is there something I am missing or doing wrong?
Thanks.
Are you using SvelteKit by any chance? We ran into the same issue, but it's actually related to SvelteKit. Their fetch implementation strips response headers and thus generates the error, as a response with content-type: application/json is expected (otherwise it'll just return "text"). Also see https://github.com/sveltejs/kit/pull/6569 (introduced at next.471)
The following code fixes it btw.
// hooks.server.js
export async function handle({ event, resolve }) {
return await resolve(event, {
filterSerializedResponseHeaders: name => name === 'content-type'
});
}
Hi @bummzack , I am not using SvelteKit as far as I know but will check it out.
Ok, maybe your graphQL Server doesn't sent the proper content-type header? That would result in the same behavior.
The weird thing is that with POSTMAN this does seem to work, so when running the same query in POSTMAN gives me the restul I would need and when running this with graphlql-request I get this weird effect that the data I want is in the error field as string. So the result is in the response but the response is wrong.
I suggest you check if the response from your graphql-endpoint contains the proper content-type header… it should be application/json
I am getting the same problem, my graphql-endpoint returns "application/graphql+json". It can't be modified.
Any suggestions?
Hi @MeloHenrique , I have not been able to fix it for now.
I think this issue is also related to #373
@MeloHenrique maybe you could create a PR that relaxes https://github.com/prisma-labs/graphql-request/blob/master/src/index.ts#L667 to allow other content-types, such as application/graphql+json?
@tverstraete Had the same issue using the default server from graphql-mesh. If you're using graphql-yoga (via graphql-mesh), send a header accept: application/json along with your request and that will set the response content-type header to application/json as well.