Skip to content

Commit

Permalink
Revert "Allow registering field extensions (#13623)" (#13735)
Browse files Browse the repository at this point in the history
This reverts commit e40f6d2.
  • Loading branch information
stefanprobst authored Apr 30, 2019
1 parent e40f6d2 commit 0a53e22
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 329 deletions.
84 changes: 84 additions & 0 deletions packages/gatsby/src/schema/add-field-resolvers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const _ = require(`lodash`)
const { defaultFieldResolver } = require(`graphql`)
const { dateResolver } = require(`./types/date`)
const { link, fileByPath } = require(`./resolvers`)

export const addFieldResolvers = ({
schemaComposer,
typeComposer,
parentSpan,
}) => {
typeComposer.getFieldNames().forEach(fieldName => {
let field = typeComposer.getField(fieldName)

const extensions = typeComposer.getFieldExtensions(fieldName)
if (
!field.resolve &&
extensions.addResolver &&
_.isObject(extensions.addResolver)
) {
const options = extensions.addResolver.options || {}
switch (extensions.addResolver.type) {
case `dateformat`: {
addDateResolver({
typeComposer,
fieldName,
options,
})
break
}
case `link`: {
typeComposer.extendField(fieldName, {
resolve: link({ from: options.from, by: options.by }),
})
break
}
case `fileByRelativePath`: {
typeComposer.extendField(fieldName, {
resolve: fileByPath({ from: options.from }),
})
break
}
}
}

if (extensions.proxyFrom) {
// XXX(freiksenet): get field again cause it will be changed because of above
field = typeComposer.getField(fieldName)
const resolver = field.resolve || defaultFieldResolver
typeComposer.extendField(fieldName, {
resolve: (source, args, context, info) =>
resolver(source, args, context, {
...info,
fieldName: extensions.proxyFrom,
}),
})
}
})
return typeComposer
}

const addDateResolver = ({
typeComposer,
fieldName,
options: { formatString, locale },
}) => {
const field = typeComposer.getField(fieldName)

let fieldConfig = {
resolve: dateResolver.resolve,
}
if (!field.args || _.isEmpty(field.args)) {
fieldConfig.args = {
...dateResolver.args,
}
if (formatString) {
fieldConfig.args.formatString.defaultValue = formatString
}
if (locale) {
fieldConfig.args.locale.defaultValue = locale
}
}

typeComposer.extendField(fieldName, fieldConfig)
}
2 changes: 0 additions & 2 deletions packages/gatsby/src/schema/context.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
const { LocalNodeModel } = require(`./node-model`)
const { fieldExtensions } = require(`./extensions`)

const withResolverContext = (context, schema) => {
const nodeStore = require(`../db/nodes`)
const createPageDependency = require(`../redux/actions/add-page-dependency`)

return {
...context,
fieldExtensions,
nodeModel: new LocalNodeModel({
nodeStore,
schema,
Expand Down
210 changes: 0 additions & 210 deletions packages/gatsby/src/schema/extensions/index.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/infer/__tests__/infer.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ describe(`GraphQL type inference`, () => {
`
with_space
with_hyphen
with_resolver(formatString: "DD.MM.YYYY")
with_resolver(formatString:"DD.MM.YYYY")
_123
_456 {
testingTypeNameCreation
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/infer/__tests__/merge-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ describe(`merges explicit and inferred type definitions`, () => {
expect(inferDate.resolve).toBeDefined()
})

it.todo(`adds explicit resolvers through extensions`)
it(`adds explicit resolvers through extensions`, async () => {})

it(`honors array depth when merging types`, async () => {
const typeDefs = `
Expand Down
10 changes: 5 additions & 5 deletions packages/gatsby/src/schema/infer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const addInferredTypes = ({
: true
if (runInfer) {
if (!typeComposer.hasInterface(`Node`)) {
noNodeInterfaceTypes.push(typeName)
noNodeInterfaceTypes.push(typeComposer)
}
typesToInfer.push(typeComposer)
}
Expand All @@ -46,16 +46,16 @@ const addInferredTypes = ({
})

if (noNodeInterfaceTypes.length > 0) {
noNodeInterfaceTypes.forEach(typeName => {
noNodeInterfaceTypes.forEach(type => {
report.warn(
`Type \`${typeName}\` declared in \`createTypes\` looks like a node, ` +
`Type \`${type}\` declared in \`createTypes\` looks like a node, ` +
`but doesn't implement a \`Node\` interface. It's likely that you should ` +
`add the \`Node\` interface to your type def:\n\n` +
`\`type ${typeName} implements Node { ... }\`\n\n` +
`\`type ${type} implements Node { ... }\`\n\n` +
`If you know that you don't want it to be a node (which would mean no ` +
`root queries to retrieve it), you can explicitly disable inference ` +
`for it:\n\n` +
`\`type ${typeName} @dontInfer { ... }\``
`\`type ${type} @dontInfer { ... }\``
)
})
report.panic(`Building schema failed`)
Expand Down
10 changes: 8 additions & 2 deletions packages/gatsby/src/schema/schema-composer.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const { SchemaComposer, GraphQLJSON } = require(`graphql-compose`)
const { getNodeInterface } = require(`./types/node-interface`)
const { GraphQLDate } = require(`./types/date`)
const { addDirectives } = require(`./extensions`)
const {
InferDirective,
DontInferDirective,
AddResolver,
} = require(`./types/directives`)

const createSchemaComposer = () => {
const schemaComposer = new SchemaComposer()
getNodeInterface({ schemaComposer })
schemaComposer.addAsComposer(GraphQLDate)
schemaComposer.addAsComposer(GraphQLJSON)
addDirectives({ schemaComposer })
schemaComposer.addDirective(InferDirective)
schemaComposer.addDirective(DontInferDirective)
schemaComposer.addDirective(AddResolver)
return schemaComposer
}

Expand Down
Loading

0 comments on commit 0a53e22

Please sign in to comment.