fetch-graph

1.0.3 • Public • Published

Simple implementation of "fetch" for GraphQL API. Module use vanilla javaScript 'XMLHttpRequest', so it will work everywhere.

No polyfills, No dependencies, just 588 Bytes simple function.

Usage pretty similar to standard "fetch".

Install

npm i -S fetch-graph

Description

import fetchGraph from 'fetch-graph';
 
fetchGraph(path, request[, variables])
// returns Promise
// path - your GraphQL API address
// request - string of same structure query as in GraphiQL tool
// variables - same logic as in GraphiQL tool

Example

import fetchGraph from 'fetch-graph';
 
let request = `query getCategory($id: String!) {
                    getCategory(id: $id) {
                        id
                        name
                    }    
               }`;
              
variables = {id: '12345'};
 
fetchGraph('/graphql', request, variables)
            .then(res => console.log(res))
            .catch(err => console.log(`${request} error: ${err});
            
/* result
{
    data: {
        getCategory: [{itemObj}]
    }
}

License

MIT

Package Sidebar

Install

npm i fetch-graph

Weekly Downloads

1

Version

1.0.3

License

MIT

Last publish

Collaborators

  • sarkistlt