Skip to content

Commit

Permalink
fix(gatsby-source-mongodb): sanitize type of nodes to only contain al…
Browse files Browse the repository at this point in the history
…phanumeric chars and underscores (gatsbyjs#7246)

Fixes: gatsbyjs#7218 

(copied from issue)
This is due to the fact that `graphql` requires the node's `name` to match the `/^[_a-zA-Z][_a-zA-Z0-9]*$/` regex: 

https://github.com/graphql/graphql-js/blob/5fe39262a308df944a87cc85b225228e7556aaa4/src/utilities/assertValidName.js#L14
  • Loading branch information
michalczaplinski authored and gpetrioli committed Jan 22, 2019
1 parent c118983 commit 1e5af8c
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/gatsby-source-mongodb/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function createNodes(
parent: `__${collectionName}__`,
children: [],
internal: {
type: `mongodb${caps(dbName)}${caps(collectionName)}`,
type: `mongodb${sanitizeName(dbName)}${sanitizeName(collectionName)}`,
content: JSON.stringify(item),
contentDigest: crypto
.createHash(`md5`)
Expand Down Expand Up @@ -129,8 +129,10 @@ function createNodes(
})
}

function caps(s) {
return s.replace(/\b\w/g, l => l.toUpperCase())
function sanitizeName(s) {
return s
.replace(/[^_a-zA-Z0-9]/, ``)
.replace(/\b\w/g, l => l.toUpperCase())
}

function getConnectionExtraParams(extraParams) {
Expand Down

0 comments on commit 1e5af8c

Please sign in to comment.