Search code examples
node.jsgraphqlgithub-api

Is any library of nodejs work with GitHub API v4 exist?


Thank you guys cause reading my question. The title is exactly what i wanna known. Hope it do not cost your time much.


Solution

  • Some of the most common graphql client are graphql.js and apollo-client. You can also use the popular request module. The graphql API is a single POST endpoint on https://api.github.com/graphql with a JSON body consisting of the query fields and the variables fields (if you have variables in the query)

    Using graphql.js

    const graphql = require('graphql.js');
    
    var graph = graphql("https://api.github.com/graphql", {
      headers: {
        "Authorization": "Bearer <Your Token>",
        'User-Agent': 'My Application'
      },
      asJSON: true
    });
    
    graph(`
        query repo($name: String!, $owner: String!){
            repository(name:$name, owner:$owner){
                createdAt      
            }
        }
    `)({
      name: "linux",
      owner: "torvalds"
    }).then(function(response) {
      console.log(JSON.stringify(response, null, 2));
    }).catch(function(error) {
      console.log(error);
    });
    

    Using apollo-client

    fetch = require('node-fetch');
    const ApolloClient = require('apollo-client').ApolloClient;
    const HttpLink = require('apollo-link-http').HttpLink;
    const setContext = require('apollo-link-context').setContext;
    const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache;
    const gql = require('graphql-tag');
    
    const token = "<Your Token>";
    
    const authLink = setContext((_, {
        headers
    }) => {
        return {
            headers: {
                ...headers,
                authorization: token ? `Bearer ${token}` : null,
            }
        }
    });
    
    const client = new ApolloClient({
        link: authLink.concat(new HttpLink({
            uri: 'https://api.github.com/graphql'
        })),
        cache: new InMemoryCache()
    });
    
    client.query({
            query: gql `
        query repo($name: String!, $owner: String!){
            repository(name:$name, owner:$owner){
                createdAt      
            }
        }
      `,
            variables: {
                name: "linux",
                owner: "torvalds"
            }
        })
        .then(resp => console.log(JSON.stringify(resp.data, null, 2)))
        .catch(error => console.error(error));
    

    Using request

    const request = require('request');
    
    request({
        method: 'post',
        body: {
            query: `
        query repo($name: String!, $owner: String!){
            repository(name:$name, owner:$owner){
                createdAt      
            }
        } `,
            variables: {
                name: "linux",
                owner: "torvalds"
            }
        },
        json: true,
        url: 'https://api.github.com/graphql',
        headers: {
            Authorization: 'Bearer <Your Token>',
            'User-Agent': 'My Application'
        }
    }, function(error, response, body) {
        if (error) {
            console.error(error);
            throw error;
        }
        console.log(JSON.stringify(body, null, 2));
    });