0

In app.module.ts I have the following:

    @Module({
      imports: [
        ...,
        GraphQLModule.forRoot<ApolloGatewayDriverConfig>({
          server: {
            context: getContext,
          },
          driver: ApolloGatewayDriver,
          gateway: {
           buildService: ({ name, url }) => {
              return new RemoteGraphQLDataSource({
                url,
                willSendRequest({ request, context }: any) {
                  ...
                },
              });
            },
            supergraphSdl: new IntrospectAndCompose({
              subgraphs: [
                { name: 'iam', url: API_URL_IAM },
              ],
            })
          },
        }),
    ]
...
})

here getContext is just a regular function which is not part of nestjs context (doesn't have injection, module capability) like below:

export const getContext = async ({ req }) => {
   return {}
}

Is there any way to use nestjs services instead of plain old functional approach to build the context for graphql gateway in nestjs?

Thanks in advance for any kind of help.

1 Answers1

0

I believe you're looking to create a service that is @Injectable and you can use that injectable service via a provider. What a provider will do is satisfy any dependency injection necessary.

In your scenario, I would import other modules as necessary. For building context, I would create a config file to create from env variables. Then create a custom provider that reads from the env variables and provides that implementation of the class/service to the other classes as their dependency injection.

For example, if I have a graphQL module. I would import the independent module. Then, I would provide in the providers section, the handler/service classes and the dependencies as an @injectable. Once your service class is created based on your config (which your provider class would handle), you would attach that service class to your GraphQL class to maybe lets say direct the URL to your dev/prod envs.