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

fix(gatsby): character escape sequences in regex filter in graphql queries #26592

Merged
merged 1 commit into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/graphql-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ _In the playground below the list, there is an example query with a description

- `eq`: short for **equal**, must match the given data exactly
- `ne`: short for **not equal**, must be different from the given data
- `regex`: short for **regular expression**, must match the given pattern. Note that backslashes need to be escaped _twice_, so `/\w+/` needs to be written as `"/\\\\w+/"`.
- `regex`: short for **regular expression**, must match the given pattern
- `glob`: short for **global**, allows to use wildcard `*` which acts as a placeholder for any non-empty string
- `in`: short for **in array**, must be an element of the array
- `nin`: short for **not in array**, must NOT be an element of the array
Expand Down
11 changes: 11 additions & 0 deletions packages/gatsby/src/schema/__tests__/fixtures/regex-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exports.query = `{
allMarkdown(filter: { frontmatter: { authors: { elemMatch: { email: { regex: "/^\\w{6}\\d@\\w{7}\\.COM$/i" } } } } }) {
nodes {
frontmatter {
authors {
email
}
}
}
}
}`
98 changes: 98 additions & 0 deletions packages/gatsby/src/schema/__tests__/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -1579,4 +1579,102 @@ describe(`Query schema`, () => {
expect(results.data).toEqual(expected)
})
})

describe(`with regex filter`, () => {
/**
* double-escape character escape sequences when written inline (test only)
* (see also the test src/utils/__tests__/prepare-regex.ts)
*/
it(`escape sequences work when correctly escaped`, async () => {
const query = `
{
allMarkdown(filter: { frontmatter: { authors: { elemMatch: { email: { regex: "/^\\\\w{6}\\\\d@\\\\w{7}\\\\.COM$/i" } } } } }) {
nodes {
frontmatter {
authors {
email
}
}
}
}
}
`
const results = await runQuery(query)
const expected = {
allMarkdown: {
nodes: [
{
frontmatter: {
authors: [
{
email: `[email protected]`,
},
{
email: `[email protected]`,
},
],
},
},
{
frontmatter: {
authors: [
{
email: `[email protected]`,
},
],
},
},
],
},
}
expect(results.errors).toBeUndefined()
expect(results.data).toEqual(expected)
})

/**
* queries are read from file and parsed with babel
*/
it.only(`escape sequences work when correctly escaped`, async () => {
const fs = require(`fs`)
const path = require(`path`)
const babel = require(`@babel/parser`)
const fileContent = fs.readFileSync(
path.join(__dirname, `./fixtures/regex-query.js`),
`utf-8`
)
const ast = babel.parse(fileContent)
const query = ast.program.body[0].expression.right.quasis[0].value.raw

const results = await runQuery(query)
const expected = {
allMarkdown: {
nodes: [
{
frontmatter: {
authors: [
{
email: `[email protected]`,
},
{
email: `[email protected]`,
},
],
},
},
{
frontmatter: {
authors: [
{
email: `[email protected]`,
},
],
},
},
],
},
}
expect(results.errors).toBeUndefined()
expect(results.data).toEqual(expected)
})
})
})
11 changes: 1 addition & 10 deletions packages/gatsby/src/utils/prepare-regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ import _ from "lodash"

export const prepareRegex = (str: string): RegExp => {
const exploded = str.split(`/`)
const regex = new RegExp(
exploded
.slice(1, -1)
.join(`/`)
// Double escaping is needed to get past the GraphQL parser,
// but single escaping is needed for the RegExp constructor,
// i.e. `"\\\\w+"` for `/\w+/`.
.replace(/\\\\/, `\\`),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC this was the actual problem, this regex was missing a global flag?

But I mean if we can drop this entirely that's even better. Will ask @vladar to take a sanity check for graphql.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double escaping my regex stops the errors, but doesn't return any results the same way the graphiql explorer does... Completely out of ideas.

_.last(exploded)
)
const regex = new RegExp(exploded.slice(1, -1).join(`/`), _.last(exploded))
return regex
}