2

I have been trying to implement Apollo's new gateway using typescript. But I keep running into a single typescript issue that I just cant seem to solve. The error message is "Property 'userId' does not exist on type 'TContext'.ts(2339)". Note I am trying to implement on AWS Lambda.

As far I can see by looking at the package source code TContext = Record is an object but it doesnt appear to resolve.

import { ApolloServer } from 'apollo-server-lambda';
import { ApolloGateway, RemoteGraphQLDataSource } from '@apollo/gateway';


const gateway = new ApolloGateway({
  serviceList: [
    { name: 'users', url: 'xx' },
    { name: 'precedents', url: 'xx' }
    // other services
  ],
  buildService({ 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 && request.http && request.http.headers.set('user-id', context.userId);
      }
    });
  }
});

const server = new ApolloServer({
  gateway,
  subscriptions: false,
  context: ({ event, context }) => {
    // get the user token from the headers
    const token = event.headers.authorization || '';

    // try to retrieve a user with the token
    const userId = token;

    // add the user to the context
    return {
      headers: event.headers,
      functionName: context.functionName,
      event,
      context,
      userId
    };
  }
});

exports.handler = server.createHandler({
  cors: {
    origin: true,
    credentials: true,
    methods: ['POST', 'GET'],
    allowedHeaders: ['Content-Type', 'Origin', 'Accept', 'authorization']
  }
});

Edit Updating per below seems to lead to same issue.

interface MyContext{
    userId: string;
}

const gateway = new ApolloGateway({
  serviceList: [
    { name: 'users', url: 'xx' },
    { name: 'precedents', url: 'xx' }
    // other services
  ],
  buildService({ url }) {
    return new RemoteGraphQLDataSource({
      url,
      willSendRequest<MyContext>({ request, context } : {request: GraphQLRequest, context: MyContext}) {
        // pass the user's id from the context to underlying services
        // as a header called `user-id`
        request && request.http && request.http.headers.set('user-id', context.userID);
      }
    });
  }
});
Fallz
  • 47
  • 1
  • 6

1 Answers1

0

First, create an interface, that will describe your graphql context

interface MyContext{
    userId: string;
}

Then add MyContext type parameter to willSendRequest method like this

...
willSendRequest<MyContext>({request, context}) {
...

Now the compiler knows, that context is of type MyContext.

  • 1
    Thanks for the response. Sorry I am still fairly new to Typescript and still unable to resolve the Typescript errors. The updated code still gives the same error? Have updated above with the new code. – Fallz Sep 14 '19 at 05:33
  • willSendRequest doesn't accept a type. RemoteGraphQLDataSource, however, does but that still doesn't quite do it for me. Gets close, but not all the way. – GhostBytes Jan 14 '22 at 20:41