Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 5 - Client Dev: JavaScript - example doesn't work #14

Closed
ivansvlv opened this issue Sep 10, 2018 · 1 comment
Closed

Chapter 5 - Client Dev: JavaScript - example doesn't work #14

ivansvlv opened this issue Sep 10, 2018 · 1 comment

Comments

@ivansvlv
Copy link

Hi, this example doesn't work:

const makeGraphqlRequest = async ({ endpoint, document }) => {
  const options = {
    method: 'POST',
    headers: new Headers({
      'Content-Type': 'application/json'
    }),
    body: JSON.stringify({
      query: document
    })
  }

  const response = await fetch(endpoint, options);
  return response.json()
}

const result = await makeGraphqlRequest({
  endpoint: 'https://api.graphql.guide/graphql',
  document: '{ githubStars }'
});

console.log(result);

await can't be used outside of the async, I guess it should be changed to the code you have in jsbin:

const makeGraphqlRequest = async ({ endpoint, document }) => {
  const options = {
    method: 'POST',
    headers: new Headers({ 
      'Content-Type': 'application/json' 
    }),
    body: JSON.stringify({
      query: document
    })
  }

  const response = await fetch(endpoint, options)
  return response.json()
}

async function fetchExample() {
  const document = '{ githubStars }'

  console.log('Sending query: ' + document)
  const result = await makeGraphqlRequest({
    endpoint: 'https://api.graphql.guide/graphql', 
    document
  })
  console.log('Response:', result)
}

fetchExample()
@lorensr
Copy link
Member

lorensr commented Sep 16, 2018

Thanks Ivan! Fixed, and will be included in r3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants