使用 apollo-link-state 查询给出错误 "Field <name> doesn't exist on type 'Query'"
Querying with apollo-link-state gives the error "Field <name> doesn't exist on type 'Query'"
我对 Apollo 和 GraphQL 都是全新的。我跟着 this apollo-link-state-tutorial,遇到了绊脚石。
我已将 link 设置为 currentGame
属性 默认值。
const stateLink = withClientState({
cache: stateCache,
defaults: {
currentGame: {
__typename: 'currentGame',
teamAScore: 0
}
}
})
我正在我的客户端中使用它。
const client = new ApolloClient({
stateCache,
link: stateLink,
...
})
我正在像这样定义一个 GraphQL 查询:
const getCurrentGame = gql`
query {
currentGame @client {
teamAScore
}
}
`
我正在将它连接到我组件的 props。
export default compose(
graphql(getCurrentGame, {
props: ({ data: { currentGame }}) => ({
currentGame
})
})
)
这会在控制台中产生一个错误。
[GraphQL error]: Message: Field 'currentGame' doesn't exist on type 'Query', Location: [object Object], Path: undefined
我检查了我的代码,但未能发现肯定是拼写错误或简单错误的地方。我该如何调试此错误消息,或者它表明问题出在哪里?
更新: 我已尝试按照 Tal Z 的建议添加解析器,但仍然收到相同的错误消息。
const stateCache = new InMemoryCache()
const stateLink = withClientState({
cache: stateCache,
resolvers: {
Query: {
currentGame: () => {
return {}
}
}
},
defaults: defaultState
})
就其价值而言,我发现的少数示例存储库中的大多数都对未定义解析器的字段进行查询。例如,this queries for todo list items, but the only resolver defined is for a mutation.
好吧,我想通了...这中断了:
import ApolloClient from 'apollo-boost'
这个有效:
import ApolloClient from 'apollo-client'
我不知道有什么区别。
我对 Apollo 和 GraphQL 都是全新的。我跟着 this apollo-link-state-tutorial,遇到了绊脚石。
我已将 link 设置为 currentGame
属性 默认值。
const stateLink = withClientState({
cache: stateCache,
defaults: {
currentGame: {
__typename: 'currentGame',
teamAScore: 0
}
}
})
我正在我的客户端中使用它。
const client = new ApolloClient({
stateCache,
link: stateLink,
...
})
我正在像这样定义一个 GraphQL 查询:
const getCurrentGame = gql`
query {
currentGame @client {
teamAScore
}
}
`
我正在将它连接到我组件的 props。
export default compose(
graphql(getCurrentGame, {
props: ({ data: { currentGame }}) => ({
currentGame
})
})
)
这会在控制台中产生一个错误。
[GraphQL error]: Message: Field 'currentGame' doesn't exist on type 'Query', Location: [object Object], Path: undefined
我检查了我的代码,但未能发现肯定是拼写错误或简单错误的地方。我该如何调试此错误消息,或者它表明问题出在哪里?
更新: 我已尝试按照 Tal Z 的建议添加解析器,但仍然收到相同的错误消息。
const stateCache = new InMemoryCache()
const stateLink = withClientState({
cache: stateCache,
resolvers: {
Query: {
currentGame: () => {
return {}
}
}
},
defaults: defaultState
})
就其价值而言,我发现的少数示例存储库中的大多数都对未定义解析器的字段进行查询。例如,this queries for todo list items, but the only resolver defined is for a mutation.
好吧,我想通了...这中断了:
import ApolloClient from 'apollo-boost'
这个有效:
import ApolloClient from 'apollo-client'
我不知道有什么区别。