reactjs - 未捕获的不变违规:需要查询选项。您必须在查询选项中指定您的 GraphQL 文档
问题描述
我正在尝试从graphql
我的反应应用程序中的服务器获取国家/地区列表。该getAllCountry
查询在操场上运行良好,但每当我在应用程序上调用相同的查询时,我都会收到以下错误:
- “查询选项是必需的。您必须在查询选项中指定您的 GraphQL 文档”(屏幕上显示的错误),
- “未捕获的不变违规:需要查询选项。您必须在查询选项中指定您的 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
)之外,它们都可以正常工作。
解决方案
您是否尝试过直接导入查询?例如
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 方法中的语法。不确定您导入它的方式是否会导致问题(尽管如果您对其他查询做了同样的事情,可能不会)。
推荐阅读
- javascript - Google OAuth2 API 的哪些方法接受 login_hint 作为参数
- .net - 如何从 azure 管道中长时间运行的 webjob 获得响应
- html -
使用 CSS的替代方案 - powerbi - 如何在powerbi中有条件地将测量值转换为短信
- scala - Akka http 构建 Uri
- cassandra - 在Cassandra中根据频繁间隔的时间范围选择记录
- android - 从 WebView 代理中完全删除 Android 作为操作系统
- php - Symfony/Doctrine - 从实体构造函数获取记录?
- javascript - 使用 axios 时可能出现未处理的 Promise 拒绝、网络错误
- javascript - 在 DOM 加载后验证引导模式表单和模式加载