0

我正在创建一个阿波罗反应应用程序。我希望阿波罗管理我当地的州。我想构建我的本地状态,因此并非所有标量值都在顶层。

这是我的配置:

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 }}返回undefineddata.author。

必须注意的是,当我将作者类型设置为 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"
4

1 回答 1

0

解决了:

显然我必须以__typename: 'Author'这种方式添加到默认作者:

const defaults = {
    author: {
        __typename: 'Author'
    }
}
于 2018-11-13T16:09:12.213 回答