0.4.1 • Published 8 years ago
@shopify/graphql-js-client v0.4.1
graphql-js-client
Feature light client library for fetching resources via GraphQL
Table Of Contents
Installation
$ yarn 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.
Creating and sending a query
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 ${btoa('some-storefront-access-token')}`
}
});
const products = [];
client.send(client.query((root) => {
root.add('shop', (shop) => {
shop.add('name');
shop.addConnection('products', {args: {first: 10}}, (product) => {
product.add('title');
});
});
}).then(({model, data}) => {
console.log(model); // The serialized model with rich features
console.log(data); // The raw data returned from the endpoint
products.push(...model.products);
if (model.products.hasNextPage) {
return client.fetchNextPage(model.products);
};
// `fetchNextPage` resolves with the model you wanted the next page of
}).then(({model, data}) => {
products.push(...model); // Page two of products
});
Contributing
Setting up:
$ git clone git@github.com: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.