forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): Move connection out of sift (gatsbyjs#9508)
**Builds on gatsbyjs#9416 . Merge that one before this** In additional to performing the queries, `run-sift.js` also currently converts the results into connections (if connection query). But as part of gatsbyjs#9338, we will be using `loki` to perform queries too. So we would need to duplicate the connection logic in both query engines. This PR pulls the connection logic out of run-sift and into `build-node-connections` which makes sense to me as the query engine doesn't need to know about connections. Only about whether it should return one result or many (thus the `firstOnly` flag). Another benefit is that the query engine no longer needs to understand page dependencies.
- Loading branch information
1 parent
cda5682
commit 4731239
Showing
14 changed files
with
352 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,7 +211,7 @@ const query = graphql` | |
} | ||
} | ||
} | ||
`; | ||
` | ||
``` | ||
|
||
## Examples | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
packages/gatsby/src/schema/__tests__/build-node-connections-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
const { graphql, GraphQLObjectType, GraphQLSchema } = require(`graphql`) | ||
const _ = require(`lodash`) | ||
const buildNodeTypes = require(`../build-node-types`) | ||
const buildNodeConnections = require(`../build-node-connections`) | ||
|
||
jest.mock(`../../redux/actions/add-page-dependency`, () => { | ||
return { | ||
createPageDependency: jest.fn(), | ||
} | ||
}) | ||
|
||
const { | ||
createPageDependency, | ||
} = require(`../../redux/actions/add-page-dependency`) | ||
|
||
describe(`build-node-connections`, () => { | ||
let schema, store, types, connections | ||
|
||
async function runQuery(query) { | ||
let context = { path: `foo` } | ||
let { data, errors } = await graphql(schema, query, context, context) | ||
expect(errors).not.toBeDefined() | ||
return data | ||
} | ||
|
||
beforeEach(async () => { | ||
;({ store } = require(`../../redux`)) | ||
store.dispatch({ type: `DELETE_CACHE` }) | ||
;[ | ||
{ | ||
id: `p1`, | ||
internal: { type: `Parent` }, | ||
hair: `red`, | ||
children: [`c1`, `c2`, `r1`], | ||
}, | ||
{ | ||
id: `r1`, | ||
internal: { type: `Relative` }, | ||
hair: `black`, | ||
children: [], | ||
parent: `p1`, | ||
}, | ||
{ | ||
id: `c1`, | ||
internal: { type: `Child` }, | ||
hair: `brown`, | ||
children: [], | ||
parent: `p1`, | ||
}, | ||
{ | ||
id: `c2`, | ||
internal: { type: `Child` }, | ||
hair: `blonde`, | ||
children: [], | ||
parent: `p1`, | ||
}, | ||
].forEach(n => store.dispatch({ type: `CREATE_NODE`, payload: n })) | ||
|
||
types = await buildNodeTypes({}) | ||
connections = await buildNodeConnections(_.values(types)) | ||
|
||
schema = new GraphQLSchema({ | ||
query: new GraphQLObjectType({ | ||
name: `RootQueryType`, | ||
fields: { ...connections, ..._.mapValues(types, `node`) }, | ||
}), | ||
}) | ||
}) | ||
|
||
it(`should build connections`, () => { | ||
expect(Object.keys(connections)).toHaveLength(3) | ||
}) | ||
|
||
it(`should result in a valid queryable schema`, async () => { | ||
let { allParent, allChild, allRelative } = await runQuery( | ||
` | ||
{ | ||
allParent(filter: { id: { eq: "p1" } }) { | ||
edges { | ||
node { | ||
hair | ||
} | ||
} | ||
} | ||
allChild(filter: { id: { eq: "c1" } }) { | ||
edges { | ||
node { | ||
hair | ||
} | ||
} | ||
} | ||
allRelative(filter: { id: { eq: "r1" } }) { | ||
edges { | ||
node { | ||
hair | ||
} | ||
} | ||
} | ||
} | ||
` | ||
) | ||
expect(allParent.edges[0].node.hair).toEqual(`red`) | ||
expect(allChild.edges[0].node.hair).toEqual(`brown`) | ||
expect(allRelative.edges[0].node.hair).toEqual(`black`) | ||
}) | ||
|
||
it(`should link children automatically`, async () => { | ||
let { allParent } = await runQuery( | ||
` | ||
{ | ||
allParent(filter: { id: { eq: "p1" } }) { | ||
edges { | ||
node { | ||
children { | ||
id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
) | ||
expect(allParent.edges[0].node.children).toBeDefined() | ||
expect(allParent.edges[0].node.children.map(c => c.id)).toEqual([ | ||
`c1`, | ||
`c2`, | ||
`r1`, | ||
]) | ||
}) | ||
|
||
it(`should create typed children fields`, async () => { | ||
let { allParent } = await runQuery( | ||
` | ||
{ | ||
allParent(filter: { id: { eq: "p1" } }) { | ||
edges { | ||
node { | ||
childrenChild { # lol | ||
id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
) | ||
expect(allParent.edges[0].node.childrenChild).toBeDefined() | ||
expect(allParent.edges[0].node.childrenChild.map(c => c.id)).toEqual([ | ||
`c1`, | ||
`c2`, | ||
]) | ||
}) | ||
|
||
it(`should create typed child field for singular children`, async () => { | ||
let { allParent } = await runQuery( | ||
` | ||
{ | ||
allParent(filter: { id: { eq: "p1" } }) { | ||
edges { | ||
node { | ||
childRelative { # lol | ||
id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
) | ||
|
||
expect(allParent.edges[0].node.childRelative).toBeDefined() | ||
expect(allParent.edges[0].node.childRelative.id).toEqual(`r1`) | ||
}) | ||
|
||
it(`should create page dependency`, async () => { | ||
await runQuery( | ||
` | ||
{ | ||
allParent(filter: { id: { eq: "p1" } }) { | ||
edges { | ||
node { | ||
childRelative { # lol | ||
id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
) | ||
|
||
expect(createPageDependency).toHaveBeenCalledWith({ | ||
path: `foo`, | ||
connection: `Parent`, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.