-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4747 from reactioncommerce/feat-4727-dancastellon…
…-primaryShop-query feat: GraphQL query & resolver for loading the primary shop
- Loading branch information
Showing
5 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
2 changes: 2 additions & 0 deletions
2
imports/plugins/core/core/server/no-meteor/resolvers/Query/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
imports/plugins/core/core/server/no-meteor/resolvers/Query/primaryShop.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
22 changes: 22 additions & 0 deletions
22
imports/plugins/core/core/server/no-meteor/resolvers/Query/primaryShop.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters