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

Add defaultHeaders configuration option #2145

Closed
marceloverdijk opened this issue Jan 17, 2022 · 4 comments
Closed

Add defaultHeaders configuration option #2145

marceloverdijk opened this issue Jan 17, 2022 · 4 comments

Comments

@marceloverdijk
Copy link

marceloverdijk commented Jan 17, 2022

The GraphiQL configuration options already have:

  /**
   * Optionally provide the query in a controlled-component manner. This will override the user state.
   *
   * If you just want to provide a different initial query, use `defaultQuery`
   */
  query?: string;

  /**
   * provide a json string that controls the headers editor state
   */
  headers?: string;

  /**
   * The defaultQuery present when the editor is first loaded
   * and the user has no local query editing state
   * @default "A really long graphql # comment that welcomes you to GraphiQL"
   */
  defaultQuery?: string;

It would be nice to add a defaultHeaders?: string option.

It could e.g be set to: { "X-API-Key": "<ENTER YOUR API KEY>" } to more easier help people fill in their API key.

    ReactDOM.render(
        React.createElement(GraphiQL, {
            fetcher: gqlFetcher,
            defaultVariableEditorOpen: true,
            headerEditorEnabled: true,
            shouldPersistHeaders: true,
            defaultHeaders: '{ "X-API-Key": "<ENTER YOUR API KEY>" }'
        }),
        document.getElementById('graphiql'),
    );
@marceloverdijk marceloverdijk changed the title Add defaultHeaders option Add defaultHeaders configuration option Jan 17, 2022
@SimenB
Copy link
Contributor

SimenB commented Aug 29, 2022

We currently pass headers, which is populated. And if I refresh the page after changing the default headers (and shouldPersistHeaders is true) that tab keeps the new headers, and a new tab is automatically created with the headers passed as headers. However, if I manually press the + to create a new tab, headers is empty.

Is that this same issue, or should I open up a separate bug report?

(this is using 2.0.2)

@thomasheyenbrock
Copy link
Collaborator

@SimenB the behavior that you describe is actually expected. During the initial render, GraphiQL checks the latest state for tabs persisted in localStorage and compares it with the "controlled props" for editor values (query, variables and headers). If no tab exist that matches these props, then a new one is added.

These controlled props are not designed for defining initial editor values, changing these props "on runtime" would for example also override the editor contents with the new value of the prop. If sounds to me that what you would like to have is exactly the defaultHeaders prop that is described in this issue.

@SimenB
Copy link
Contributor

SimenB commented Aug 29, 2022

Great, thanks for clarifying 👍

@acao
Copy link
Member

acao commented Nov 12, 2022

This was just added in #2886, enjoy!

@acao acao closed this as completed Nov 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants