graphql - React w/ Apollo & Drupal nodeQuery - 如何定义/传递变量?
问题描述
将 GraphQL 与 React 和 Drupal 一起使用非常有趣,但很少有文档说明如何让它们一起工作。这个按标签提取文章的 nodeQuery 工作得很好,但我还没有看到任何定义如何将变量传递给使用 gqlClient 定义的 nodeQuery 的示例。我在此查询中放置了变量占位符,但我收到一条错误消息:
Unhandled Rejection (Error): GraphQL error: Variable "$limit" is not defined.
GraphQL error: Variable "$offset" is not defined.
有没有人在这方面取得成功?
import gqlClient from "../gqlClient";
import { gql } from "apollo-boost";
export const articleListByTerm = gqlClient.query({
query: gql`
{
nodeQuery(
limit: $limit
offset: $offset
filter: {
conditions: [
{ operator: EQUAL, field: "type", value: ["article"] }
{ operator: EQUAL, field: "status", value: ["1"] }
{ operator: EQUAL, field: "field_tags.entity.vid", value: ["tags"] }
{
operator: EQUAL
field: "field_tags.entity.name"
value: ["thiphif"]
}
]
}
) {
entities {
entityLabel
entityBundle
... on NodeArticle {
body {
value
}
fieldImage {
url
}
queryFieldTags {
entities {
entityId
entityLabel
}
}
}
}
}
}
`,
});
这是查询的调用方式。您可以看到我正在尝试传递变量,希望它们能正常工作但没有运气。
articleListByTerm({
variables: { offset: 0, limit: 10 },
}).then(({ data }) =>
dispatch(receivePostsByTerm(data.nodeQuery.entities))
);
解决方案
好吧 - 我找到了答案:
import gqlClient from "../gqlClient";
import { gql } from "apollo-boost";
let limit = 10;
let offset = 0;
let type = "article";
export const articleListByTerm = gqlClient.query({
variables: { limit: limit, offset: offset, type: type },
query: gql`
query($limit: Int!, $offset: Int!, $type: String!) {
nodeQuery(
limit: $limit
offset: $offset
filter: { .... etc ...
我希望这可以帮助别人。绝对对我有帮助。我添加了一个字符串!示例的变量也可以提供帮助。IDK,如果你可以做一个数组。这是一个相当简单的解决方案,只是它没有记录在任何一个地方。我扯了一些零碎的猜测,终于弄明白了。
推荐阅读
- java - AmazonDynamoDBv2 查询无法识别 GSI
- postgresql - 如何在 PostgreSQL 中为表创建同义词
- c++ - 这是什么侧音设置?
- java - 错误soapResponse异常SpringBusFactory
- azure - AppServices 不允许在设置名称中使用“:”,但 linux env var 需要
- r - 用另一列中的值替换(gsub)一列中的所有行?
- javascript - 带有插件的 IE11 上的 QuickBlox 应用程序?
- python - 如何通过分解单应矩阵获得更准确的旋转
- javascript - 增加数组数量而不重置它(Javascript/Canvas)
- c# - 在 Microsoft Bot framework V4 中完成子对话框时,如何继续父对话框?