【问题标题】:Github GraphQL Error: A query attribute must be specified and must be a stringGithub GraphQL 错误:必须指定查询属性并且必须是字符串
【发布时间】:2019-11-22 05:17:44
【问题描述】:

我有两个不同的查询。

第一个是在私人仓库中:

const query = `{
  repository(
    owner: "PrivateOrg"
    name: "privateRepoName"
  ) {
    name
    forkCount
    forks(
      first: 11
      orderBy: { field: NAME, direction: DESC }
    ) {
      totalCount
      nodes {
        name
      }
    }
  }
}`

第二个在公共回购中。我在 Explorer 上对其进行了测试,然后将其放入我的 nodeJS 应用程序 - 它适用于资源管理器

const querytwo = `{
  repository(
    owner: "mongodb"
    name: "docs-bi-connector"
  ) {
    name
    forkCount
    forks(
      first: 27
      orderBy: { field: NAME, direction: DESC }
    ) {
      totalCount
      nodes {
        name
      }
    }
  }
}`

除了查询之外,两者的提取看起来相同:

  fetch('https://api.github.com/graphql', {
  method: 'POST',
  body: JSON.stringify({query}),
  headers: {
    'Authorization': `Bearer ${accessToken}`,
  },
}).then(res => res.text())
  .then(body => console.log(body))
  .catch(error => console.error(error));
  console.log("\n\n\n");

    fetch('https://api.github.com/graphql', {
      method: 'POST',
      body: JSON.stringify({querytwo}),
      headers: {
        'Authorization': `Bearer ${accessToken}`,
      },
    }).then(res => res.text())
      .then(body => console.log(body))
      .catch(error => console.error(error));
    console.log("\n\n\n");

第一个查询返回:

{"data":{"repository":{"name":"mms-docs","forkCount":26,"forks":{"totalCount":8,"nodes":[{"name":"mms-docs"},{"name":"mms-docs"},{"name":"mms-docs"},{"name":"mms-docs"},{"name":"mms-docs"},{"name":"mms-docs"},{"name":"mms-docs"},{"name":"mms-docs"}]}}}}

但是第二个查询返回错误:

{"errors":[{"message":"A query attribute must be specified and must be a string."}]}

为什么会这样?

我尝试将第二个错误查询更改为我在 curl 调用中看到的内容:

const querytwo = `query: {
      repository(
        owner: "mongodb"
        name: "docs-bi-connector"
      ) {
        name
        forkCount
        forks(
          first: 27
          orderBy: { field: NAME, direction: DESC }
        ) {
          totalCount
          nodes {
            name
          }
        }
      }
    }`;

但我得到了同样的错误

【问题讨论】:

    标签: github graphql


    【解决方案1】:

    简写对象符号错误

    JSON.stringify({query})
    

    JSON.stringify({query: query})的简写

    变成了

    {
    query: 
     {
          repository(
            owner: "PrivateOrg"
            name: "privateRepoName"
          ) {
            name
            forkCount
            forks(
              first: 11
              orderBy: { field: NAME, direction: DESC }
            ) {
              totalCount
              nodes {
                name
              }
            }
          }
        }
       }`
    

    JSON.stringify({querytwo})JSON.stringify({querytwo: querytwo}) 的简写

    {
    querytwo: 
     {
          repository(
            owner: "PrivateOrg"
            name: "privateRepoName"
          ) {
            name
            forkCount
            forks(
              first: 11
              orderBy: { field: NAME, direction: DESC }
            ) {
              totalCount
              nodes {
                name
              }
            }
          }
        }
       }`
    

    因此 GraphQL 找不到 query - 它找到了 queryTwo

    【讨论】:

      猜你喜欢
      • 2020-01-26
      • 2018-01-13
      • 2023-03-30
      • 2018-12-30
      • 1970-01-01
      • 2019-10-15
      • 2019-06-02
      • 2019-06-09
      • 1970-01-01
      相关资源
      最近更新 更多