Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Dec 1, 2017
0 parents commit 6cc39d4
Show file tree
Hide file tree
Showing 18 changed files with 2,313 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/addDigestToNode.js
/addEntityAttributes.js
/createItemNodes.js
/createItemTypeNodes.js
/createSiteNode.js
/createTextNode.js
/digest.js
/fetch.js
/gatsby-node.js
/index.js
/initNodeFromEntity.js
/makeId.js
/makeType.js
/node_modules
Empty file added .npmignore
Empty file.
240 changes: 240 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
# gatsby-source-datocms

Source plugin for pulling models and records into Gatsby from DatoCMS administrative areas. It creates links between records so they can be queried in Gatsby using GraphQL.

## Install

`npm install --save gatsby-source-datocms`

## How to use

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-source-datocms`,
options: {
apiToken: `YOUR_READONLY_API_TOKEN`,
},
},
]
```

## How to query

Two standard data types will be available from DatoCMS: `DatoCmsModel` and `DatoCmsSite`. You can query model nodes created from DatoCMS like the following:

```graphql
{
allDatoCmsModel {
edges {
node {
apiKey
name
fields {
apiKey
fieldType
}
}
}
}
}
```

Your site global settings can be queried like this:

```graphql
{
datoCmsSite {
name
internalDomain
locales
}
}
```

### Accessing records

Non-standard data types, i.e. models you define in DatoCMS, will also be
available in Gatsby. They'll be created in your site's GraphQL schema under
`datoCms{modelApiKey}` and `allDatoCms{modelApiKey}`. For example,
if you have a `blog_post` model, you will be able to query it like the following:

```graphql
{
allDatoCmsBlogPost(sort: { fields: [publicationDate], order: DESC }, limit: 5) {
edges {
node {
title
excerpt
publicationDate(formatString: "MM-DD-YYYY")
author {
name
avatar {
url
}
}
}
}
}
}
```

### Multiple-paragraph text fields

Fields of type *Multiple-paragraph text* will be available both as simple
strings (ie. `excerpt`) and nodes (ie. `excerptNode`). You can use the latter
if you want to apply further transformations, like converting markdown with [`gatsby-transformer-remark`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-remark)):

```graphql
{
allDatoCmsBlogPost {
edges {
node {
excerptNode {
childMarkdownRemark {
html
timeToRead
}
}
}
}
}
}
```

### Modular content fields

[Modular-content fields](https://docs.datocms.com/schema/modular-content.html) can be queried this way:

```graphql
{
allDatoCmsBlogPost {
edges {
node {
title
content {
... on DatoCmsText {
text
}
... on DatoCmsImage {
image {
url
}
}
}
}
}
}
}
```

### Favicon

You can get the complete set of meta tags related to your site favicon like this:

```graphql
{
datoCmsSite {
faviconMetaTags {
tagName
attributes {
rel
sizes
href
name
content
type
}
}
}
}
```

### SEO meta tags

All records have a `seoMetaTags` field that you can use to build SEO meta tags
for your record's pages:

```graphql
{
allDatoCmsBlogPost {
edges {
node {
title

seoMetaTags {
tagName
content
attributes {
property
content
name
}
}
}
}
}
}
```

### Tree-like collections

If you have a model configured as a tree, you can navigate the hierarchy with
`treeChildren` and `treeParent` this way:

```graphql
{
allDatoCmsCategory(filter: { root: { eq: true } }) {
edges {
node {
title
treeChildren {
title
treeChildren {
title
}
}
}
}
}
}
```

### Single instance models

You can access to single instance models like this:

```graphql
{
datoCmsHomepage {
title
content
}
}
```

### Localized fields

If your site is multi-lingual, records will be duplicated for every locale
available, so you can query them like this. The same applies for the `DatoCmsSite`
node:


```graphql
{
allDatoCmsBlogPost(filter: { locale: { eq: "it" } }) {
edges {
node {
title
excerpt
}
}
}

datoCmsHomepage(locale: { eq: "it" }) {
title
content
}
}
```
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "gatsby-source-datocms",
"version": "1.0.3",
"description": "Gatsby source plugin for building websites using DatoCMS as data source",
"main": "index.js",
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"watch": "babel -w src --out-dir . --ignore __tests__",
"prepublish": "cross-env NODE_ENV=production npm run build"
},
"devDependencies": {
"babel-cli": "6.26.0",
"cross-env": "^5.0.5"
},
"dependencies": {
"babel-runtime": "^6.26.0",
"datocms-client": "0.3.39",
"humps": "2.0.1",
"json-stringify-safe": "5.0.1"
},
"keywords": [
"gatsby",
"gatsby-plugin",
"gatsby-source-plugin"
],
"author": "DatoCMS <support@datocms.com>",
"license": "ISC"
}
6 changes: 6 additions & 0 deletions src/addDigestToNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const digest = require('./digest');
const stringify = require('json-stringify-safe');

module.exports = function addDigestToNode(node) {
node.internal.contentDigest = digest(stringify(node));
}
7 changes: 7 additions & 0 deletions src/addEntityAttributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function addEntityAttributes(node, entity) {
Object.entries(entity.payload.attributes || {}).forEach(([name, value]) => {
node[name] = value;
});
}


Loading

0 comments on commit 6cc39d4

Please sign in to comment.