Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.