Giter Club home page Giter Club logo

graphql-js-client's Introduction

Travis

graphql-js-client

Feature light client library for fetching resources via GraphQL.

Table Of Contents

Installation

With Yarn:

$ yarn add graphql-js-client

With NPM:

$ npm install graphql-js-client

Examples

GraphQLClient requires a "type bundle" which is a set of ES6 modules generated by graphql-js-schema that represent your GraphQL schema.

Initialization

import GraphQLClient from 'graphql-js-client';

// This is the generated type bundle from graphql-js-schema
import types from './types.js';

const client = new GraphQLClient(types, {
  url: 'https://graphql.myshopify.com/api/graphql',
  fetcherOptions: {
    headers: { Authorization: 'Basic aGV5LXRoZXJlLWZyZWluZCA=' }
  }
});

Creating and sending a query

const query = client.query((root) => {
  root.add('shop', (shop) => {
    shop.add('name');
    shop.addConnection('products', {args: {first: 10}}, (product) => {
      product.add('title');
    });
  });
});

/* Will generate the following query:

  query {
    shop {
      name
      products (first: 10) {
        pageInfo {
          hasNextPage
          hasPreviousPage
        }
        edges {
          cursor
          node {
            id
            title
          }
        }
      }
    }
  }

Note: things that implement Node will automatically have the id added to the
query. `addConnection` will automatically build out the connection query with
all information necessary for pagination.

*/

let objects;

client.send(query).then(({model, data}) => {
  objects = model;
  console.log(model); // The serialized model with rich features
  console.log(data); // The raw data returned from the API endpoint
});

Refetching Nodes

client.refetch(objects.shop.products[0]).then((product) => {
  console.log(product); // This is a fresh instance of the product[0]
});

In the above example, objects.shop.products[0] implements the Node interface. The client is smart enough to understand this, and generate the following query:

query {
  node (id: 'abc123') {
    __typename
    ... on Product {
      id
      title
    }
  }
}

It then resolves directly with the node field, which is a product in this case.

Pagination

const query = client.query((root) => {
  root.add('shop', (shop) => {
    shop.add('name');
    shop.addConnection('products', {args: {first: 10}}, (product) => {
      product.add('title');
      product.addConnection('variants', {args: {first: 50}}, (variant) => {
        variant.add('title');
        variant.add('price');
      });
    });
  });
});

client.send(query).then(({model}) => {
  client.fetchNextPage(model.shop.products).then((products) => {
    console.log(products); // resolves with the next 10 products.
  });

  client.fetchNextPage(model.shop.products[0].variants).then((variants) => {
    console.log(variants); // resolves with the next 50 variants. Page size is
                           // taken from the previous `first` argument.
  });
});

The client understands the Relay specification, and will send the following query for the call client.fetchNextPage(model.shop.products):

query {
  shop {
    name
    products (first: 10, after: 'abc123') {
      pageInfo {
        hasNextPage
        hasPreviousPage
      }
      edges {
        cursor
        node {
          id
          title
          variants (first: 50) {
            pageInfo {
              hasNextPage
              hasPreviousPage
            }
            edges {
              cursor
              node {
                id
                title
                price
              }
            }
          }
        }
      }
    }
  }
}

The client can also use the Node interface to optimize queries for nested paginated sets. fetchNextPage in the second case client.fetchNextPage(model.shop.products[0].variants) will generate the following query:

query {
  node (id: '1') {
    __typename
    ... on Product {
      id
      variants (first: 50, after: 'abc123') {
        id
        title
        price
      }
    }
  }
}

In both cases, fetchNextPage resolves with the models you're paginating, since the object graph to those models may not be obvious due to the query generation algorithm. Page size and fields on the paginated object are retained, while fields not in the paginated set are pruned.

Directives

const query = client.query((root) => {
  root.add('shop', (shop) => {
    shop.add('name');
    shop.addConnection('products', {args: {first: 10}}, (product) => {
      product.add('title', {directives: {include: {if: true}}});
    });
  });
});

/* Will generate the following query:

  query {
    shop {
      name
      products (first: 10) {
        pageInfo {
          hasNextPage
          hasPreviousPage
        }
        edges {
          cursor
          node {
            id
            title @include(if: true)
          }
        }
      }
    }
  }

*/

Documentation

For full API documentation, check out the API docs.

Contributing

Setting up:

$ git clone [email protected]:Shopify/graphql-js-client.git
$ cd graphql-js-client
$ yarn install

Running the tests in a browser

$ yarn start

Then visit http://localhost:4200

Running the tests in node

$ yarn test

License

MIT, see LICENSE.md for details.

graphql-js-client's People

Contributors

minasmart avatar jamesmacaulay avatar mikkoh avatar jzjiang avatar swalkinshaw avatar lyninx avatar camerondubas avatar melissaluu avatar rebeccajfriedman avatar shopify-services avatar zudochkin avatar lreeves avatar roperzh avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.