Search code examples
graphqlapollonestjsapollo-server

NestJS with Apollo DataSource


I have been trying to re-create the Apollo tutorial with NestJS. But when I try using apollo-datasource-rest with NestJS, it fails when fetching data from the external data source with the following error:

[Nest] 29974   - 07/14/2020, 9:33:20 PM   [ExceptionsHandler] Cannot read property 'fetch' of undefined +125971ms
TypeError: Cannot read property 'fetch' of undefined

It seems as if the data source class in not being injected properly in the resolver, but I can't figure out why?

// The data source class
@Injectable()
class LaunchAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = 'https://api.spacexdata.com/v2/';
  }
  async getLaunchById({ launchId }) {
    return await this.get('launches', { flight_number: launchId });
  }
}

// The Nest resolver
@Resolver('launch')
@Injectable()
export class LaunchResolver {
  constructor(private readonly  launchAPI: LaunchAPI) {}

  @Query('getLaunch')
  async getLaunch(
    @Args('id') id: string
  ) {
    return await this.launchAPI.getLaunchById({ launchId: id })
  }
}

// The app module
@Module({
  imports: [
    GraphQLModule.forRoot({
      dataSources,
      context: ({ req }) => {
        if (req) {
          return { headers: req.headers };
        }
      },
      typePaths: ['./**/*.graphql'],
      definitions: {
        path: join(process.cwd(), 'src/graphql.schema.ts'),
        outputAs: 'class',
      },
      debug: true,
    })
  ],
  controllers: [AppController],
  providers: [AppService, LaunchAPI, LaunchResolver],
})
export class AppModule {}

What's the best way to use Apollo's data source with Nest resolvers?


Solution

  • I was able to solve the issue by using the @Context decorator on each of my resolver methods in order to grab the apollo data source services (e.g. dataSources), instead of injecting the data source in the resolver class. So the updated looks as following:

    // The Nest resolver
    @Resolver('launch')
    @Injectable()
    export class LaunchResolver {
      @Query('getLaunch')
      async getLaunch(
        @Context('dataSources') { launchAPI }: DataSources,
        @Args('id') id: string
      ) {
        return await launchAPI.getLaunchById({ launchId: id })
      }
    }
    
    // app.module.ts
    
    // set up any dataSources our resolvers need
    const dataSources = () => ({
      launchAPI: new LaunchAPI(),
    });
    
    @Module({
      imports: [
        GraphQLModule.forRoot({
          dataSources,
          context: ({ req }) => {
            if (req) {
              return { headers: req.headers };
            }
          },
          typePaths: ['./**/*.graphql'],
          definitions: {
            path: join(process.cwd(), 'src/graphql.schema.ts'),
            outputAs: 'class',
          },
          debug: true,
        })
      ],
      controllers: [AppController],
      providers: [AppService, LaunchAPI, LaunchResolver],
    })
    export class AppModule {}