Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created May 18, 2019 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmota/42729a28ea5c29af3d3b41654cf033c3 to your computer and use it in GitHub Desktop.
Save miguelmota/42729a28ea5c29af3d3b41654cf033c3 to your computer and use it in GitHub Desktop.
Node GraphQL query example using fetch
const fetch = require('node-fetch')
const query = `
query Echo($message:String) {
message: echo(message:$message)
}
`
const variables = {
message: 'hello'
}
async function main() {
const res = await fetch('http://localhost:4000/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
query,
variables
})
})
const jsonRes = await res.json()
console.log(JSON.stringify(jsonRes, null, 2))
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment