cometkim / gatsby-plugin-typegen

Let's give developers using GatsbyJS better DX with extreme type-safety
https://www.gatsbyjs.org/packages/gatsby-plugin-typegen/
MIT License
204 stars 25 forks source link

Plugin option types not accurate/outdated (?) or bug regarding emitSchema #161

Closed kije closed 2 years ago

kije commented 3 years ago

First of all, thanks for this amazing plugin!

I noticed that either the types of this plugin's options are inaccurate/wrong in regards to emitSchema, or there is a bug in the plugin.

According to the plugin option types for emitSchema, I should be able to configure the plugin as follows

{
  resolve: `gatsby-plugin-typegen`,
  options: {
    outputPath: "gatsby-graphql-types.d.ts",
    emitSchema: {
      "./schema-gatsby.graphql": {
        format: "sdl",
        commentDescriptions: false
      }
    }
  }
}

However, if I use this configuration, no schema file gets emmited. Only if I use:

{
  resolve: `gatsby-plugin-typegen`,
  options: {
    outputPath: "gatsby-graphql-types.d.ts",
    emitSchema: {
      "./schema-gatsby.graphql": true
    }
  }
}

then a schema file gets emitted.

I think the reason for this seems to be the logic here: https://github.com/cometkim/gatsby-plugin-typegen/blob/7ee59cd5040e311bb11378656169e1e6e6878000/plugin/src/plugin-utils.ts#L73-L84

While validating/sanitizing the emitSchema option, only the options === true case is handled. The case where options is an object however is not handled and thus doesn't get added to the sanitized version of emitSchema.

I'm not sure if this is intentional (as the docs of the plugin only show usage of the true case) and the types of the plugin options are simply not accurate/outdated, of if this is a bug in the options sanitation logic of the plugin.

cometkim commented 2 years ago

Thanks for reporting. This should be fixed in v3.