Typescript errors for context object in Apollo gateway
See original GitHub issuePackage Versions:
@apollo-gateway: 0.10.4
Using the code below (from: [https://www.apollographql.com/docs/apollo-server/federation/implementing/]) I wouldn’t expect to see any errors when using typescript.
const gateway = new ApolloGateway({
serviceList: [
{ name: 'products', url: 'http://localhost:4001' },
// other services
],
buildService({ name, url }) {
return new RemoteGraphQLDataSource({
url,
willSendRequest({ request, context }) {
// pass the user's id from the context to underlying services
// as a header called `user-id`
request.http.headers.set('user-id', context.userId);
},
});
},
});
However, typescript warns me that “Property ‘userId’ does not exist on type ‘TContext’.”
Looking at the underlying code it appears that willSendRequest should accept any key value pair with a string but that doesn’t appear to be the case. Although Im not sure where the error could be.
Sorry if I have missed anything, Im still learning some of the advanced methods in typescript.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Error handling - Apollo GraphQL Docs
Whenever you throw a GraphQLError , you can add arbitrary fields to the error's extensions object to provide additional context to the client....
Read more >Apollo gateway with typescript wont accept user ID in context
But I keep running into a single typescript issue that I just cant seem to solve. The error message is "Property 'userId' does...
Read more >GraphQL error handling to the max with Typescript ...
It allows to build a context object that keeps track of the unwrapped values produced by the different operations in the pipeline. After...
Read more >Top 5 @apollo/gateway Code Examples
Learn more about how to use @apollo/gateway, based on @apollo/gateway code ... willSendRequest({ request, context }) { Object.keys(context.headers).
Read more >GraphQL Server Tutorial with Apollo Server and Express
Think about it as similar to a JavaScript object with objects or arrays inside ... and pass it in the context object when...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Adding to this issue, if a type of
{ request: GraphQLRequest, context: Context }
is added, TypeScript will complain that the providedContext
isn’t compatible withTContext
.It looks the author misunderstands the way to use generic type in the class. It should pass generic type from class to it’s method, but in code it only set generic type on the method.
The correct one would be:
I can make a PR later.