Skip to content

Commit

Permalink
Merge pull request #4747 from reactioncommerce/feat-4727-dancastellon…
Browse files Browse the repository at this point in the history
…-primaryShop-query

feat: GraphQL query & resolver for loading the primary shop
  • Loading branch information
aldeed authored Oct 18, 2018
2 parents 44d7fd9 + 3ad8dbe commit 947bbb8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
14 changes: 13 additions & 1 deletion imports/plugins/core/core/server/no-meteor/queries.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import url from "url";

export default {
shopById: (context, _id) => context.collections.Shops.findOne({ _id }),
shopBySlug: (context, slug) => context.collections.Shops.findOne({ slug })
shopBySlug: (context, slug) => context.collections.Shops.findOne({ slug }),
primaryShop: async (context) => {
const { collections, rootUrl } = context;
const { Shops } = collections;
const domain = url.parse(rootUrl).hostname;
let shop = await Shops.findOne({ domains: domain });
if (!shop) {
shop = await Shops.findOne({ shopType: "primary" });
}
return shop;
}
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import primaryShopId from "./primaryShopId";
import primaryShop from "./primaryShop";
import shop from "./shop";
import shopBySlug from "./shopBySlug";
import tag from "./tag";
import tags from "./tags";

export default {
primaryShopId,
primaryShop,
shop,
shopBySlug,
tag,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @name "Query.primaryShop"
* @method
* @memberof Shop/GraphQL
* @summary Gets the primary shop
* @param {Object} parentObject - unused
* @param {Object} args - unused
* @param {Object} context - an object containing the per-request state
* @return {Promise<Object>} The shop, based on the domain in ROOT_URL
*/
export default async function primaryShop(_, __, context) {
const shop = await context.queries.primaryShop(context);
return shop;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import primaryShop from "./primaryShop";

const fakeShopId = "W64ZQe9RUMuAoKrli";

const fakeShop = {
_id: fakeShopId,
name: "Reaction"
};

test("calls queries.primaryShop and returns the correct shop", async () => {
const primaryShopMock = jest.fn().mockName("queries.primaryShop").mockReturnValueOnce(Promise.resolve(fakeShop));

const shopObject = await primaryShop(null, null, {
queries: {
primaryShop: primaryShopMock
}
});

expect(shopObject).toEqual(fakeShop);

expect(primaryShopMock).toHaveBeenCalled();
});
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,7 @@ extend type Query {

"Returns the ID of the primary shop for the domain"
primaryShopId: ID

"Returns the primary shop for the domain"
primaryShop: Shop
}

0 comments on commit 947bbb8

Please sign in to comment.