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

Dynamic headers in queries are not possible #10202

Closed
electrovir opened this issue Oct 18, 2022 · 2 comments · Fixed by #10215
Closed

Dynamic headers in queries are not possible #10202

electrovir opened this issue Oct 18, 2022 · 2 comments · Fixed by #10215
Assignees
Labels

Comments

@electrovir
Copy link

electrovir commented Oct 18, 2022

Intended outcome:

I am able to make multiple queries with the same client, with the same GraphQL query, but with different custom headers.

Actual outcome:

The first set custom header is used for all following queries.

How to reproduce the issue:

edit: moved the client definition outside of the fetch function.

const client = new ApolloClient({
    uri: 'http://localhost:4000/graphql',
    cache: new InMemoryCache(),
});

export async function fetchUsers(role: string) {

    await client.query<any>({
        query: gql`
            {
                users {
                    id
                }
            }
        `,
        context: {
            headers: {
                role,
            },
        },
    });

    return data.data.users;
}

Workaround:

Using a fetchPolity of no-cache makes no difference. The only workaround I've figured out so far is to create a new client for every request:

function getClient() {
    const client = new ApolloClient({
        uri: 'http://localhost:4000/graphql',
        cache: new InMemoryCache(),
    });
    return client;
}

export async function fetchUsers(role: string) {
    await getClient().query<any>({
        query: gql`
            {
                users {
                    id
                }
            }
        `,
        context: {
            headers: {
                role,
            },
        },
    });

    return data.data.users;
}

Versions

  System:
    OS: macOS 12.6
  Binaries:
    Node: 16.17.0 - ~/.nvm/versions/node/v16.17.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.17.0/bin/yarn
    npm: 8.15.0 - ~/.nvm/versions/node/v16.17.0/bin/npm
  Browsers:
    Chrome: 104.0.5112.101
    Firefox: 104.0.2
    Safari: 16.0
@bignimbus
Copy link
Contributor

Tagging @MrDoomBringer since his open PR should address this issue - thanks @MrDoomBringer! And thanks @electrovir for opening this issue!

@electrovir
Copy link
Author

Oops, I just realized my "How to reproduce the issue" and "Workaround" code were actually the same. I modified the "How to reproduce the issue" example to remedy that. The issue occurs when using a single instance of a client, which I assume is preferred?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants