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?