gatsbyjs / gatsby

The best React-based framework with performance, scalability and security built in.
https://www.gatsbyjs.com
MIT License
55.27k stars 10.31k forks source link

[1.0] Creating Interfaces/Unions on sourcing/transforming/typegen? #885

Closed danielfarrell closed 6 years ago

danielfarrell commented 7 years ago

Is there a way to add Union types of Interfaces from plugins?

KyleAMathews commented 7 years ago

So currently there's an API for extending the graphql types that Gatsby creates. You can see implementations in gatsby-typegen-remark and gatsby-typegen-sharp.

A simplified implementation looks like:

exports.extendNodeType = ({
  type,
}) => {
  if (type.name !== `TypeToModify`) {
    return {}
  }

  return {
    greeter: {
      type: GraphQLString,
      args: {
        name: {
          type: GraphQLString,
          defaultValue: "Bob",
        },
      },
      resolve(node, { name }) {
        return `Hi ${name}!`
      },
    },
  }
}

What this doesn't let you do is create entirely new types. Is that what you'd need? We could easily add a new API hook at https://github.com/gatsbyjs/gatsby/blob/77b15a947be84d24660a88fb30f7dce4aa2329cc/packages/gatsby/lib/schema/index.js#L17 that'd let you mutate things however you want before the schema is created.

danielfarrell commented 7 years ago

I think I would need to create new types for union/interface types, yeah. I could probably change things to live without them if it's not something of interest to others, but that API hook would interest me.

Should I take a stab at implementing it in a PR?

KyleAMathews commented 7 years ago

Should I take a stab at implementing it in a PR?

Yes please!

New API hooks are a one liner to add.

KyleAMathews commented 6 years ago

Open PR for this @ https://github.com/gatsbyjs/gatsby/pull/2990

cvharris commented 6 years ago

Is extendNodeType deprecated? Is the alternative recommended solution for this problem to use setFieldsOnGraphQLNodeType?

KyleAMathews commented 6 years ago

Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help!