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' using Babel #1550

Closed
danlunde opened this issue Aug 17, 2018 · 9 comments
Closed

Comments

@danlunde
Copy link

Very similar in nature to #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?

@danlunde danlunde changed the title Class constructor RESTDataSource cannot be invoked without 'new' with Babel Class constructor RESTDataSource cannot be invoked without 'new' using Babel Aug 18, 2018
@sbrichardson
Copy link
Contributor

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

@danlunde
Copy link
Author

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.

@danlunde
Copy link
Author

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.

@sbrichardson
Copy link
Contributor

@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.

@sbrichardson
Copy link
Contributor

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.

@danlunde
Copy link
Author

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

@sbrichardson
Copy link
Contributor

No problem! Could this issue be closed?

@danlunde
Copy link
Author

Yes, and thank you again!

@mschipperheyn
Copy link

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

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