【问题标题】:React apollo multiple query in redux action在 redux 操作中反应 apollo 多个查询
【发布时间】:2017-12-04 17:09:14
【问题描述】:

当我尝试做两个不同的查询时,我得到了这个错误我该如何解决它

``错误:需要查询选项。您必须在查询选项中指定您的 GraphQL 文档。

这是代码的 sn-p

//in my index class I dispatch some actions

componentDidMount () {
 this.props.fetchNavigationBarDataThunk();
 this.props.fetchActionToDoTitleDataThunk();
} 

//then in my action I have something like that 

import ApolloClient, { createNetworkInterface } from 'apollo-client';
import gql from 'graphql-tag';

const opts = {uri: 'http://localhost:8080/wfgen/graphql'};
const networkInterface = createNetworkInterface(opts);
const client = new ApolloClient({networkInterface});

var query1 = gql`query {...}`;

var query2= gql`query {...}`;


export function fetchActionToDoTitleDataThunk () {
return (dispatch) => {
dispatch(fetchActionToDoTitleData())
client.query({ query2 }).then((results) => {
  if (results.networkStatus === 7 && results.loading === false) {
    dispatch(fetchActionToDoTitleDataFulFilled(results));
  }
  else{
  ...


export function fetchNavigationBarDataThunk () {
return (dispatch) => {

dispatch(fetchNavigationBarData())
client.query({query}).then((results) => {
  if (results.networkStatus === 7 && results.loading === false) {
    dispatch(fetchNavigationBarDataFulFilled(results));
  }
  else{
 ....

【问题讨论】:

    标签: reactjs redux react-redux graphql apollo


    【解决方案1】:

    认为您的问题在于client.query({ query2 })client.query({query})

    首先,使用速记对象属性初始化,{ query2 } 等价于 { query2: query2 } - 但 ApolloClient 期望类似于 { query: query2 }

    其次,根据您提供的代码,query 未定义。我想你的意思是client.query({query: query1})client.query({query: query2})

    【讨论】:

    • 我只是想保持代码简短,所以我没有编写完整的查询,但感谢您的帮助
    猜你喜欢
    • 2018-02-16
    • 2017-12-27
    • 2017-04-07
    • 2017-11-04
    • 1970-01-01
    • 2018-02-09
    • 2021-01-12
    • 2019-02-07
    • 2017-04-14
    相关资源
    最近更新 更多