Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
adampetrie opened this issue Jul 20, 2018 · 6 comments
Closed

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

adampetrie opened this issue Jul 20, 2018 · 6 comments

Comments

@adampetrie
Copy link

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
Copy link
Contributor

martijnwalraven commented Jul 21, 2018

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

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

@kylemh
Copy link

kylemh commented Oct 18, 2019

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
Copy link

ketimaBU commented Dec 4, 2020

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?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants