-
Notifications
You must be signed in to change notification settings - Fork 20
Question: how to forward the origin extra http header ? #6
Comments
Good question; we faced this use case, too. You can pass a custom class AuthForwardingGraphQLClient extends HttpGraphQLClient {
protected async getHeaders(document: DocumentNode, variables?: { [name: string]: any }, context?: any, introspect?: boolean): Promise<{ [index: string]: string }> {
const headers = await super.getHeaders(document, context, introspect);
return {
...headers,
Authentication: getAuthTokenFromGraphQLContext(context)
};
}
}
const schema: GraphQLSchema = await weaveSchemas({
endpoints: [{
namespace: 'library',
client: new AuthForwardingGraphQLClient('http://example.com/graphql')
}]
}); Then, you somehow need to implement function getAuthTokenFromGraphQLContext(context: any) {
if (!context) {
return undefined;
}
return context.header('Authentication');
} There is one pitfall: When you call |
I followed your advice , but I still can't make it work, this is the demo code https://github.com/gengjiawen/schema-stitching-demo/tree/feature/pass_header, can you take a look when you have free time ? Thanks. |
Sorry, I confused I only now realized you wrote Origin. Do you mean the HTTP Origin header? I assumed you meant authorization because you wrote token. If you want to forward the origin, just replace the header name. |
thanks, I will give it a try tomorrow. |
It works like as expected now, thanks. But why when i first running, I got log looks like this in the sample https://github.com/gengjiawen/schema-stitching-demo/tree/feature/pass_header? looks like the context not passed when get schema.
|
I see, that's because that time express-graphql is not used. Thanks for your great patience and help. |
For anyone want to pass all the original headers, you can do something like this: class AuthForwardingGraphQLClient extends HttpGraphQLClient {
protected async getHeaders(document: DocumentNode, variables?: { [name: string]: any }, context?: any, introspect?: boolean): Promise<{ [index: string]: string }> {
const headers = await super.getHeaders(document, context, introspect);
console.log('headers', headers)
if (context) {
console.log('context headers', context.headers)
delete context.headers['content-length']
return context.headers
} else {
return headers
}
}
} |
Hey Yogu, I was just working on this exact scenario last week and came up with pretty much the same solution you suggested. |
I need to pass the token to the server in my situation.
The text was updated successfully, but these errors were encountered: