Skip to content

Commit

Permalink
feat(gatsby): Allow proxying field values from nested fields (#16149)
Browse files Browse the repository at this point in the history
* Allow proxying field values from nested fields

* Lint'n'Loki

* Test using args.from in custom field extension

* Add some extra safety

* Pass options via info

* Lint

* Add test example for proxying to parent node

* Add test todos for proxying to fields on other nodes

* Remove unnecessary restriction
  • Loading branch information
stefanprobst authored and freiksenet committed Aug 12, 2019
1 parent 259c7d1 commit d2128ab
Show file tree
Hide file tree
Showing 10 changed files with 444 additions and 83 deletions.
9 changes: 8 additions & 1 deletion docs/docs/schema-customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,12 @@ exports.createSchemaCustomization = ({ actions }) => {
sanitize: "Boolean",
},
resolve(source, args, context, info) {
const fieldValue = source[info.fieldName]
const fieldValue = context.defaultFieldResolver(
source,
args,
context,
info
)
const shouldSanitize =
args.sanitize != null ? args.sanitize : options.sanitize
const processor = remark().use(html, { sanitize: shouldSanitize })
Expand Down Expand Up @@ -640,6 +645,8 @@ If multiple field extensions are added to a field, resolvers are processed in th
first a custom resolver added with `createTypes` (or `createResolvers`) runs, then field
extension resolvers execute from left to right.

Finally, note that in order to get the current fieldValue, we use `context.defaultFieldResolver`.

## createResolvers API

While it is possible to directly pass `args` and `resolvers` along the type
Expand Down
13 changes: 7 additions & 6 deletions packages/gatsby/src/schema/__tests__/build-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
buildUnionType,
buildInterfaceType,
} = require(`../types/type-builders`)
const withResolverContext = require(`../context`)
require(`../../db/__tests__/fixtures/ensure-loki`)()

const nodes = require(`./fixtures/node-model`)
Expand Down Expand Up @@ -913,7 +914,7 @@ describe(`Build schema`, () => {
fields[`name`].resolve(
{ name: `Mikhail` },
{ withHello: true },
{},
withResolverContext({}, schema),
{
fieldName: `name`,
}
Expand All @@ -923,7 +924,7 @@ describe(`Build schema`, () => {
fields[`name`].resolve(
{ name: `Mikhail` },
{ withHello: false },
{},
withResolverContext({}, schema),
{
fieldName: `name`,
}
Expand Down Expand Up @@ -959,7 +960,7 @@ describe(`Build schema`, () => {
fields[`name`].resolve(
{ name: `Mikhail` },
{ withHello: true },
{},
withResolverContext({}, schema),
{
fieldName: `name`,
}
Expand All @@ -969,7 +970,7 @@ describe(`Build schema`, () => {
fields[`name`].resolve(
{ name: `Mikhail` },
{ withHello: false },
{},
withResolverContext({}, schema),
{
fieldName: `name`,
}
Expand Down Expand Up @@ -1105,7 +1106,7 @@ describe(`Build schema`, () => {
await fields[`date`].resolve(
{ date: new Date(2019, 10, 10) },
{ formatString: `YYYY` },
{},
withResolverContext({}, schema),
{
fieldName: `date`,
}
Expand All @@ -1115,7 +1116,7 @@ describe(`Build schema`, () => {
await fields[`date`].resolve(
{ date: new Date(2010, 10, 10) },
{ formatString: `YYYY` },
{},
withResolverContext({}, schema),
{
fieldName: `date`,
}
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/schema/context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { LocalNodeModel } = require(`./node-model`)
const { defaultFieldResolver } = require(`./resolvers`)

const withResolverContext = (context, schema, customContext) => {
const nodeStore = require(`../db/nodes`)
Expand All @@ -7,6 +8,7 @@ const withResolverContext = (context, schema, customContext) => {
return {
...context,
...customContext,
defaultFieldResolver,
nodeModel: new LocalNodeModel({
nodeStore,
schema,
Expand Down
Loading

0 comments on commit d2128ab

Please sign in to comment.