【问题标题】:Graphql query fails for deep local state in ApolloApollo 中深度本地状态的 Graphql 查询失败
【发布时间】:2019-04-16 06:04:37
【问题描述】:

我正在创建一个 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 在我所在的州建立一些结构?

这些是我的相关依赖项:

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"

【问题讨论】:

    标签: reactjs apollo react-apollo apollo-link-state


    【解决方案1】:

    已解决:

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

    const defaults = {
        author: {
            __typename: 'Author'
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-12
      • 2020-01-23
      • 2022-07-15
      • 2020-06-07
      • 2017-12-02
      • 2021-07-05
      • 2019-01-07
      • 2021-09-23
      • 2018-10-03
      相关资源
      最近更新 更多