Graphql使用Apollo中的数组查询本地状态

时间:2019-03-02 14:58:50

标签: graphql apollo react-apollo

我正在服务器上的Apollo中设置属性,并且需要在客户端上查询它。

我将本地状态设置为(简化代码以使其可读):

const Navigation = {
  __typename: 'Navigation',
  Links     : [
    {__typename: 'Link', to: '/test-layout-page', text: 'Go to Home'},
    {__typename: 'Link', to: '/test-layout-page', text: 'Go to Page'},
    {__typename: 'Link', to: '/test-layout-article', text: 'Go to Article'},
  ],
};

  const staticContent =  {
    Navigation: Navigation
  }

  const stateLink = withClientState({
    cache,
    defaults: {
        staticContent
      },
  });

  return new ApolloClient({
    link: ApolloLink.from([stateLink, link]),
    cache,
    ssrMode: ssr,
    connectToDevTools: !ssr && !prod,
  });

然后在客户端上我查询:

const GET_STATE = gql`
  {
    renderType @client,
    path @client,
    staticContent @client {
      Navigation {
        Links
      }
    }
  }
`;

我得到的错误是与Links

  

错误:为以下类型的链接缺少对象的选择集   查询字段链接

我需要gql查询以返回数组中的所有项目,我尝试指定字段,

const GET_STATE = gql`
  {
    renderType @client,
    path @client,
    staticContent @client {
      Navigation {
        Links {
          text,
          to
        }
      }
    }
  }
`;

但我知道

  

错误:网络错误:无法读取未定义的属性“查询”

1 个答案:

答案 0 :(得分:0)

该错误在graphql查询中。正确的查询是:

const GET_STATE = gql`
  {
    renderType @client,
    path @client,
    staticContent @client {
      Navigation @client {
        Links @client {
          to,
          text
        }
      }
    }
  }
`;
相关问题