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

Created on 18 Aug 2018  路  9Comments  路  Source: apollographql/apollo-server

Very similar in nature to https://github.com/apollographql/apollo-server/issues/1388, but I'm using Babel, not TypeScript

When using babel with apollo-datasource-rest I am getting the Class constructor RESTDataSource cannot be invoked without 'new'. Here's a simplified version of my code.

// myapi.js
import { RESTDataSource } from "apollo-datasource-rest";

export default class extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = "http://example.com";
  }

  hello() {
    return "Hello World!";
  }
}
// server.js
import express from "express";
import { ApolloServer } from "apollo-server-express";
import schema from "./data/schema";
import MyAPI from "./data/api/myapi";

const app = express();

const server = new ApolloServer({
  schema,
  dataSources: () => ({ MyAPI: new MyAPI() }),
});

server.applyMiddleware({ app, path: "/graphql" });

app.listen(8080, () => console.log(`Server is now running`));
// schema.js
import { makeExecutableSchema, gql } from "apollo-server-express";

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: (_, __, { dataSources: { MyAPI } }) => MyAPI.hello(),
  },
};

const schema = makeExecutableSchema({ typeDefs, resolvers });

export default schema;

Is there any way that I can utilize apollo-datasource-rest with babel?

Most helpful comment

I have tried all the workarounds I could find and still have the issue. Could this be reopened?

All 9 comments

Can you verify that you are using version 0.1.4 apollo-datasource-rest?

I saw this error when using an older version, and it was fixed by upgrading the package. If you are, I would delete the npm modules/reinstall etc to ensure it's not using the old version, just to confirm before diagnosing further.

https://github.com/seanli3/apoll-datasource-rest-bug-report/pull/1/files

Yes, currently using version 0.1.4 of apollo-datasource-rest according to my yarn.lock file and I removed my node_modules folder and re-installed my packages just to be sure. I am still getting the same error.

I created a stripped down project that will reproduce the error that I'm getting. https://github.com/danlunde/datasource-test. Doing a graphQL query { hello } produces the error in the response for me.

@danlunde - I added a pull request to your example repo, it seemed to have resolved the error, can you review? I'm not a babel expert, I just swapped out a few dev dependencies to what I use for starter repos and I didn't get any errors afterward.

I didn't add it in, but I like to write the babel transpile result to a folder usually, to see what is actually getting written. If in dev mode, it sometimes helps see where the error is.

@sbrichardson - That's a great tip, thank you. And thanks also for helping me understand babel a little more. This definitely solved my problem!

No problem! Could this issue be closed?

Yes, and thank you again!

I have tried all the workarounds I could find and still have the issue. Could this be reopened?

Was this page helpful?
0 / 5 - 0 ratings