我做了一个基本的实现 ApolloClient
:
const gqlClient = new ApolloClient({
connectToDevTools: true,
link: new HttpLink({
uri: "/api",
}),
cache: new InMemoryCache(),
resolvers: {
Group: {
icon: () => "noIcon",
},
},
typeDefs: gql`
extend type Group {
icon: String!
}
`,
});
唯一花哨的是一个解析器和类型def - 这两个都是为了支持一个 icon
字段(即将推出的功能)。
然后我试着用下面的方法查询服务器。
gqlClient.query({
query: gql`{
groups {
name
icon
}
}`,
})
.then(console.log);
然后得到一个很大的错误。
bundle.esm.js:63 Uncaught (in promise) Error: GraphQL error: Cannot query field `icon' on type `Group'.
at new ApolloError (bundle.esm.js:63)
at bundle.esm.js:1247
at bundle.esm.js:1559
at Set.forEach (<anonymous>)
at bundle.esm.js:1557
at Map.forEach (<anonymous>)
at QueryManager../node_modules/apollo-client/bundle.esm.js.QueryManager.broadcastQueries (bundle.esm.js:1555)
at bundle.esm.js:1646
at Object.next (Observable.js:322)
at notifySubscription (Observable.js:135)
运行相同的查询,而不要求 icon
完美的工作。 我不太清楚我做错了什么。 我怎么能嘲笑 icon
并修复这个错误?
我只运行了Apollo客户端--我需要运行Apollo服务器来获得所有的功能吗? 传出的请求似乎没有我的任何类型def信息,所以我不确定Apollo服务器会有什么不同。
gqlClient.query({
query: gql`
{
groups {
name
icon @client
}
}
`,
});