Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tverstraete opened this issue Sep 14, 2022 · 9 comments
Closed

Comments

@tverstraete
Copy link

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.

@bummzack
Copy link

bummzack commented Sep 20, 2022

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 sveltejs/kit#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'
	});
}

@tverstraete
Copy link
Author

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

@bummzack
Copy link

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

@tverstraete
Copy link
Author

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.

@bummzack
Copy link

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

@MeloHenrique
Copy link

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

Any suggestions?

@tverstraete
Copy link
Author

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

@bummzack
Copy link

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?

@cpgruber
Copy link

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

mlabrum added a commit to mlabrum/graphql-request that referenced this issue Nov 3, 2022
mlabrum added a commit to mlabrum/graphql-request that referenced this issue Nov 3, 2022
…plication/graphql-response+json content type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants