-
Notifications
You must be signed in to change notification settings - Fork 59
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
How to dynamically update the link headers in the binding instance ? #164
Comments
@cRicateau were you able to figure this out? I have the same question |
I had similar use-case and I was able to figure it out. You can extend import { HttpLink } from 'apollo-link-http';
import { fetch } from 'cross-fetch';
import { Request } from 'express';
class HttpLinkWithHeaders extends HttpLink {
// Whole request or only headers if you want
private req: Request = null;
constructor() {
super({
uri: `....`,
fetch: (input: RequestInfo, init?: RequestInit) => {
// Modify init.headers based on this.req here
return fetch(input, init);
},
});
}
// Call this on every request
public setHtppRequest = (req: Request) => {
this.req = req;
};
} |
You can use the const fetch = require('node-fetch');
const { Binding } = require('graphql-binding');
const { HttpLink } = require('apollo-link-http');
const { makeRemoteExecutableSchema, introspectSchema } = require('graphql-tools');
const { setContext } = require('apollo-link-context');
const http = new HttpLink({ uri: process.env.GRAPHQL_ENDPOINT, fetch });
const link = setContext((request, { graphqlContext }) => (
graphqlContext ? {
headers: {
Authorization: `Bearer ${graphqlContext.auth}`,
},
} : {})).concat(http);
const schemaPromise = introspectSchema(link);
module.exports.graphql = schemaPromise
.then(typeDefs => new class GraphQLBinding extends Binding {
constructor() {
super({
schema: makeRemoteExecutableSchema({ link, schema: typeDefs }),
});
}
}()); You can specify the context as a property of the third parameter in your bindings: const auth = 'auth token';
const result = await bindings.query.whatevers({
skip: 0,
limit: 10,
}, `{ items { id, body }}`, { context: { auth } }); |
Thank you for reporting. In the last few months, since the transition of many libraries under The Guild's leadership, We've reviewed and released many improvements and versions to graphql-cli, graphql-config and graphql-import. We've reviewed What we've found is that the new GraphQL Mesh library is covering not only all the current capabilities of GraphQL Binding, but also the future ideas that were introduced in the original GraphQL Binding blog post and haven't come to life yet. And the best thing - GraphQL Mesh gives you all those capabilities, even if your source is not a GraphQL service at all! Just like GraphQL Binding, you get a fully typed SDK (thanks to the protocols SDKs and the GraphQL Code Generator), but from any source, and that SDK can run anywhere, as a connector or as a full blown gateway. If you think that we've missed anything from GraphQL Binding that is not supported in a better way in GraphQL Mesh, please let us know! In the context of that particular issue - GraphQL Mesh We're looking forward for your feedback of how we can make your experience even better! |
@cRicateau We also observed the memory leak when creating an instance of |
@timotgl could you share a simple example in a repo? |
I use graphql-binding in an express server to query another graphql server. On each request, I need to pass a header whose value changes everytime (it comes from the express
req
object and it is used for tracing ). I create a newHttpLink
and a newGraphQLServiceBinding
instance each time, but it creates a memory leak on my server (see code below).Is there a way to dynamically change the headers in my
link
instance without re-creating a new instance ofGraphQLServiceBinding
each time ?I use the function
getGraphqlClient
to create a graphql client in all my requests:The text was updated successfully, but these errors were encountered: