Skip to content

Commit

Permalink
Merge pull request #4927 from reactioncommerce/feat-4796-impactmass-s…
Browse files Browse the repository at this point in the history
…itemap-query

feat: Make Sitemap data available via GraphQL query
  • Loading branch information
impactmass committed Jan 24, 2019
2 parents 831c381 + dd7b7f7 commit c58d8ce
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 3 deletions.
4 changes: 4 additions & 0 deletions imports/plugins/included/sitemap-generator/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Reaction from "/imports/plugins/core/core/server/Reaction";
import mutations from "./server/no-meteor/mutations";
import resolvers from "./server/no-meteor/resolvers";
import schemas from "./server/no-meteor/schemas";
import startup from "./server/no-meteor/startup";

Reaction.registerPackage({
label: "Sitemap Generator",
Expand All @@ -12,5 +13,8 @@ Reaction.registerPackage({
resolvers,
schemas
},
functionsByType: {
startup: [startup]
},
mutations
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import getSitemapXML from "../lib/get-sitemap-xml";
* @summary Route handler/middleware for generated sitemap XML files
* @param {Object} req - Node.js IncomingMessage object
* @param {Object} res - Node.js ServerResponse object
* @param {Function} next - Passes handling of request to next relevant middlewhere
* @param {Function} next - Passes handling of request to next relevant middleware
* @returns {undefined} - Sends XML to response, or triggers 404
*/
export default function handleSitemapRoutes(req, res, next) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sitemap from "./sitemap";

export default {
sitemap
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import url from "url";

/**
* @name sitemapQuery
* @method
* @param {Object} _ - unused
* @param {Object} params - an object of all arguments that were sent by the client
* @param {String} params.handle - Sitemap's handle, as set in Sitemaps collection
* @param {String} params.shopUrl - URL of the shop the sitemap belongs to. The URL is used to find the shop with the domain of the URL
* @param {Object} context - an object containing the per-request state
* @returns {String} - Sitemap object containing XML with placeholders replaced (BASE_URL, LAST_MOD)
*/
export default async function sitemapQuery(_, params, context) {
const { Sitemaps, Shops } = context.collections;
const { handle, shopUrl } = params;

const domain = url.parse(shopUrl).hostname;

// ensure the domain requested is for a known shop domain
const { _id: shopId } = await Shops.findOne({ domains: domain }) || {};

if (!shopId) return null;

const sitemap = await Sitemaps.findOne({ shopId, handle });

if (!sitemap) return null;

sitemap.xml = sitemap.xml.replace(/BASE_URL/g, shopUrl);

return sitemap;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { encodeShopOpaqueId } from "@reactioncommerce/reaction-graphql-xforms/shop";

export default {
shopId: (node) => encodeShopOpaqueId(node.shopId)
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Mutation from "./Mutation";
import Query from "./Query";
import Sitemap from "./Sitemap";

export default {
Mutation
Mutation,
Query,
Sitemap
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,26 @@ input GenerateSitemapsInput {
type GenerateSitemapsPayload {
"The same string you sent with the mutation params, for matching mutation calls with their responses"
clientMutationId: String

"Whether the sitemap generation job was successfully scheduled"
wasJobScheduled: Boolean!
}

extend type Query {
"Returns Sitemap object for a shop based on the handle param"
sitemap(handle: String!, shopUrl: String!): Sitemap
}

type Sitemap {
"The shop ID"
shopId: String!

"The Sitemap handle, as set in Sitemaps collection"
handle: String!

"The Sitemap XML content"
xml: String!

"Date created"
createdAt: Date!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @summary Called on startup
* @param {Object} context Startup context
* @returns {undefined}
*/
export default function startup(context) {
context.collections.Sitemaps = context.app.db.collection("Sitemaps");
}

0 comments on commit c58d8ce

Please sign in to comment.