首页 > 解决方案 > 未捕获的不变违规:需要查询选项。您必须在查询选项中指定您的 GraphQL 文档

问题描述

我正在尝试从graphql我的反应应用程序中的服务器获取国家/地区列表。该getAllCountry查询在操场上运行良好,但每当我在应用程序上调用相同的查询时,我都会收到以下错误

  1. “查询选项是必需的。您必须在查询选项中指定您的 GraphQL 文档”(屏幕上显示的错误),
  1. “未捕获的不变违规:需要查询选项。您必须在查询选项中指定您的 GraphQL 文档。” (控制台错误)

这是我的代码的样子:

// gql query inside gqlQueries.js

export const GET_ALL_COUNTRIES = gql`
  query getAllCountry {
    getAllCountry {
      name
      id
      countryCode
      currencyCode
    }
  }
`;

// calling the query

 import { queries as gql } from "./gqlQueries";

 const getAllCountries = () => {
    client
      .query({
        query: gql.GET_ALL_COUNTRIES
      })
      .then((res) => {
        console.log(res.data);
      })
      .catch((err) => console.log(err));
  };

我非常确定我的客户端配置正确,因为我的gqlQueries.js文件中有其他查询,除了这个特定的查询(getAllCountry)之外,它们都可以正常工作。

标签: reactjsgraphqlapolloapollo-clientreact-apollo

解决方案


您是否尝试过直接导入查询?例如


 import { GET_ALL_COUNTRIES } from "./gqlQueries";

 const getAllCountries = () => {
    client
      .query({
        query: GET_ALL_COUNTRIES
      })
      .then((res) => {
        console.log(res.data);
      })
      .catch((err) => console.log(err));
  };

我有一个类似的问题,它是 .query 方法中的语法。不确定您导入它的方式是否会导致问题(尽管如果您对其他查询做了同样的事情,可能不会)。


推荐阅读