在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Shopify/graphql-js-client开源软件地址(OpenSource Url):https://github.com/Shopify/graphql-js-client开源编程语言(OpenSource Language):JavaScript 99.6%开源软件介绍(OpenSource Introduction):graphql-js-clientFeature light client library for fetching resources via GraphQL. Table Of ContentsInstallationWith Yarn: $ yarn add graphql-js-client With NPM: $ npm install graphql-js-client Examples
Initializationimport 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 queryconst 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 Nodesclient.refetch(objects.shop.products[0]).then((product) => {
console.log(product); // This is a fresh instance of the product[0]
}); In the above example, query {
node (id: 'abc123') {
__typename
... on Product {
id
title
}
}
} It then resolves directly with the Paginationconst 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
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. query {
node (id: '1') {
__typename
... on Product {
id
variants (first: 50, after: 'abc123') {
id
title
price
}
}
}
} In both cases, Directivesconst 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)
}
}
}
}
}
*/ DocumentationFor full API documentation, check out the API docs. ContributingSetting 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 LicenseMIT, see LICENSE.md for details. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论