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!
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.
Apollo Client should work just fine on Node. You only have to install cross-fetch.
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.
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.
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.
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.
The ApolloClient class encapsulates Apollo's core client-side API. It backs all available view-layer integrations (React, iOS, and so on).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With