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

Allow dynamic headers #296

Open
LucaFilitz opened this issue Oct 27, 2022 · 5 comments
Open

Allow dynamic headers #296

LucaFilitz opened this issue Oct 27, 2022 · 5 comments

Comments

@LucaFilitz
Copy link

Hello,
when you create a upload link, you can only set the headers once.
This is a problem when you refresh your authentication token.

Apollo allows to pass a function to the connecitonParams so that every time a request is made the token is fetched. This is better.

What is possible:
createUploadLink({ uri: "SOME URI", headers: { Authorization: "Bearer " + get(token) } })

What I need:
createUploadLink({ uri: "SOME URI", headers: () => { return { Authorization: "Bearer " + get(token) } } })

Currently, when I do this no headers are passed.

Could you fix this?
Kind Regards
Luca

@jaydenseric
Copy link
Owner

Is that a new-ish thing in Apollo that you can do that?

For now, you should be able to create the headers each request via a custom Apollo Link:

https://www.apollographql.com/docs/react/networking/advanced-http-networking/#customizing-request-logic

@LucaFilitz
Copy link
Author

I think it is kind of new. But not that new.
Oh, yes that is corretct I didn't think about adding a middleware.
Still, it would be nice to not need it :)
I might open a pull request if you don't mind.

@LucaFilitz
Copy link
Author

LucaFilitz commented Oct 27, 2022

It looks like
selectHttpOptionsAndBody (createUploadLink.js l. 118)
somehow validates the headers.
Therfor you'd have to rewrite that part/ create your own function.

To be honest I do not know what it does and do not want to touch it.

=> I will use the middleware for now.

@sashrika
Copy link

This worked for me.

const httpOptions = {
timeout: 15000,
uri: GRAPHQL_SERVER_V1,
};

const authMiddleware = setContext((operation, { headers }) => {
    return getCredentials().then(data => {
      return {
        headers: {
          ...headers,
          authorization: `Bearer ${data?.accessToken}` || null,
        }
      };
    });
  });
  

const singleLink = split(
    ({ query }) => {
        const definition = getMainDefinition(query);
        return (
            definition.kind === "OperationDefinition" &&
            definition.operation === "subscription"
        );
    },
    wssLink,
    split(
        operation => operation.getContext().hasUpload,
        from([authMiddleware, createUploadLink({ ...httpOptions, isExtractableFile: customIsExtractableFile })]),
        from([authMiddleware, httpLink])
    )
);

@ilbertt
Copy link

ilbertt commented Aug 21, 2024

This worked for me.

const httpOptions = { timeout: 15000, uri: GRAPHQL_SERVER_V1, };

const authMiddleware = setContext((operation, { headers }) => {
    return getCredentials().then(data => {
      return {
        headers: {
          ...headers,
          authorization: `Bearer ${data?.accessToken}` || null,
        }
      };
    });
  });
  

const singleLink = split(
    ({ query }) => {
        const definition = getMainDefinition(query);
        return (
            definition.kind === "OperationDefinition" &&
            definition.operation === "subscription"
        );
    },
    wssLink,
    split(
        operation => operation.getContext().hasUpload,
        from([authMiddleware, createUploadLink({ ...httpOptions, isExtractableFile: customIsExtractableFile })]),
        from([authMiddleware, httpLink])
    )
);

My operation context doesn't have the hasUpload key. Did you set it manually in the mutation?

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