question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript errors for context object in Apollo gateway

See original GitHub issue

Package 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:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
miguelollercommented, Nov 4, 2019

Adding to this issue, if a type of { request: GraphQLRequest, context: Context } is added, TypeScript will complain that the provided Context isn’t compatible with TContext.

1reaction
LucienLeecommented, Feb 11, 2020

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:

export class RemoteGraphQLDataSource<TContext> implements GraphQLDataSource { // <- pass generic from class
  public willSendRequest?<TContext>( // <- so we could pass our own context type here
    requestContext: Pick<
      GraphQLRequestContext<TContext>,
      'request' | 'context'
    >,
  ): ValueOrPromise<void>;
}

I can make a PR later.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found