1.3.0 • Published 5 years ago

graphql-api-client v1.3.0

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

GraphQL API client

This module is a simple graphql API client supported query building.

Sneaky-peaky code

Here is quick example how it works

import GraphqlCLient from 'graphql-api-client';
// Your api schema
import schema from '../schema.json';

// Args are optional, here is default values:
let client = new GraphqlCLient({
    apiUrl = 'graphql',
    wsApiUrl = null,
    wsStreamName = 'graphql',
    reqParams = {},
    schemaUrl = '/schema.json',
    baseQueryName = 'Query',
    baseMutationName = 'Mutation',
    onlyQueries = null,
    onlyMutations = null,
    synced = [],
    onData = () => { },
    onError = () => { },
    vaerbose = false
})

// Make a query
client.query('login', {
   username: 'some@email.here',
   password: 'password here'
}).then(data => {
   let isLoggedIn = data.login;
   // Some logic
});


// Perform a mutation
client.mutate('changeProfile', {
   data: {
      firstName: 'John',
      lastName: 'White',
   }
}).then(data => {
   let newProfileData = data.changeProfile;
});

Setting up

Contructor

Contructor accepts connection params:

  • apiUrl - default: /graphql
  • reqParams - requests parameters such as headers, mode and so on - default: {}
  • schemaUrl - valid graphql schema url to be fetched from - default: /schema.json
  • baseQueryName - parent query name containing all queries as fields - default: Query
  • baseMutationName - parent query name containing all mutations as fields - default: Mutation
  • onlyQueries - list of query names you need to be used only, if null is passed all parent query fields will be used - default: null
  • onlyMutations - list of mutations names you need to be used only, if null is passed all parent mutation fields will be used - default: null
  • synced - list of query names that take no parameters to be synced just after creating the client - default: []
  • onData - function called when any data is recieved from server - default: () => { }
  • onError - function called if an error returned from server - default: () => { }

If you want to implement some kind of storage - onData is what you need, all data goes through it.

TODO: make synced items to update dynamically through websockets

Query

To make a query call

client.query('queryName', {
   param1: data
})

Returns a Promise resolving single object containing keys whose names equal to queryName

Mutation

To perform a mutation call

client.mutate('mutationName', {
   param1: data
})

Returns a Promise resolving single object containing keys whose names equal to mutationName

Notes

Also supports file uploads, just set a variable to file instance. Backend should handle multipart/farm-data requests for graphql this way. See graphql-multipart-request-spec for more info.

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

6 years ago

0.7.5

6 years ago

0.7.4

6 years ago

0.7.2

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.5

6 years ago

0.6.4

6 years ago

0.6.3

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.14

6 years ago

0.4.13

6 years ago

0.4.12

6 years ago

0.4.11

6 years ago

0.4.10

6 years ago

0.4.9

6 years ago

0.4.8

6 years ago

0.4.7

6 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago