Skip to content

Commit

Permalink
fix(gatsby): pass custom graphql context provided by createResolverCo…
Browse files Browse the repository at this point in the history
…ntext to materialization executor (#36552)
  • Loading branch information
pieh committed Sep 7, 2022
1 parent 9c5eacf commit 8aeae21
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
46 changes: 46 additions & 0 deletions packages/gatsby/src/schema/__tests__/node-model.js
Expand Up @@ -629,6 +629,7 @@ describe(`NodeModel`, () => {
let resolveBetterTitleMock
let resolveOtherTitleMock
let resolveSlugMock
let resolveCustomContextMock
let materializationSpy
beforeEach(async () => {
const nodes = (() => [
Expand Down Expand Up @@ -735,6 +736,13 @@ describe(`NodeModel`, () => {
resolveBetterTitleMock = jest.fn()
resolveOtherTitleMock = jest.fn()
resolveSlugMock = jest.fn()
resolveCustomContextMock = jest.fn()

store.dispatch(
actions.createResolverContext({
myCustomContext: `foo`,
})
)
store.dispatch({
type: `CREATE_TYPES`,
payload: [
Expand Down Expand Up @@ -819,6 +827,13 @@ describe(`NodeModel`, () => {
return true
},
},
usingCustomResolverContext: {
type: `String`,
resolve: (parent, _args, context) => {
resolveCustomContextMock({ context, parent })
return `${context.myCustomContext}/${parent.title}`
},
},
},
}),
typeBuilders.buildObjectType({
Expand Down Expand Up @@ -1296,6 +1311,37 @@ describe(`NodeModel`, () => {
])
)
})

it(`injects context passed by createResolverContext action when materializing fields`, async () => {
nodeModel.replaceFiltersCache()
const wat = await nodeModel.findAll(
{
query: {
sort: { fields: [`usingCustomResolverContext`], order: [`ASC`] },
},
type: `Test`,
},
{ path: `/` }
)

expect(resolveCustomContextMock).toHaveBeenCalledTimes(2)
expect(resolveCustomContextMock).toHaveBeenCalledWith({
context: expect.objectContaining({
myCustomContext: `foo`,
}),
parent: expect.objectContaining({
id: `id1`,
}),
})
expect(resolveCustomContextMock).toHaveBeenCalledWith({
context: expect.objectContaining({
myCustomContext: `foo`,
}),
parent: expect.objectContaining({
id: `id2`,
}),
})
})
})

describe(`node tracking`, () => {
Expand Down
25 changes: 18 additions & 7 deletions packages/gatsby/src/schema/node-model.js
Expand Up @@ -477,6 +477,9 @@ class LocalNodeModel {
)

if (!_.isEmpty(actualFieldsToResolve)) {
const {
schemaCustomization: { context: customContext },
} = store.getState()
const resolvedNodes = new Map()
for (const node of getDataStore().iterateNodesByType(typeName)) {
this.trackInlineObjectsInRootNode(node)
Expand All @@ -487,7 +490,8 @@ class LocalNodeModel {
node,
type,
queryFields,
actualFieldsToResolve
actualFieldsToResolve,
customContext
)

resolvedNodes.set(node.id, resolvedFields)
Expand Down Expand Up @@ -770,7 +774,8 @@ async function resolveRecursive(
node,
type,
queryFields,
fieldsToResolve
fieldsToResolve,
customContext
) {
const gqlFields = getFields(schema, type, node)
const resolvedFields = {}
Expand All @@ -786,7 +791,8 @@ async function resolveRecursive(
schema,
node,
gqlField,
fieldName
fieldName,
customContext
)
if (gqlField && innerValue != null) {
if (
Expand All @@ -800,7 +806,8 @@ async function resolveRecursive(
innerValue,
gqlFieldType,
queryField,
_.isObject(fieldToResolve) ? fieldToResolve : queryField
_.isObject(fieldToResolve) ? fieldToResolve : queryField,
customContext
)
} else if (
isCompositeType(gqlFieldType) &&
Expand All @@ -818,7 +825,8 @@ async function resolveRecursive(
item,
gqlFieldType,
queryField,
_.isObject(fieldToResolve) ? fieldToResolve : queryField
_.isObject(fieldToResolve) ? fieldToResolve : queryField,
customContext
)
)
)
Expand All @@ -839,7 +847,8 @@ async function resolveRecursive(
schema,
node,
gqlFields[fieldName],
fieldName
fieldName,
customContext
)
}
}
Expand All @@ -853,7 +862,8 @@ function resolveField(
schema,
node,
gqlField,
fieldName
fieldName,
customContext
) {
if (!gqlField?.resolve) {
return node[fieldName]
Expand All @@ -875,6 +885,7 @@ function resolveField(
schema,
schemaComposer,
nodeModel,
customContext,
}),
{
fieldName,
Expand Down

0 comments on commit 8aeae21

Please sign in to comment.