Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: GraphQL query & resolver for loading the primary shop #4747

Merged
merged 6 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice and simple, but it does two Shops.findOne calls when we often could do just one. You can make a separate primaryShop query function that does:

const domain = url.parse(context.rootUrl).hostname;
let shop = await Shops.findOne({ domains: domain });
if (!shop) {
  shop = await Shops.findOne({ shopType: "primary" });
}
return shop;

And then call that in this resolver

}
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
}