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): pass custom graphql context provided by createResolverContext to materialization executor #36552

Merged
merged 1 commit into from
Sep 7, 2022
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
46 changes: 46 additions & 0 deletions packages/gatsby/src/schema/__tests__/node-model.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is main fix - rest is just "prop drilling" to get it here

}),
{
fieldName,
Expand Down