Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 5

Github GraphQL in NodeJS App: A query attribute must be specified and must be a string.

Solved! Go to Solution.

I have two different queries.

 

The first is in a private repo:

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

 

The second is in a public repo:

    const querytwo = `{
      repository(
        owner: "publicOrg"
        name: "publicRepo"
      ) {
        name
        forkCount
        forks(
          first: 11
          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"}]}}}}
The second query returns:
{"errors":[{"message":"A query attribute must be specified and must be a string."}]}
What am I doing wrong? 
4 Replies
Copilot Lvl 2
Message 2 of 5

Re: Github GraphQL in NodeJS App: A query attribute must be specified and must be a string.

hi i need to my develop my api doc to bitcoin and send my personal account .
Solution
Copilot Lvl 2
Message 3 of 5

Re: Github GraphQL in NodeJS App: A query attribute must be specified and must be a string.

The issue was short hand object notation. 

 

 

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}), or in other words it creates this query:

{
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

Ground Controller Lvl 1
Message 4 of 5

Re: Github GraphQL in NodeJS App: A query attribute must be specified and must be a string.

เย็ดแม่กวนตีนแล้วมึง
Ground Controller Lvl 1
Message 5 of 5

Re: Github GraphQL in NodeJS App: A query attribute must be specified and must be a string.

I need help I'm new I'm kind of confuse