npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-api-client

v1.3.0

Published

Advanced graphql client aimed to simplify client-server communication using graphql schema.

Downloads

119

Readme

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: '[email protected]',
   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.