cometkim / gatsby-plugin-typegen

Let's give developers using GatsbyJS better DX with extreme type-safety

Home Page:https://www.gatsbyjs.org/packages/gatsby-plugin-typegen/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Types for gatsby-node.ts

jsbrain opened this issue · comments

Hi, I'm trying to get types generated for my root query in gatsby-node.ts.

Somehow this seems to not work. I used a query name (e.g. query GatsbyNode ...) as with all my other queries. All other type generation works fine, just not the gatsby-node.ts ones.

What am I doing wrong here

@jsbrain Not your fault. it's simply the plugin doesn't support gatsby-node yet.

I'm planning it for the next major version

Amazing, thx for your reply!
Do you already have an idea about the rough date?

I will stabilize the plugin implementation over the next week and release v3 that relies on the new lifecycle hook.

The investigation into schema customization will probably be v4. I'm using a lot of schema builders recently, so I know it's really nice to have, but honestly, I'm not sure which is the best way to do

Sounds good!
Meanwhile, do you have any idea how I could achieve getting those types anyhow?
Like can you think of any workaround I could apply or will I definitely have to time them myself? :D

I just ran into the same issue, now I know its not my fault 👍 Types for queries in gatsby-node would be awesome!

Is there a recommended work around for this in the interim? Can the queries be placed somewhere where types are generated and then be referenced from gatsby-node.ts?

As a temporary workaround I added the following codegen.yml to my project:

schema: http://localhost:8000/__graphql
documents:
  - ./gatsby-node.ts
generates:
  ./src/__generated__/gatsby-node-types.ts:
    plugins:
      - typescript
      - typescript-operations

Then I run a generate script defined as such:

"scripts": {
  "generate": "graphql-codegen",
}

This makes the type generation for the gatsby-*.ts files out-of-band, but it works for me for root queries until this plugin supports gatsby-node.ts.

As part of this RFC/Initiative gatsbyjs/gatsby#35420 I have a WIP PR up for an initial implementation. I have support for gatsby-node.ts working. I'd be really happy if you could follow that discussion and once I have a version published to try out, you could try it out. Thanks!