Feature light client library for fetching resources via GraphQL
$ yarn install graphql-js-client
GraphQLClient
requires a "type bundle" which is a set of ES6 modules generated by graphql-js-schema
that represent your GraphQL schema.
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
});
$ git clone git@github.com:Shopify/graphql-js-client.git
$ cd graphql-js-client
$ yarn install
$ yarn start
Then visit http://localhost:4200
$ yarn test
MIT, see LICENSE.md for details.