Home

Awesome

Minimal graphql npm

Minimal graphql is a minimal node graphql client, it is built using the awesome apollo library, but wires it up so that you can use it in node instead of the browser.

Usage

Initializing

The only libraries you will need are minimal-graphql (guess what) and graphql-tag (parses you graphql documents). To create a client

const minimalGraphql = require("minimal-graphql")
const client = minimalGraphql(httpOptions, subscriptionOptions)

Queries

Once you created a client you can use it's prop query to run a query. The client.query(opts) function returns a Promise and takes an option object as input, it has this props:

const gql = require("graphql-tag")

// create the client here

client
    .query({
        query: gql`
            {
                user {
                    email
                }
            }
        `,
    })
    .then(res => console.log(res.data))
const gql = require("graphql-tag")

// create the client here

client
    .query({
        query: gql`
            query userQuery($id: ID!) {
                user(id: $id) {
                    email
                }
            }
        `,
        variables: {
            id: "myId",
        },
    })
    .then(res => console.log(res.data))

Mutations

To run a mutation use the client.mutation(opts) function. It works as the client.query(opts) function, but instead of passing a query parameter in options you pass a mutation parameter E.g.

client
    .mutate({
        mutation: gql`mutation logIn($email:String, password:String){
        authenticateUser(email:$email, password:$password){
            token
        }
    }`,
        variables: {
            email: "99.zanin@gmail.com",
            password: "password",
        },
    })
    .then(res => console.log(res.data))

Subscriptions

Finally you can use subscriptions through client.subscribe(opts). The options are the same as for client.query(opts), but instead of returning a Promise it returns a ZenObservable.
The ZenObservable has a observable.subscribe(handlers) prop that allows you to handle the subscription results. The handlers object has to have the following props:

E.g.

const gql = require("graphql-tag")

client
    .subscribe({
        query: gql`
            subscription {
                deviceCreated {
                    id
                    customName
                }
            }
        `,
    })
    .subscribe({
        next: console.log,
        err: console.log,
    })

Contributing

This package is hosted here, feel free to contribute or ask for any new feature ;)