graphql-request icon indicating copy to clipboard operation
graphql-request copied to clipboard

Getting error but error message contains the actual data I am looking for

Open tverstraete opened this issue 3 years ago • 8 comments

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.

tverstraete avatar Sep 14 '22 14:09 tverstraete

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'
	});
}

bummzack avatar Sep 20 '22 06:09 bummzack

Hi @bummzack , I am not using SvelteKit as far as I know but will check it out.

tverstraete avatar Sep 21 '22 08:09 tverstraete

Ok, maybe your graphQL Server doesn't sent the proper content-type header? That would result in the same behavior.

bummzack avatar Sep 21 '22 08:09 bummzack

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.

tverstraete avatar Sep 21 '22 09:09 tverstraete

I suggest you check if the response from your graphql-endpoint contains the proper content-type header… it should be application/json

bummzack avatar Sep 21 '22 09:09 bummzack

I am getting the same problem, my graphql-endpoint returns "application/graphql+json". It can't be modified.

Any suggestions?

MeloHenrique avatar Sep 28 '22 21:09 MeloHenrique

Hi @MeloHenrique , I have not been able to fix it for now.

tverstraete avatar Sep 29 '22 18:09 tverstraete

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?

bummzack avatar Sep 30 '22 08:09 bummzack

@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.

cpgruber avatar Oct 11 '22 15:10 cpgruber