Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(core): Send Accept application/graphql+json header (#2457)

* feat(core): Add `Accept` header to GraphQL requests

* Add changeset

* Update snapshots

authored by

Phil Pluckthun and committed by
GitHub
9215f1a0 2695fb93

+26 -9
+6
.changeset/spotty-ligers-play.md
··· 1 + --- 2 + '@urql/core': minor 3 + --- 4 + 5 + Add `Accept` header to GraphQL HTTP requests. This complies to the specification but doesn't go as far as sending `Content-Type` which would throw a lot of APIs off. Instead, we'll now be sending an accept header for `application/graphql+json, application/json` to indicate that we comply with the GraphQL over HTTP protocol. 6 + This also fixes headers merging to allow overriding `Accept` and `Content-Type` regardless of the user options' casing.
+10 -2
exchanges/multipart-fetch/src/__snapshots__/multipartFetchExchange.test.ts.snap
··· 572 572 } 573 573 `; 574 574 575 - exports[`on success uses a file when given 2`] = `Object {}`; 575 + exports[`on success uses a file when given 2`] = ` 576 + Object { 577 + "accept": "application/graphql+json, application/json", 578 + } 579 + `; 576 580 577 581 exports[`on success uses a file when given 3`] = `FormData {}`; 578 582 ··· 712 716 } 713 717 `; 714 718 715 - exports[`on success uses multiple files when given 2`] = `Object {}`; 719 + exports[`on success uses multiple files when given 2`] = ` 720 + Object { 721 + "accept": "application/graphql+json, application/json", 722 + } 723 + `; 716 724 717 725 exports[`on success uses multiple files when given 3`] = `FormData {}`;
+10 -7
packages/core/src/internal/fetchOptions.ts
··· 71 71 body?: FetchBody 72 72 ): RequestInit => { 73 73 const useGETMethod = shouldUseGet(operation); 74 - 74 + const headers: HeadersInit = { 75 + accept: 'application/graphql+json, application/json', 76 + }; 77 + if (!useGETMethod) headers['content-type'] = 'application/json'; 75 78 const extraOptions = 76 - typeof operation.context.fetchOptions === 'function' 79 + (typeof operation.context.fetchOptions === 'function' 77 80 ? operation.context.fetchOptions() 78 - : operation.context.fetchOptions || {}; 79 - 81 + : operation.context.fetchOptions) || {}; 82 + if (extraOptions.headers) 83 + for (const key in extraOptions.headers) 84 + headers[key.toLowerCase()] = extraOptions.headers[key]; 80 85 return { 81 86 ...extraOptions, 82 87 body: !useGETMethod && body ? JSON.stringify(body) : undefined, 83 88 method: useGETMethod ? 'GET' : 'POST', 84 - headers: useGETMethod 85 - ? extraOptions.headers 86 - : { 'content-type': 'application/json', ...extraOptions.headers }, 89 + headers, 87 90 }; 88 91 };