我正在创建一个apollo react应用程序。我想让阿波罗来管理我当地的州。我想要构造我的本地状态,这样并不是所有的标量值都在顶层。
这是我的配置:
import React from 'react'
import ReactDOM from 'react-dom'
import ApolloClient from 'apollo-boost'
import gql from 'graphql-tag'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { ApolloProvider, Query } from 'react-apollo'
const defaults = {
author: null
}
const resolvers = {
Query: {
author() {
return { id: 1 }
}
}
}
const typedefs = gql(`
type Query {
author: Author
}
type Author {
id: Int
}
`)
const apolloClient = new ApolloClient({
clientState: {
defaults,
resolvers,
typedefs
},
cache: new InMemoryCache()
});
ReactDOM.render(
<ApolloProvider client={ apolloClient }>
<Query query={ gql`{ author @client }` }>
{ ({ data }) => console.log(data.author) || null }
</Query>
</ApolloProvider>,
document.getElementById('app')
)然后,此应用程序会记录undefined。也就是说,查询{ author @client { id }}在data.author中返回undefined。
必须注意的是,当我将作者类型设置为Int,默认值为1,并执行查询{ author @client }时,应用程序会正确地记录1。
我怎样才能在阿波罗所在的州有一些结构呢?
以下是我的相关依赖项:
apollo-cache "^1.1.20"
apollo-cache-inmemory "^1.3.8"
apollo-client "^2.4.5"
apollo-link "^1.0.6"
apollo-link-error "^1.0.3"
apollo-link-http "^1.3.1"
apollo-link-state "^0.4.0"
graphql-tag "^2.4.2"发布于 2018-11-14 00:09:12
已解决:
显然,我必须以这种方式将__typename: 'Author'添加到默认作者:
const defaults = {
author: {
__typename: 'Author'
}
}https://stackoverflow.com/questions/53280445
复制相似问题