Skip to content

Commit

Permalink
fix(gatsby): Fix wrongly skipping graphql filter cache (#21578)
Browse files Browse the repository at this point in the history
* Fix wrongly skipping cache

* Keys were both flattened to dotted chains and not and failed
comparison
* Additionally refactored so that we don't flatten too much

* Add a helpful comment

* Don't include comparator in index path

* Add tests to test caching

* Node8 support

* Jeesh typobot, okay

* Fix linting errors

* More lint fixes

* Update packages/gatsby/src/redux/__tests__/run-sift.js

Co-Authored-By: Vladimir Razuvaev <vladimir.razuvaev@gmail.com>

* Fix ts warnings

Co-authored-by: Vladimir Razuvaev <vladimir.razuvaev@gmail.com>
  • Loading branch information
freiksenet and vladar authored Feb 21, 2020
1 parent be1d1aa commit 4f68761
Show file tree
Hide file tree
Showing 5 changed files with 896 additions and 417 deletions.
289 changes: 289 additions & 0 deletions packages/gatsby/src/db/common/__tests__/query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
import { createDbQueriesFromObject, dbQueryToSiftQuery } from "../query"

describe(`DbQuery`, () => {
it(`converts basic query`, () => {
const query = createDbQueriesFromObject({ id: { $eq: `2` } })
expect(query).toMatchInlineSnapshot(`
Array [
Object {
"path": Array [
"id",
],
"query": Object {
"comparator": "$eq",
"value": "2",
},
"type": "query",
},
]
`)
expect(query.map(q => dbQueryToSiftQuery(q))).toMatchInlineSnapshot(`
Array [
Object {
"id": Object {
"$eq": "2",
},
},
]
`)
})

it(`converts nested query`, () => {
const query = createDbQueriesFromObject({
internal: { value: { $eq: `2` } },
})
expect(query).toMatchInlineSnapshot(`
Array [
Object {
"path": Array [
"internal",
"value",
],
"query": Object {
"comparator": "$eq",
"value": "2",
},
"type": "query",
},
]
`)
expect(query.map(q => dbQueryToSiftQuery(q))).toMatchInlineSnapshot(`
Array [
Object {
"internal.value": Object {
"$eq": "2",
},
},
]
`)
})

it(`converts branching nested query`, () => {
const query = createDbQueriesFromObject({
internal: { value: { $eq: `2` }, otherValue: { $regex: /baz/ } },
})
expect(query).toMatchInlineSnapshot(`
Array [
Object {
"path": Array [
"internal",
"value",
],
"query": Object {
"comparator": "$eq",
"value": "2",
},
"type": "query",
},
Object {
"path": Array [
"internal",
"otherValue",
],
"query": Object {
"comparator": "$regex",
"value": /baz/,
},
"type": "query",
},
]
`)
expect(query.map(q => dbQueryToSiftQuery(q))).toMatchInlineSnapshot(`
Array [
Object {
"internal.value": Object {
"$eq": "2",
},
},
Object {
"internal.otherValue": Object {
"$regex": /baz/,
},
},
]
`)
})

it(`converts complex branching nested query`, () => {
const query = createDbQueriesFromObject({
id: { $in: [`foo`] },
internal: { value: { $eq: `2` }, otherValue: { $regex: /baz/ } },
})

expect(query).toMatchInlineSnapshot(`
Array [
Object {
"path": Array [
"id",
],
"query": Object {
"comparator": "$in",
"value": Array [
"foo",
],
},
"type": "query",
},
Object {
"path": Array [
"internal",
"value",
],
"query": Object {
"comparator": "$eq",
"value": "2",
},
"type": "query",
},
Object {
"path": Array [
"internal",
"otherValue",
],
"query": Object {
"comparator": "$regex",
"value": /baz/,
},
"type": "query",
},
]
`)
expect(query.map(q => dbQueryToSiftQuery(q))).toMatchInlineSnapshot(`
Array [
Object {
"id": Object {
"$in": Array [
"foo",
],
},
},
Object {
"internal.value": Object {
"$eq": "2",
},
},
Object {
"internal.otherValue": Object {
"$regex": /baz/,
},
},
]
`)
})

it(`converts elemMatch`, () => {
const query = createDbQueriesFromObject({
nested: { $elemMatch: { $eq: `foo` } },
})
expect(query).toMatchInlineSnapshot(`
Array [
Object {
"nestedQuery": Object {
"path": Array [],
"query": Object {
"comparator": "$eq",
"value": "foo",
},
"type": "query",
},
"path": Array [
"nested",
],
"type": "elemMatch",
},
]
`)
expect(query.map(q => dbQueryToSiftQuery(q))).toMatchInlineSnapshot(`
Array [
Object {
"nested": Object {
"$elemMatch": Object {
"$eq": "foo",
},
},
},
]
`)
})

it(`converts complex elemMatch`, () => {
const query = createDbQueriesFromObject({
nested: { $elemMatch: { foo: { $eq: `foo` } } },
})
expect(query).toMatchInlineSnapshot(`
Array [
Object {
"nestedQuery": Object {
"path": Array [
"foo",
],
"query": Object {
"comparator": "$eq",
"value": "foo",
},
"type": "query",
},
"path": Array [
"nested",
],
"type": "elemMatch",
},
]
`)
expect(query.map(q => dbQueryToSiftQuery(q))).toMatchInlineSnapshot(`
Array [
Object {
"nested": Object {
"$elemMatch": Object {
"foo": Object {
"$eq": "foo",
},
},
},
},
]
`)
})

it(`converts nested elemMatch`, () => {
const query = createDbQueriesFromObject({
nested: { $elemMatch: { foo: { $eq: `foo` }, bar: { $eq: `bar` } } },
})
expect(query).toMatchInlineSnapshot(`
Array [
Object {
"nestedQuery": Object {
"path": Array [
"foo",
],
"query": Object {
"comparator": "$eq",
"value": "foo",
},
"type": "query",
},
"path": Array [
"nested",
],
"type": "elemMatch",
},
Object {
"nestedQuery": Object {
"path": Array [
"bar",
],
"query": Object {
"comparator": "$eq",
"value": "bar",
},
"type": "query",
},
"path": Array [
"nested",
],
"type": "elemMatch",
},
]
`)
})
})
93 changes: 0 additions & 93 deletions packages/gatsby/src/db/common/query.js

This file was deleted.

Loading

0 comments on commit 4f68761

Please sign in to comment.