JSPM

  • Created
  • Published
  • Downloads 141
  • Score
    100M100P100Q76931F

GraphQL Network Communication Framework (Client)

Package Exports

  • graphql-io-client
  • graphql-io-client/lib/browser/graphql-io.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (graphql-io-client) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

GraphQL-IO-Meta  |  GraphQL-IO-Client  |  GraphQL-IO-Server

GraphQL-IO-Client

GraphQL Network Communication Framework (Client)

About

This is a GraphQL network communication framework for JavaScript clients, running under either Node.js or in the Browser. It is based on the GraphQL engine GraphQL.js, the GraphQL client library Apollo Client, its WebSocket network interface Apollo Client WS and the HTTP client library Axios. It has be used with the corresponding GraphQL-IO-Server network communication framework on the JavaScript server side.

Installation

$ npm install graphql-io-client

Usage

Simple "Hello World":

const { Client } = require("graphql-io-client")
;(async () => {
    const sv = new Client({ url: "http://127.0.0.1:12345/api" })
    sv.on("debug", ({ log }) => console.log(log))
    await sv.connect()
    let result = await sv.query("{ hello }")
    console.log(result.data)
    await sv.disconnect()
})()

Complex Example:

/*  external requirements  */
const { inspect } = require("util")
const { Client }  = require("graphql-io-client")

/*  helper function for dumping data structure  */
const dump = (data) => {
    if (process.browser)
        data = JSON.stringify(data, null, "    ")
    else
        data = inspect(data, { colors: true, depth: null })
    return data
}

/*  create a GraphQL-IO client service instance  */
const newService = async (id) => {
    const sv = new Client({
        url:      "http://127.0.0.1:12345/api",
        path:     { graph: "" },
        encoding: "cbor",
        debug:    1
    })
    sv.on("debug", ({ log }) => {
        console.error(`${id}: ${log}`)
    })
    await sv.connect()
    return sv
}

/*  execute asynchronous function in a wrapping procedure  */
;(async () => {
    /*  create two client service instances  */
    const sv1 = await newService("sv1")
    const sv2 = await newService("sv2")

    /*  the first service continuously queries...  */
    let subscription = sv1.query(`subscription {
        OrgUnits {
            id
            name
            director   { id name }
            parentUnit { id name }
            members    { id name }
        }
    }`).subscribe((response) => {
        console.log(`sv1: response: ${dump(response)}`)
    }, (err) => {
        console.log(`sv1: ERROR: ${err}`)
    })

    /*  the second service manipulates multiple times...  */
    let cnt = 0
    let timer = setInterval(async () => {
        await sv2.query(`mutation ($with: JSON!) {
            OrgUnit (id: "XT") {
                update(with: $with) {
                    name
                }
            }
        }`, {
            with: {
                name: `dummy${cnt}`
            }
        }).then((response) => {
            console.log(`sv2: response: ${dump(response)}`)
        }, (err) => {
            console.log(`sv2: ERROR: ${err}`)
        })
        if (cnt++ >= 2) {
            /*  stop processing  */
            clearTimeout(timer)
            setTimeout(async () => {
                await subscription.unsubscribe()
                await sv1.disconnect()
                if (!process.browser)
                    process.exit(0)
            }, 1 * 1000)
        }
    }, 1 * 1000)
})().catch((err) => {
    console.log(`global: ERROR: ${err}`)
})

Application Programming Interface (API)

See the TypeScript type definition of the GraphQL-IO-Client API for details.

License

Copyright (c) 2016-2017 Ralf S. Engelschall (http://engelschall.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.