Skip to content

Commit

Permalink
feat(gatsby): Allow specifying type on link extension (#16680)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst authored and sidharthachatterjee committed Aug 19, 2019
1 parent a4c97e3 commit f9bbb55
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ describe(`GraphQL field extensions`, () => {
internal: { type: `Test` },
somedate: `2019-09-13`,
otherdate: `2019-09-13`,
nested: {
onemoredate: `2019-09-26`,
},
},
{
id: `test3`,
Expand Down Expand Up @@ -1564,6 +1567,95 @@ describe(`GraphQL field extensions`, () => {
it.todo(`proxies to nested field on linked in node`)
})
})

it(`link extension accepts return type argument`, async () => {
dispatch(
createFieldExtension({
name: `reduce`,
args: {
to: `String!`,
},
extend(options, fieldConfig) {
return {
async resolve(source, args, context, info) {
const { getValueAt } = require(`../../../utils/get-value-at`)
const resolver =
fieldConfig.resolve || context.defaultFieldResolver
const fieldValue = await resolver(source, args, context, info)
if (fieldValue == null) return null
return getValueAt(fieldValue, options.to)
},
}
},
})
)
dispatch(
createTypes(`
type Test implements Node @dontInfer {
fieldFromParent: Date @link(from: "parent", on: "AnotherTest") @reduce(to: "date") @dateformat(formatString: "MM/DD/YYYY")
}
type AnotherTest implements Node @dontInfer {
fieldsFromChildren: [Date] @link(from: "children", on: "[Test!]!") @reduce(to: "somedate") @dateformat(formatString: "DD/MM/YYYY")
nestedFieldsFromChildren: [Date] @link(from: "children", on: "[Test!]!") @reduce(to: "nested.onemoredate") @dateformat(formatString: "DD/MM/YYYY")
}
`)
)
const query = `
{
allTest {
nodes {
id
fieldFromParent
}
}
allAnotherTest {
nodes {
id
fieldsFromChildren
nestedFieldsFromChildren
}
}
}
`
const results = await runQuery(query)
const expected = {
allTest: {
nodes: [
{
id: `test1`,
fieldFromParent: `01/01/2019`,
},
{
id: `test2`,
fieldFromParent: `Invalid date`,
},
{
id: `test3`,
fieldFromParent: null,
},
{
id: `test4`,
fieldFromParent: null,
},
],
},
allAnotherTest: {
nodes: [
{
id: `test5`,
fieldsFromChildren: [`01/09/2019`],
nestedFieldsFromChildren: [`30/07/2019`],
},
{
id: `test6`,
fieldsFromChildren: [`13/09/2019`],
nestedFieldsFromChildren: [`26/09/2019`],
},
],
},
}
expect(results).toEqual(expected)
})
})

const buildSchema = async () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/gatsby/src/schema/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ const builtInFieldExtensions = {
defaultValue: `id`,
},
from: `String`,
on: `String`,
},
extend(args, fieldConfig) {
extend(args, fieldConfig, schemaComposer) {
const type = args.on && schemaComposer.typeMapper.getWrapped(args.on)
return {
resolve: link(args, fieldConfig),
resolve: link({ ...args, type }, fieldConfig),
}
},
},
Expand Down Expand Up @@ -225,7 +227,7 @@ const processFieldExtensions = ({
const prevFieldConfig = typeComposer.getFieldConfig(fieldName)
typeComposer.extendField(
fieldName,
extend(extensions[name], prevFieldConfig)
extend(extensions[name], prevFieldConfig, schemaComposer)
)
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const link = (options = {}, fieldConfig) => async (
return fieldValue
}

const returnType = getNullableType(info.returnType)
const returnType = getNullableType(options.type || info.returnType)
const type = getNamedType(returnType)

if (options.by === `id`) {
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/utils/get-value-at.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const getValueAt = (obj, selector) => {
}

const get = (obj, selectors) => {
if (Array.isArray(obj)) return getArray(obj, selectors)
const [key, ...rest] = selectors
const value = obj[key]
if (!rest.length) return value
Expand Down

0 comments on commit f9bbb55

Please sign in to comment.