我没有任何UI框架。只是需要查询GraphQL的简单Node.js脚本。
代码:
const ApolloClient = require('apollo-client')
const client = new ApolloClient()
Run Code Online (Sandbox Code Playgroud)
错误信息:
TypeError: ApolloClient is not a constructor
Run Code Online (Sandbox Code Playgroud)
Package.json:
{
...
"dependencies": {
"apollo-client": "^2.4.13",
"graphql": "^14.1.1",
"graphql-tag": "^2.10.1"
},
}
Run Code Online (Sandbox Code Playgroud)
节点: v8.9.4
我用Google搜索了一会儿,这个人主要是因为 ApolloClient is no longer in react-apollo. You have to import it from 'apollo-client'
而我从进口apollo-client
为const ApolloClient = require('apollo-client')
有任何想法吗?谢谢!
对于喜欢我使用 Noderequire
并且只想让它工作的人。
套餐:
npm install graphql apollo-client apollo-cache-inmemory apollo-link-http node-fetch --save
代码:
const fetch = require('node-fetch')
const { createHttpLink } = require('apollo-link-http')
const { InMemoryCache } = require('apollo-cache-inmemory')
const { ApolloClient } = require('apollo-client')
const gql = require('graphql-tag')
const httpLink = createHttpLink({
uri: 'https://api.github.com/graphql',
fetch: fetch
})
const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache()
})
const query = gql`
query {
viewer {
login
}
}
`
client.query({
query
}).catch((error) => {
console.log(error)
done()
})
Run Code Online (Sandbox Code Playgroud)
响应是错误的,因为您需要添加Authorization: bearer YOURTOKEN
到请求标头中,但这是另一回事。
感谢这个答案
如果您使用require
,则可以这样导入:
const ApolloClient = require('apollo-client').default
Run Code Online (Sandbox Code Playgroud)
或像这样
const { ApolloClient } = require('apollo-client')
Run Code Online (Sandbox Code Playgroud)
否则,您将导入整个模块,而该模块本身不是构造函数。
归档时间: |
|
查看次数: |
831 次 |
最近记录: |