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

grafico-ql

v3.0.7

Published

A minimal graphql client.

Downloads

31

Readme

GraficoQL

Minimal GraphQL client with UMD-support, written in plain javascript (ES5). Inspired by graphql-request (with almost identical interface).

Migration of version 2 to 3

See changes in Version 3 here

Features and limitations

  • Very simple and lightweight GraphQL client (~ 1.2 kB gzipped)
  • No dependencies
  • AMD-, CommonJS and global-support (Universal Module Definition)
  • Promise-based API (works with async / await)
  • Supports HTTP methods POST and GET
  • You have to provide polyfills for fetch and Promise to use it in IE9+


 

Install

npm install grafico-ql


 

Quickstart

Send a GraphQL query with few lines of code.

<script src="node_modules/grafico-ql/dist/grafico-ql.min.js"></script>
<script>
  var query = '{country(code:"IT") {name}}';

  GraficoQL.request('https://countries.trevorblades.com', query)
    .then(function (data) { console.log(data); });
  
</script>


 

Usage

// Run GraphQL queries/mutations using a static function
GraficoQL.request(endpoint, query, variables)
  .then(function (data) { console.log(data); });

// ... or create a GraphQL client instance to send requests
var client = GraficoQL.create(endpoint, { headers: {} });
client.request(query, variables).then(function (data) { console.log(data); });

API documentation

API of GraficoQL


 


Examples

Use method GET instead of POST

Additional options to the fetch-call, can only defined during instantiating a GraphQL-client. So, if you want to use the method GET instead of POST, you have to create a client:

await GraficoQL.create(endpoint, {method: "GET"}).request(query);


 

Authentication via HTTP header

Again, for passing additional options to fetch, you have to create a client:

<script src="node_modules/requirejs/require.js"></script>
<script>
  require.config({baseUrl: 'node_modules'});

  require(['grafico-ql/dist/grafico-ql.min'], function (GQL) {
    var endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr';
    var query = '{Movie(title: "Inception") {releaseDate, actors {name}}}';

    var graphQLClient = GQL.create(endpoint, {
      headers: {
        authorization: 'Bearer MY_TOKEN'
      }
    });

    graphQLClient.request(query)
      .then(function (data) {
        console.log(JSON.stringify(data, undefined, 2));
      })
      .catch(function (error) {
        console.error(error);
      });
  });
</script>


 

Passing more options to fetch ...

<script src="node_modules/requirejs/require.js"></script>
<script>
  require.config({baseUrl: 'node_modules'});

  require(['grafico-ql/dist/grafico-ql.min'], function (GQL) {
    main(GQL).catch(error => console.error(error));
  });

  async function main(GraphQLClient) {
    const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
    const query = /* GraphQL */ `
      {
        Movie(title: "Inception") {
          releaseDate
          actors {
            name
          }
        }
      }
    `

    const graphQLClient = GraphQLClient.create(endpoint, {
      credentials: 'include',
      mode: 'cors',
    })

    const data = await graphQLClient.request(query)
    console.log(JSON.stringify(data, undefined, 2))
  }
</script>


 

Using variables

<script src="node_modules/requirejs/require.js"></script>
<script>
  require.config({baseUrl: 'node_modules'});

  require(['grafico-ql/dist/grafico-ql.min'], function (GQL) {
    var endpoint = 'https://countries.trevorblades.com';
    var variables = {
      cc: 'IT'
    };
    var query = 'query getCountry($cc: ID!) {'
      + 'country(code:$cc) {name}'
      + '}';

    GQL.request(endpoint, query, variables)
      .then(function (data) {
        console.log(JSON.stringify(data, undefined, 2));
      })
      .catch(function (error) {
        console.error(error);
      });
  });
</script>


 

Error handling

For better debugging purposes, in case the status code of the response has not the value 200 (OK), the promise will be rejected and the resulting data will be in format: {response: {...}, request: {...}}.

<script src="node_modules/grafico-ql/dist/grafico-ql.min.js"></script>
<script>
  var endpoint = 'https://countries.trevorblades.com'
  var query = /* GraphQL */ '\
    {\
      country(code: "IT") {\
        name\
        notAvailableField\
      }\
    }\
  ';

  GraficoQL.request(endpoint, query)
    .then(function (data) {
        console.log(data);
        return data;
    })
    .catch(function (err) {
        console.error(err);
        return Promise.reject(err)
    });
</script>


 

Receiving a raw response

The request method will return the data, errors and/or extensions key from the response. If you need to access any additional (non-standard) keys, you can use the rawRequest method:

import { rawRequest } from 'grafico-ql'

async function main() {
  const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

  const query = /* GraphQL */ `
    {
      Movie(title: "Inception") {
        releaseDate
        actors {
          name
        }
      }
    }
  `

  const { data, errors, extensions, headers, status } = await rawRequest(
    endpoint,
    query
  )
  console.log(
    JSON.stringify({ data, errors, extensions, headers, status }, undefined, 2)
  )
}

main().catch(error => console.error(error))


 


Development

If you want to distribute your changes in the 'dist'-directory, you can use npm:

    $ npm run build


 

License

The MIT License (MIT)

Copyright (c) 2018 Daniel Moritz

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.