Last active
August 27, 2019 05:19
-
-
Save rheinardkorf/66e9e62a4cf02c69541bdcaccd42c60e to your computer and use it in GitHub Desktop.
Use parent's resolver using custom field extension. FTW!!
This file contains hidden or 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
exports.createSchemaCustomization = ({ actions }) => { | |
const { createFieldExtension, createTypes } = actions; | |
createFieldExtension({ | |
name: `parent`, | |
description: `Proxy resolver from a parent's field.`, | |
args: { | |
from: `String!` | |
}, | |
extend(options, fieldConfig) { | |
return { | |
async resolve(source, args, context, info) { | |
const fieldName = options.from || info.from; | |
const parentNode = context.nodeModel.getNodeById({ | |
id: source.parent | |
}); | |
const type = info.schema.getType(parentNode.internal.type); | |
const resolver = type.getFields()[fieldName].resolve; | |
const result = await resolver(parentNode, args, context, { | |
fieldName | |
}); | |
return result; | |
} | |
}; | |
} | |
}); | |
createTypes(`type MdxCustom implements Node & Custom { | |
id: ID! | |
slug: String @proxy(from:"frontmatter.slug") | |
body: String @parent(from:"body") | |
}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment