Apollo-server: Class constructor RESTDataSource cannot be invoked without 'new'

Created on 20 Jul 2018  路  6Comments  路  Source: apollographql/apollo-server

Im new to Apollo and GraphQL and Im trying to get a basic server up and running in Typescript however whenever I try to query my local playground I get the following error response:

Class constructor RESTDataSource cannot be invoked without 'new'

I've followed the docs and have setup my server as follows:

import { RESTDataSource, RequestOptions } from "apollo-datasource-rest";

export class MoviesAPI extends RESTDataSource {
  apiKey = "xxx";

  constructor() {
    super();
    this.baseURL = "https://api.themoviedb.org/3/";
  }

  willSendRequest(request: RequestOptions) {
    request.params.set("api_key", this.apiKey);
  }

  async getMovie(id: string) {
    return this.get(`movies/${id}`);
  }
}

and

import { ApolloServer, IResolvers, gql } from "apollo-server";
import { MoviesAPI } from "./dataSources";

const typeDefs = gql`
  type Movie {
    backdrop_path: String
    id: Int
    overview: String
    poster_path: String
    release_date: String
    tagline: String
    title: String
  }

  type Query {
    movie(id: ID!): Movie
  }
`;

const resolvers: IResolvers = {
  Query: {
    movie: async (obj, args, context) => {
      return context.dataSources.moviesAPI.getMovie(args.id);
    }
  }
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
  dataSources: () => {
    return {
      moviesAPI: new MoviesAPI()
    };
  }
});

server.listen().then(({ url }) => {
  console.log(`馃殌  Server ready at ${url}`);
});

Can anyone offer some insight? Thanks

Most helpful comment

What target do you have specified in your tsconfig.json? If you're transpiling to ES5, extending native classes may not work. With modern versions of Node, a target of es2016 (for Node 6) or even higher should be ok.

All 6 comments

What target do you have specified in your tsconfig.json? If you're transpiling to ES5, extending native classes may not work. With modern versions of Node, a target of es2016 (for Node 6) or even higher should be ok.

Thanks for the tip. I've updated my tsconfig.json to the following:

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "strict": false,
    "outDir": "dist",
    "sourceMap": true,
    "noImplicitAny": true,
    "jsx": "react",
    "skipLibCheck": true
  },
  "exclude": ["node_modules", "dist"],
  "include": ["src/**/*"]
}

But I'm still getting the same error.

Hmmm, that is really surprising. Is there any chance you're transpiling the resulting JavaScript further with Babel? Or else maybe you could try to clean your output directory and recompile?

Looks like I had some caching issues that we're preventing the change from taking affect. Thanks for your help!

If my compiler is targeting es5 and I'm unable to change it for other reasons, how would go about integrating a REST data source into my Apollo server?

Looks like I had some caching issues that we're preventing the change from taking affect. Thanks for your help!

@adampetrie how did you manage to solve it, what cashing issues?

Was this page helpful?
0 / 5 - 0 ratings