Graphql 与阿波罗公司合作;Drupal nodeQuery-如何定义/传递变量?
将GraphQL与React和Drupal一起使用是非常有趣的,但是关于如何使它们协同工作的文档很少。这个按标记拉取文章的nodeQuery工作得很好,但我还没有看到任何示例定义如何将变量传递给使用gqlClient定义的nodeQuery。我在此查询中放置了变量占位符,但出现了一个错误,错误是:Graphql 与阿波罗公司合作;Drupal nodeQuery-如何定义/传递变量?,graphql,drupal-8,react-apollo,apollo-client,Graphql,Drupal 8,React Apollo,Apollo Client,将GraphQL与React和Drupal一起使用是非常有趣的,但是关于如何使它们协同工作的文档很少。这个按标记拉取文章的nodeQuery工作得很好,但我还没有看到任何示例定义如何将变量传递给使用gqlClient定义的nodeQuery。我在此查询中放置了变量占位符,但出现了一个错误,错误是: Unhandled Rejection (Error): GraphQL error: Variable "$limit" is not defined. GraphQL error: Variabl
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,如果你能做一个数组。这是一个相当直截了当的解决方案,只是它没有在任何地方记录在案。我想来想去,终于猜出了答案。阅读关于graphql传递变量-查询语法的内容