Skip to content

Instantly share code, notes, and snippets.

@rajinwonderland
Created January 11, 2021 21:56
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 rajinwonderland/9cea9b8917d69779d7b79fc1c1171985 to your computer and use it in GitHub Desktop.
Save rajinwonderland/9cea9b8917d69779d7b79fc1c1171985 to your computer and use it in GitHub Desktop.
Using node's http to make a vanilla request to a graphql server
const https = require('https')
const query = `
query GetCurrencyInfo($CODE: [String!]) {
currencies(currencyCodes: $CODE) {
name
code
decimalDigits
numericCode
active
}
}
`
// your query above ^
const variables = {
CODE: "USD"
}
// your variable above ^
const data = JSON.stringify({
query,
variables
})
const options = {
hostname: 'swop.cx', // your graphql endpoint here
path: '/graphql',
method: 'POST',
headers: {
"Authorization": "ApiKey <YOUR-KEY-HERE>", // for authenticating
'Content-Type': 'application/json',
}
}
const req = https.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log('Response', chunk); // your graphql query response
console.log('Parsed Response', JSON.parse(chunk)); // parsed graphql query response for display purposes only
return chunk;
});
})
req.on('error', (error) => {
console.error(error)
})
req.write(data)
req.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment