【问题标题】:React w/ Apollo & Drupal nodeQuery - how to define/pass variables?React w/ Apollo & Drupal nodeQuery - 如何定义/传递变量?
【发布时间】: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


【解决方案1】:

好吧 - 我找到了答案:

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,如果你可以做一个数组。这是一个相当简单的解决方案,只是它没有记录在任何一个地方。我点点滴滴猜测,终于弄明白了。

【讨论】:

    猜你喜欢
    • 2020-06-21
    • 2019-01-26
    • 2019-10-03
    • 2021-06-09
    • 2018-10-09
    • 2018-01-27
    • 2023-03-15
    • 2018-06-27
    • 2020-09-05
    相关资源
    最近更新 更多