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

Chain headers option should not overwrite the default headers

Open ValentinH opened this issue 3 years ago • 0 comments

In this part: https://github.com/graphql-editor/graphql-zeus/blob/25d891938a7e2cb777321fee6437eaaf3ecfe956/src/TreeToTS/templates/typescript/browser/fetchFunction.ts#L34-L37

The fetchOptions are spread and if it contains a headers property, it overwrites the default 'Content-Type' header. I faced a quite subtle bug where we were using the headers options to set a specific header and did not explicitly set the Content-Type one.

I think the code could be written like this to keep a default Content-Type if the headers option is set without Content-Type:

return fetchFunction(`${options[0]}`, {
  body: JSON.stringify({ query: queryString, variables }),
  method: 'POST',
  ...fetchOptions,
  headers: {
    'Content-Type': 'application/json',
    ...fetchOptions.headers,
  },
})

If this sounds like a good idea, I'm ready to do a PR 🙂

ValentinH avatar Feb 08 '22 19:02 ValentinH