Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0] Make nodes fully immutable by adding API for allowing plugins to add fields #1035

Merged
merged 17 commits into from
May 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename 'pluginFields' to 'fields'
  • Loading branch information
KyleAMathews committed May 26, 2017
commit e883aa17ddb5c19b8a1d2c48281f8a397bb883f3
2 changes: 1 addition & 1 deletion docs/docs/node-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ id: String,
children: Array[String],
parent: String,
// Reserved for plugins who wish to extend other nodes.
pluginFields: Object,
fields: Object,
internal: {
contentDigest: String,
mediaType: String,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/joi-schemas/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const nodeSchema = Joi.object()
id: Joi.string().required(),
children: Joi.array(Joi.string()).required(),
parent: Joi.string().required(),
pluginFields: Joi.object(),
fields: Joi.object(),
internal: Joi.object().keys({
contentDigest: Joi.string().required(),
mediaType: Joi.string().required(),
Expand Down
10 changes: 5 additions & 5 deletions packages/gatsby/src/redux/__tests__/__snapshots__/nodes.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Object {
},
"parent": "test",
"pickle": true,
"pluginFields": Object {
"fields": Object {
"joy": "soul's delight",
},
},
Expand Down Expand Up @@ -100,11 +100,11 @@ exports[`Create and update nodes throws error if a node is created by a plugin n
}"
`;

exports[`Create and update nodes throws error if a node sets a value on "pluginFields" 1`] = `
"Plugins creating nodes can not set data on the reserved field \\"pluginFields\\"
exports[`Create and update nodes throws error if a node sets a value on "fields" 1`] = `
"Plugins creating nodes can not set data on the reserved field \\"fields\\"
as this is reserved for plugins which wish to extend your nodes.

If your plugin didn't add \\"pluginFields\\" you're probably seeing this
If your plugin didn't add \\"fields\\" you're probably seeing this
error because you're reusing an old node object.

Node:
Expand All @@ -113,7 +113,7 @@ exports[`Create and update nodes throws error if a node sets a value on "pluginF
\\"id\\": \\"hi\\",
\\"children\\": [],
\\"parent\\": \\"test\\",
\\"pluginFields\\": {
\\"fields\\": {
\\"test\\": \\"I can't do this but I like to test boundaries\\"
},
\\"internal\\": {
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/redux/__tests__/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ describe(`Create and update nodes`, () => {
expect(callActionCreator).toThrowErrorMatchingSnapshot()
})

it(`throws error if a node sets a value on "pluginFields"`, () => {
it(`throws error if a node sets a value on "fields"`, () => {
function callActionCreator() {
actions.createNode(
{
id: `hi`,
children: [],
parent: `test`,
pluginFields: {
fields: {
test: `I can't do this but I like to test boundaries`,
},
internal: {
Expand Down
14 changes: 7 additions & 7 deletions packages/gatsby/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ actions.createNode = (node, plugin) => {
return { type: `VALIDATION_ERROR`, error: true }
}

// Ensure node isn't directly setting pluginFields.
if (node.pluginFields) {
// Ensure node isn't directly setting fields.
if (node.fields) {
throw new Error(
stripIndent`
Plugins creating nodes can not set data on the reserved field "pluginFields"
Plugins creating nodes can not set data on the reserved field "fields"
as this is reserved for plugins which wish to extend your nodes.

If your plugin didn't add "pluginFields" you're probably seeing this
If your plugin didn't add "fields" you're probably seeing this
error because you're reusing an old node object.

Node:
Expand Down Expand Up @@ -189,8 +189,8 @@ actions.addFieldToNode = ({ node, fieldName, fieldValue }, plugin) => {
if (!node.internal.fieldPluginOwners) {
node.internal.fieldPluginOwners = {}
}
if (!node.pluginFields) {
node.pluginFields = {}
if (!node.fields) {
node.fields = {}
}

// Check that this field isn't owned by another plugin.
Expand All @@ -209,7 +209,7 @@ actions.addFieldToNode = ({ node, fieldName, fieldValue }, plugin) => {
}

// Update node
node.pluginFields[fieldName] = fieldValue
node.fields[fieldName] = fieldValue
node.internal.fieldPluginOwners[fieldName] = plugin.name

return {
Expand Down
14 changes: 7 additions & 7 deletions www/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
allMarkdownRemark(limit: 1000) {
edges {
node {
pluginFields {
fields {
slug
package
}
Expand All @@ -37,22 +37,22 @@ exports.createPages = ({ graphql, boundActionCreators }) => {

// Create docs pages.
result.data.allMarkdownRemark.edges.forEach(edge => {
if (_.includes(edge.node.pluginFields.slug, `/blog/`)) {
if (_.includes(edge.node.fields.slug, `/blog/`)) {
upsertPage({
path: `${edge.node.pluginFields.slug}`, // required
path: `${edge.node.fields.slug}`, // required
component: slash(blogPostTemplate),
context: {
slug: edge.node.pluginFields.slug,
slug: edge.node.fields.slug,
},
})
} else {
upsertPage({
path: `${edge.node.pluginFields.slug}`, // required
path: `${edge.node.fields.slug}`, // required
component: slash(
edge.node.pluginFields.package ? packageTemplate : docsTemplate
edge.node.fields.package ? packageTemplate : docsTemplate
),
context: {
slug: edge.node.pluginFields.slug,
slug: edge.node.fields.slug,
},
})
}
Expand Down
4 changes: 2 additions & 2 deletions www/src/pages/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const IndexRoute = React.createClass({
post.frontmatter.author.avatar.childImageSharp.responsiveResolution
return (
<div>
<Link to={post.pluginFields.slug}>
<Link to={post.fields.slug}>
<h2
css={{
marginBottom: rhythm(1 / 8),
Expand Down Expand Up @@ -102,7 +102,7 @@ query IndexRouteQuery {
edges {
node {
excerpt
pluginFields { slug }
fields { slug }
frontmatter {
title
date(formatString: "DD MMMM, YYYY")
Expand Down
6 changes: 3 additions & 3 deletions www/src/templates/template-blog-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const BlogPostTemplate = React.createClass({
link={[
{
rel: `canonical`,
href: `https://gatsbyjs.org${post.pluginFields.slug}`,
href: `https://gatsbyjs.org${post.fields.slug}`,
},
{
rel: `author`,
Expand Down Expand Up @@ -176,11 +176,11 @@ export default BlogPostTemplate

export const pageQuery = graphql`
query TemplateBlogPost($slug: String!) {
markdownRemark(pluginFields: { slug: { eq: $slug }}) {
markdownRemark(fields: { slug: { eq: $slug }}) {
html
excerpt
timeToRead
pluginFields { slug }
fields { slug }
frontmatter {
title
date(formatString: "MMM D, YYYY")
Expand Down
2 changes: 1 addition & 1 deletion www/src/templates/template-docs-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default DocsTemplate

export const pageQuery = graphql`
query TemplateDocsMarkdown($slug: String!) {
markdownRemark(pluginFields: { slug: { eq: $slug }}) {
markdownRemark(fields: { slug: { eq: $slug }}) {
html
frontmatter {
title
Expand Down
6 changes: 3 additions & 3 deletions www/src/templates/template-docs-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import presets from "../utils/presets"

const DocsTemplate = React.createClass({
render() {
const packageName = this.props.data.markdownRemark.pluginFields.title
const packageName = this.props.data.markdownRemark.fields.title
return (
<div>
<a
Expand Down Expand Up @@ -35,8 +35,8 @@ export default DocsTemplate

export const pageQuery = graphql`
query TemplateDocsQuery($slug: String!) {
markdownRemark(pluginFields: { slug: { eq: $slug }}) {
pluginFields {
markdownRemark(fields: { slug: { eq: $slug }}) {
fields {
title
}
html
Expand Down