Skip to content

graphqlTypegen generating wrong Type definitions for gatsby-plugin-image + gatsby-source-contentful #36243

@Mike-Logit

Description

@Mike-Logit

Preliminary Checks

Description

TL;DR The TypeScript generated by Gatsby using the graphqlTypegen setting defined in gatsby-config.ts is wrong.

I'm using the following package versions to query gatsbyImageData from Contentful using TypeScript:

  "dependencies": {
    "gatsby": "^4.19.2",
    "gatsby-plugin-image": "^2.19.0",
    "gatsby-plugin-sharp": "^4.19.0",
    "gatsby-source-contentful": "^7.17.0",
    "gatsby-transformer-sharp": "^4.19.0",
    "react": "^18.1.0",
    "react-dom": "^18.1.0"
  },
  "devDependencies": {
    "@types/node": "^17.0.45",
    "@types/react": "^18.0.15",
    "@types/react-dom": "^18.0.6",
    "typescript": "^4.7.4"
  }

I'm also using graphqlTypegen declared in gatsby-config.ts to generate types for my Contentful assets to add TypeScript support.

I query images from Contentful using the following example query (by following the migration guide and the relevant plugin documentation):

  const { allContentfulProduct } = useStaticQuery<Queries.Query>(graphql`
    query {
      allContentfulProduct(limit: 1) {
        nodes {
          image {
            gatsbyImageData(height: 260, width: 380, layout: CONSTRAINED)
          }
        }
      }
    }
  `);

The problem is that the following use of the GatsbyImage component does not work with this product image I queried for, despite the gatsbyImage value being the correct TypeScript type being supplied for the image property:

<GatsbyImage
  alt="gatsbyImage"
  image={productImageAsset.gatsbyImage}
/>

And using the gatsbyImageData property available to me works but is not the correct TypeScript type:

<GatsbyImage
  alt="gatsbyImageData"
  image={productImageAsset.gatsbyImageData} // I'm seeing an error reported here but the image shows as expected
/>

The generated TypeScript looks like this:

type ContentfulAsset = ContentfulReference & Node & RemoteFile & {
  // omitted other properties for brevity...

  /** Data used in the <GatsbyImage /> component. See https://gatsby.dev/img for more info. */
  readonly gatsbyImage: Maybe<Scalars['GatsbyImageData']>;
  readonly gatsbyImageData: Maybe<Scalars['JSON']>;

 // omitted other properties for brevity...
}

And the typing definition for the "image" property used by the GatsbyImage component looks like this:

export interface GatsbyImageProps extends ... {
     // omitted other properties for brevity...

    image: IGatsbyImageData;
  
     // omitted other properties for brevity...
}

So, TypeScript is telling me to use "gatsbyImage" (which results in no image being displayed) instead of "gatsbyImageData" (which shows my image and the plugin works as expected):

image

So I believe the TypeScript being generated is wrong for this scenario as I have to ignore the typing errors for it to work.

Reproduction Link

https://codesandbox.io/s/vigilant-golick-ekfgim?file=/src/pages/index.tsx

Steps to Reproduce

  1. Open the CodeSandbox link or VSCode
  2. Install dependencies from package.json using npm i if using VSCode. CodeSandbox should do this automatically.
  3. If using VSCode, run gatsby develop
  4. You should see an error with a red squiggly line below image={productImageAsset.gatsbyImageData} despite it working correctly due to a typing issue from the generated TypeScript.

Expected Result

The generated TypeScript should be correct so that you can pass gatsbyImageData in the ContentfulAsset to the GatsbyImage component without a TypeScript error due to the wrong types being generated.

Actual Result

You need to use gatsbyImageData for the image to show even though it is not of type IGatsbyImageData and the GatsbyImage component expects a type of IGatsbyImageData passed to the image property, so this is wrong:

type ContentfulAsset = ContentfulReference & Node & RemoteFile & {
  // omitted other properties for brevity...

  /** Data used in the <GatsbyImage /> component. See https://gatsby.dev/img for more info. */
  readonly gatsbyImage: Maybe<Scalars['GatsbyImageData']>; // DOES NOT WORK
  readonly gatsbyImageData: Maybe<Scalars['JSON']>; // WORKS!! But not of type IGatsbyImageData...

 // omitted other properties for brevity...
}

Environment

System:
    OS: Windows 10 10.0.19044
    CPU: (8) x64 Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
  Binaries:
    Node: 18.4.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.12.1 - C:\Program Files\nodejs\npm.CMD
  Languages:
    Python: 3.9.13
  Browsers:
    Edge: 44.19041.1266.0
  npmPackages:
    gatsby: ^4.19.2 => 4.19.2 
    gatsby-plugin-image: ^2.19.0 => 2.19.0 
    gatsby-plugin-sharp: ^4.19.0 => 4.19.0 
    gatsby-source-contentful: ^7.17.0 => 7.17.0 
    gatsby-transformer-sharp: ^4.19.0 => 4.19.0

Config Flags

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: confirmedIssue with steps to reproduce the bug that’s been verified by at least one reviewer.topic: TypeScriptIssues and PRs related to TS in general, public typings or gatsby-plugin-typescripttopic: source-contentfulRelated to Gatsby's integration with Contentfultype: bugAn issue or pull request relating to a bug in Gatsby

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions