maddie
maddie

Reputation: 1954

Github GraphQL Error: A query attribute must be specified and must be a string

I have two different queries.

The first is in a private repo:

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

The second is in a public repo. I tested it on Explorer before putting it in my nodeJS app - it works on explorer:

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

The fetch for both look identical except the queries:

  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");

The first query returns: 

{"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"}]}}}}

But the second query returns an error:

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

Why would that be?

I've tried changing the second, faulty query to what I've seen in curl calls:

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

But I get the same error

Upvotes: 3

Views: 1912

Answers (1)

maddie
maddie

Reputation: 1954

Shorthand object notation mistake

JSON.stringify({query})

is shorthand for JSON.stringify({query: query})

which becomes

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

JSON.stringify({querytwo}) is shorthand for JSON.stringify({querytwo: querytwo})

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

Hence why GraphQL couldn't find a query - it found a queryTwo

Upvotes: 3

Related Questions