Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] [schema] Allow registering custom field extensions #13623

Merged
merged 1 commit into from
Apr 30, 2019

Conversation

stefanprobst
Copy link
Contributor

@stefanprobst stefanprobst commented Apr 25, 2019

This is a WIP for allowing custom GraphQL field extensions. This is how it works:

The registerFieldExtension API accepts an extension consisting of a description, extension options, and a process function that should return a (partial) field config which will be merged into the existing field config. The extension can decide to wrap or overwrite an existing resolver.

Example:

exports.registerFieldExtension = ({ registerFieldExtension }) => {
  registerFieldExtension(`volume`, {
    description: `Adjust the volume.`,
    args: {
      loud: { type: `Boolean` },
    },
    // Return a new partial field config to extend the previous field config with.
    // Receives extension `args` and the current field config as parameters.
    process(defaults, prevFieldConfig) {
      return {
        type: `String`,
        args: {
          loud: { type: `Boolean` },
        },
        resolve(source, args, context, info) {
          const fieldValue = source[info.fieldName]
          const shouldUpperCase = args.loud != null ? args.loud : defaults.loud
          return shouldUpperCase ? fieldValue.toUpperCase() : fieldValue
        },
      }
    },
  })
}

It can then be used either with Gatsby Type Builders, or in SDL, e.g.:

exports.sourceNodes = ({ actions }) => {
  actions.createTypes(`
    type Site implements Node {
      siteMetadata: SiteMetadata
    }
    type SiteMetadata {
      title: String @volume(loud: true)
    }
  `)
}

Would love to get feedback on this!

@stefanprobst stefanprobst requested a review from a team as a code owner April 25, 2019 04:49
@stefanprobst stefanprobst mentioned this pull request Apr 26, 2019
23 tasks
@KartSriv KartSriv merged commit e40f6d2 into gatsbyjs:infer-fixing Apr 30, 2019
@KartSriv
Copy link
Contributor

Merged

@KyleAMathews
Copy link
Contributor

@KartSriv hey, any particular reason you merged that? It was WIP (work in progress) still so shouldn't have been merged. In the future, please don't merge PRs but just leave reviews and allow core maintainers to sign off on the work and do the merging.

@stefanprobst
Copy link
Contributor Author

Yes, this really shouldn't have been merged, as we first should have discussed if this is the way we want to go with extensions. Also, this shouldn't be part of the 2.4 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants