Skip to content

Instantly share code, notes, and snippets.

@sanderploegsma
Last active October 17, 2019 06:56
Show Gist options
  • Save sanderploegsma/785a0fbee47148ea245678ce944f3216 to your computer and use it in GitHub Desktop.
Save sanderploegsma/785a0fbee47148ea245678ce944f3216 to your computer and use it in GitHub Desktop.
exports.plugins = [
{
resolve: `gatsby-source-graphql`,
options: {
typeName: `WordPressNL`,
fieldName: `wordpressNL`,
url: `example.com/graphql`
}
},
{
resolve: `gatsby-source-graphql`,
options: {
typeName: `WordPressEN`,
fieldName: `wordpressEN`,
url: `example.com/en/graphql`
}
},
// ... other languages
];
const path = require("path");
const pageTemplate = path.resolve("gatsby-page-template.js");
const pagesQuery = `
query Pages {
wordpressNL {
pages {
nodes {
id
slug
}
}
}
wordpressEN {
pages {
nodes {
id
slug
}
}
}
# ... other languages
}
`;
const createLocalizedPages = (createPage, pages, language) =>
pages.forEach(({ id, slug }) => createPage({
path: `/${language}/${slug}`,
component: pageTemplate,
context: {
id,
language
}
}));
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(pagesQuery);
createLocalizedPages(createPage, result.data.wordpressNL.pages.nodes, "nl");
createLocalizedPages(createPage, result.data.wordpressEN.pages.nodes, "en");
// ... other languages
};
import React from "react";
import { graphql } from "gatsby";
const Page = ({ data }) => {
const page = data.wordpressNL.page || data.wordpressEN.page; // ... other languages
return (
<div>
<h1>{page.title}</h1>
<p>{page.description}</p>
</div>
);
};
export const query = graphql`
query Page($id: String!) {
wordpressNL {
page(id: $id) {
title
description
}
}
wordpressEN {
page(id: $id) {
title
description
}
}
# ... other languages
}
`;
export default Page;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment