gatsby-source-microcms
![install size](https://packagephobia.now.sh/badge?p=gatsby-source-microcms)
Source plugin for Gatsby from microCMS.
Install
$ npm install gatsby-source-microcms
$ yarn add gatsby-source-microcms
How to use
gatsby-config.js
First, to fetch contents from microCMS, you need setting options in gatsby-config.js
.
module.exports = {
plugins: [
{
resolve: 'gatsby-source-microcms',
options: {
apiKey: 'MICROCMS_API_KEY',
serviceId: 'myblog',
apis: [
{
endpoint: 'posts',
},
],
},
},
],
};
gatsby-node.js
You can query like the following. Gatsby create Pages based on microCMS contents.
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(
`
{
allMicrocmsPost(sort: { fields: [createdAt], order: DESC }) {
edges {
node {
id
createdAt
}
}
}
}
`
);
if (result.errors) {
throw result.errors;
}
result.data.allMicrocmsPost.edges.forEach((post, index) => {
createPage({
path: post.node.id,
component: path.resolve('./src/templates/blog-post.js'),
context: {
slug: post.node.id,
},
});
});
};
Options
module.exports = {
plugins: [
{
resolve: 'gatsby-source-microcms',
options: {
apiKey: '11111111-2222-3333-4444-555555555555',
serviceId: 'xxxx',
apis: [
{
endpoint: 'posts',
type: 'post',
format: 'object',
query: {
draftKey: 'DRAFT_KEY',
limit: 100,
offset: 40,
fields: ['id', 'title', 'body'].join(','),
filters: 'tag[exists]',
depth: 2,
},
},
],
},
},
],
};
filters
This plugin provides filters query building helper.
const {
and,
contains,
exists,
} = require('gatsby-source-microcms/src/query-builder');
module.exports = {
plugins: [
{
resolve: 'gatsby-source-microcms',
options: {
apis: [
{
query: {
filters: and(contains('title', 'sale'), exists('tag')),
},
},
],
},
},
],
};
Helper list:
equals('gender', 'women');
notEquals('gender', 'women');
lessThan('createdAt', '2019-11');
greaterThan('createdAt', '2019-11');
contains('title', 'sale');
exists('nextLink');
notExists('nextLink');
beginsWith('publishedAt', '2019-11');
and('filter1', 'filter2', ..., 'filter10')
or('filter1', 'filter2', ..., 'filter10')
Contributing
日本語歓迎 🇯🇵
Pull Request, Issue お気軽にどうぞ。