Using Node to call a GraphQL API – ITNEXT

March 29, 2018 0 Comments

Using Node to call a GraphQL API – ITNEXT

 

 



  • , $cursor: String) { – repository(owner: $owner, name: $name) { – stargazers(first: 100, after: $cursor) { – …starGazerFields – } – } – }I am not going to cover the details of the GraphQL Language or how to build queries, however the following articles can provide some insight if you…

  • We need to start by building a function that loads GraphQL files from disk – const { readFile } = require(‘fs’) – const { resolve } = require(‘path’) – – const loadQuery = (fileName) = new Promise((res, reject) = { – readFile(resolve(fileName), ‘utf-8’, (err, data) = { – (err) ?…

  • I created two different functions for this depending on wether you need to pass variables to the query: – const buildBody = (query) = Promise.resolve({ – body: buildBodyWithVariables = (variables) = (query) = Promise.resolve({ – body: JSON.stringify({query, variables}) – })buildHeadersOnce we have a stringified body we can build our header…

  • const buildRequestOptions = ({headers, body}) = Promise.resolve({ – body, – options: { – method: ‘POST’, – host: ‘api.github.com’, – path: ‘/graphql’, – port: 443, – headers – } – })Observations: I have used object destruction for the function parameters.

  • const { request } = require(‘https’) – – const makeRequest = ({options, body}) = new Promise((resolve, reject) = { – request(options, (res) = { – let result = ” – res.on(‘data’, (chunk) = result += chunk) – res.on(‘end’, () = resolve(JSON.parse(result))) – res.on(‘error’, (err) = reject(err)) – }).


Making a call to a GraphQL API server can seem complicated at first glance. There are a few good options like Apollo Fetch. However, if you are building a Node application and want a simple solution…

Using Node to call a GraphQL APIWith native modulesClick here to share this article on LinkedIn »

Making a call to a GraphQL API server can seem complicated at first glance. There are a few good options like Apollo Fetch. However, if you are building a Node application and want a simple solution that uses native node modules, here is your solution. At Credit Karma we were looking to use the GitHub GraphQL API to search for certain aspects of our repositories, so I developed a simple client and thought I would share what I discovered.

So before setting out to build the application, I established the following goals:

It should work without any dependenciesShould be able to load queries written as GraphQL Schema LanguageThe queries should be able to use variables passed from the command lineSupport fragments being used in multiple queriesSupport pagination by calling the same query multiple timesBuilt with pure this example we are going to call the GitHub GraphQL API to get a list of Stargazers for a repository. The main query we will be sending:

query StarGazers($owner: String!, $name: String!, $cursor: String) {

repository(owner: $owner, name: $name) {

stargazers(first: 100, after: $cursor) {

…starGazerFields

}

}

}I am not going to cover the details of the GraphQL Language or how to build queries, however the following articles can provide some insight if you are new to GraphQL:

Using Node to call a GraphQL API – ITNEXT

Comments are closed, but trackbacks and pingbacks are open.


Tag cloud