apollographql / apollo-server

🌍  Spec-compliant and production ready JavaScript GraphQL server that lets you develop in a schema-first way. Built for Express, Connect, Hapi, Koa, and more.
https://www.apollographql.com/docs/apollo-server/
MIT License
13.79k stars 2.03k forks source link

Class constructor RESTDataSource cannot be invoked without 'new' #1388

Closed adampetrie closed 6 years ago

adampetrie commented 6 years ago

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

martijnwalraven commented 6 years ago

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.

adampetrie commented 6 years ago

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.

martijnwalraven commented 6 years ago

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?

adampetrie commented 6 years ago

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

kylemh commented 5 years ago

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?

ketimaBU commented 3 years ago

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?