Skip to content

Commit 47a800c

Browse files
michalczaplinskipieh
authored andcommitted
fix(gatsby-source-mongodb): sanitize type of nodes to only contain alphanumeric chars and underscores (#7246)
Fixes: #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
1 parent 00f2992 commit 47a800c

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

packages/gatsby-source-mongodb/src/gatsby-node.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function createNodes(
8383
parent: `__${collectionName}__`,
8484
children: [],
8585
internal: {
86-
type: `mongodb${caps(dbName)}${caps(collectionName)}`,
86+
type: `mongodb${sanitizeName(dbName)}${sanitizeName(collectionName)}`,
8787
content: JSON.stringify(item),
8888
contentDigest: crypto
8989
.createHash(`md5`)
@@ -129,8 +129,10 @@ function createNodes(
129129
})
130130
}
131131

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

136138
function getConnectionExtraParams(extraParams) {

0 commit comments

Comments
 (0)