【发布时间】:2020-09-25 01:18:35
【问题描述】:
将 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))
);
【问题讨论】:
-
阅读有关 graphql 传递变量的信息 - 查询语法很重要
标签: graphql drupal-8 react-apollo apollo-client