在 redux 操作中反应 apollo 多个查询

React apollo multiple query in redux action

当我尝试执行两个不同的查询时出现此错误我该如何解决

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

这是代码片段

//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{
 ....

认为你的问题是这个client.query({ query2 })client.query({query})

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

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