Skip to content
Closed
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
23 changes: 21 additions & 2 deletions packages/gatsby/src/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@
const _ = require(`lodash`)
const { GraphQLSchema, GraphQLObjectType } = require(`graphql`)

const apiRunner = require(`../utils/api-runner-node`)
const buildNodeTypes = require(`./build-node-types`)
const buildNodeConnections = require(`./build-node-connections`)
const { store } = require(`../redux`)
const { store, getNodes } = require(`../redux`)
const invariant = require(`invariant`)

async function getEnhancedGqlFields(typesGQL) {
const enhancedFields = await apiRunner(`enhanceSchema`, {
types: typesGQL,
allNodes: getNodes(),
traceId: `initial-enhanceSchema`,
})
return enhancedFields
}

module.exports = async () => {
const typesGQL = await buildNodeTypes()
let enhancedFields = {}
const enhancedFieldResults = await getEnhancedGqlFields(typesGQL)
Object.keys(enhancedFieldResults).forEach(key => {
enhancedFields = {
...enhancedFields,
...enhancedFieldResults[key],
}
})

const connections = buildNodeConnections(_.values(typesGQL))

// Pull off just the graphql node from each type object.
Expand All @@ -20,7 +39,7 @@ module.exports = async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: `RootQueryType`,
fields: { ...connections, ...nodes },
fields: { ...connections, ...nodes, ...enhancedFields },
}),
})

Expand Down
6 changes: 6 additions & 0 deletions packages/gatsby/src/utils/api-node-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,9 @@ exports.onPostBuild = true
* See gatsby-transformer-remark and gatsby-source-contentful for examples.
*/
exports.onPreExtractQueries = true

/**
* Run after the schema has been created but before it's dispatched allowing you
* to do cool enhancements e.g. union / interface stuff
*/
exports.enhanceSchema = true
Copy link
Contributor

Choose a reason for hiding this comment

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

Per the API specification, this should be setSchemaFields https://www.gatsbyjs.org/docs/api-specification/#operators

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, let's make this createSchemaFields. There's no reason to allow people to override the default fields so we should validate against that.