Created
May 18, 2019 07:25
-
-
Save miguelmota/42729a28ea5c29af3d3b41654cf033c3 to your computer and use it in GitHub Desktop.
Node GraphQL query example using fetch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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