Reputation: 340
I am pretty new to TypeScript and Apollo. I'm trying to setup my Apollo server for the first time but I keep getting an error about 'DataSource'. I'm unsure what this means and how to fix it.
import { ApolloServer, gql } from 'apollo-server';
//...
const { RESTDataSource } = require('apollo-datasource-rest');
class MoviesAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = 'https://movies-api.example.com/';
}
async getMovie(id) {
return this.get(`movies/${id}`);
}
async getMostViewedMovies(limit = 10) {
const data = await this.get('movies', {
per_page: limit,
order_by: 'most_viewed',
});
return data.results;
}
}
const SERVERCONFIG = {
'typeDefs': gql(typeDefs) ,
resolvers,
dataSources: ()=> ({
MoviesAPI: new MoviesAPI(),
} ) ,
};
const server = new ApolloServer(SERVERCONFIG);
I get the following error:
Type '{ 'typeDefs': DocumentNode; resolvers: { Query: {}; }; dataSources: () => { MoviesAPI: MoviesAPI; }; }' is not assignable to type 'ApolloServerExpressConfig'.
The types returned by 'dataSources()' are incompatible between these types.
Type '{ MoviesAPI: MoviesAPI; }' is not assignable to type 'DataSources<object>'.
Property 'MoviesAPI' is incompatible with index signature.
Type 'MoviesAPI' has no properties in common with type 'DataSource<object>'.
I can get the error to go away if I did something like: dataSources: ()=> ({...MoviesAPI}) but I do not think that fixes the problem...
Does anyone know what is causing this?
Upvotes: 2
Views: 2285
Reputation: 3627
Change
const { RESTDataSource } = require('apollo-datasource-rest');
to
import { RESTDataSource } from 'apollo-datasource-rest';
This should fix it for you.
Upvotes: 3