Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ApolloClient is not a constructor (apollo-client with nodejs)

I don't have any UI framework. Just a simple Nodejs script where I need to query a GraphQL.

Codes:

const ApolloClient = require('apollo-client')
const client = new ApolloClient()

Error message:

TypeError: ApolloClient is not a constructor

Package.json:

{
  ...

  "dependencies": {
    "apollo-client": "^2.4.13",
    "graphql": "^14.1.1",
    "graphql-tag": "^2.10.1"
  },
}

Node: v8.9.4

I googled a while people have this issue mainly because ApolloClient is no longer in react-apollo. You have to import it from 'apollo-client'

And I'm importing from apollo-client as const ApolloClient = require('apollo-client')

Any ideas? Thanks!

like image 642
mCY Avatar asked Feb 14 '19 10:02

mCY


People also ask

What is ApolloClient?

Apollo Client is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. Use it to fetch, cache, and modify application data, all while automatically updating your UI.

Does Apollo Client work in node?

Apollo Client should work just fine on Node. You only have to install cross-fetch.

Is react Apollo deprecated?

The react-apollo package has been deprecated, and the functionality offered by each of the above packages can now be accessed from @apollo/client directly: @apollo/react-hooks -> now available directly from @apollo/client.

What is Apollo client in Node JS?

Apollo Client is a complete state management library for JavaScript apps. It's a powerful tool since it can be used on both the back end and front end side. In this tutorial, we will use it on the front end and back end by building an Apollo GraphQL Server with Node JS.

Can Apollo client be used as a dependency for react?

I've been experimenting with Apollo Client recently and started work on a new package that will use it as a dependency. After some initial experiments in a Create React App project, I scaffolded a new vanilla JS project using Vite. Much to my surprise, when I added Apollo my build started failing because React was missing as a dependency.

Does Apollo client have caching?

It also has zero-configuration caching. Just by setting up Apollo Client in your app, you get an intelligent cache out of the box, with no additional configuration required. Apollo Client is also interoperable with other frameworks, such as Angular, Vue.js, and React.

What is the apolloclient class?

The ApolloClient class encapsulates Apollo's core client-side API. It backs all available view-layer integrations (React, iOS, and so on).


1 Answers

If you're using require, you can import it like this:

const ApolloClient = require('apollo-client').default

or like this

const { ApolloClient } = require('apollo-client')

Otherwise, you're importing the entire module, which itself is not a constructor.

like image 87
Daniel Rearden Avatar answered Oct 25 '22 04:10

Daniel Rearden