Created
January 11, 2021 21:56
-
-
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
This file contains hidden or 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 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