Skip to content

Commit

Permalink
move helpers into resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Mina Smart committed Nov 30, 2017
1 parent f1c892b commit 7a0ad29
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 43 deletions.
26 changes: 3 additions & 23 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import GraphQLJSClient from './graphql-client';
import Config from './config';
import productHelpers from './product-helpers';
import imageHelpers from './image-helpers';
import ProductResource from './product-resource';
import CollectionResource from './collection-resource';
import ShopResource from './shop-resource';
import CheckoutResource from './checkout-resource';
import ImageResource from './image-resource';
import {version} from '../package.json';

// GraphQL
Expand All @@ -17,26 +16,6 @@ import types from '../schema.json';
*/
class Client {

/**
* A namespace providing utilities for product resources.
* @namespace
*/
static get Product() {
return {
Helpers: productHelpers
};
}

/**
* A namespace providing utilities for image resources.
* @namespace
*/
static get Image() {
return {
Helpers: imageHelpers
};
}

/**
* Primary entry point for building a new Client.
*/
Expand Down Expand Up @@ -64,10 +43,11 @@ class Client {
}
});

this.product = new ProductResource(this.graphQLClient, productHelpers);
this.product = new ProductResource(this.graphQLClient);
this.collection = new CollectionResource(this.graphQLClient);
this.shop = new ShopResource(this.graphQLClient);
this.checkout = new CheckoutResource(this.graphQLClient);
this.image = new ImageResource(this.graphQLClient);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/image-resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Resource from './resource';
import imageHelpers from './image-helpers';

export default class ImageResource extends Resource {
get helpers() {
return imageHelpers;
}
}
4 changes: 4 additions & 0 deletions src/product-resource.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import Resource from './resource';
import defaultResolver from './default-resolver';
import {paginateProductConnectionsAndResolve} from './paginators';
import productHelpers from './product-helpers';

// GraphQL
import productNodeQuery from './graphql/productNodeQuery.graphql';
import productConnectionQuery from './graphql/productConnectionQuery.graphql';
import productByHandleQuery from './graphql/productByHandleQuery.graphql';

export default class ProductResource extends Resource {
get helpers() {
return productHelpers;
}

/**
* Fetches all products on the shop.
Expand Down
3 changes: 1 addition & 2 deletions src/resource.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export default class Resource {
constructor(client, helpers) {
constructor(client) {
this.graphQLClient = client;
this.helpers = helpers;
}
}
28 changes: 10 additions & 18 deletions test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import GraphQLJSClient from '../src/graphql-client';
import Config from '../src/config';
import Client from '../src/client';
import types from '../schema.json';
import fetchMock from './isomorphic-fetch-mock'; // eslint-disable-line import/no-unresolved
import {version} from '../package.json';

suite('client-test', () => {
teardown(() => {
fetchMock.restore();
});
const config = {
domain: 'sendmecats.myshopify.com',
storefrontAccessToken: 'abc123'
};

test('it instantiates a GraphQL client with the given config', () => {
let passedTypeBundle;
Expand All @@ -24,12 +24,7 @@ suite('client-test', () => {
}
}

const config = new Config({
domain: 'sendmecats.myshopify.com',
storefrontAccessToken: 'abc123'
});

new Client(config, FakeGraphQLJSClient); // eslint-disable-line no-new
new Client(new Config(config), FakeGraphQLJSClient); // eslint-disable-line no-new

assert.deepEqual(passedTypeBundle, types);
assert.equal(passedUrl, 'https://sendmecats.myshopify.com/api/graphql');
Expand All @@ -43,18 +38,15 @@ suite('client-test', () => {
});

test('it creates an instance of the GraphQLJSClient by default', () => {
const config = new Config({
domain: 'sendmecats.myshopify.com',
storefrontAccessToken: 'abc123'
});

const client = new Client(config);
const client = Client.buildClient(config);

assert.ok(GraphQLJSClient.prototype.isPrototypeOf(client.graphQLClient));
});

test('it has static helpers', () => {
assert.ok(Client.Product.Helpers);
assert.ok(Client.Image.Helpers);
const client = Client.buildClient(config);

assert.ok(client.product.helpers);
assert.ok(client.image.helpers);
});
});

0 comments on commit 7a0ad29

Please sign in to comment.