Gatsby: [gatsby-image] Bad schema typing for gatsby-images ?

Created on 23 Aug 2019  路  10Comments  路  Source: gatsbyjs/gatsby

Hi,

The TS defs for a fluid image is:

export interface FluidObject {
  aspectRatio: number
  src: string
  srcSet: string
  sizes: string
  base64?: string
  tracedSVG?: string
  srcWebp?: string
  srcSetWebp?: string
  media?: string
}

Yet if we look at a graphql image fragment like this one, we can notice they are actually incompatible:

image

The GraphQL schema says that all those attributes can be undefined (which does not really make sense to me, is it possible to have an image without at least an src?)

The problem is that if you try to generate typedefs from generic queries, you'll end up with incompatible typings and will have to find workarounds (like using "any").

Otherwise you'll get typescript errors like this:

[0] src/page/home/queries/Images.tsx(54,3): error TS2322: Type 'Pick<ImageSharpFluid, "src" | "sizes" | "srcSet" | "aspectRatio" | "tracedSVG" | "srcWebp" | "srcSetWebp">[]' is not assignable to type 'Pick<FluidObject, "media" | "src" | "base64" | "sizes" | "srcSet" | "tracedSVG" | "srcWebp" | "srcSetWebp">[]'.
[0]   Type 'Pick<ImageSharpFluid, "src" | "sizes" | "srcSet" | "aspectRatio" | "tracedSVG" | "srcWebp" | "srcSetWebp">' is not assignable to type 'Pick<FluidObject, "media" | "src" | "base64" | "sizes" | "srcSet" | "tracedSVG" | "srcWebp" | "srcSetWebp">'.

I suggest to make those types compatible so that people using tools like graphql-codegen can easily use gatsby images

imagemedia question or discussion

Most helpful comment

Stumbled upon the same problem today. I think this should be reconsidered..

All 10 comments

I experience the same problem with TypeScript generation.
Also relevant for gatsby-source-contentful, as well as the FixedObject.

Instead of weaken the types of gatsby-image, this should be addressed in the schema. As a result we can distinguish between fluid and fixed object and get a type error when used wrong.

Yeah, not really for weakening the types either :)

I'd rather have gatsby image being a graphql union type and being more precise on the schema typings, like union ImageSharpFluid = ImageSharpFluidWithSVG | ImageSharpFluidWebp | ...

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.

If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 馃挭馃挏

Not stale

Hey again!

It鈥檚 been 30 days since anything happened on this issue, so our friendly neighborhood robot (that鈥檚 me!) is going to close it.

Please keep in mind that I鈥檓 only a robot, so if I鈥檝e closed this issue in error, I鈥檓 HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks again for being part of the Gatsby community!

Can we re-open? Not stale.

Hey again!

It鈥檚 been 30 days since anything happened on this issue, so our friendly neighborhood robot (that鈥檚 me!) is going to close it.

Please keep in mind that I鈥檓 only a robot, so if I鈥檝e closed this issue in error, I鈥檓 HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks again for being part of the Gatsby community!

Stumbled upon the same problem today. I think this should be reconsidered..

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 馃挭馃挏

Not so spooky anymore :ghost:

Please review my pull request which should resolve the problem for Contentful and ImageSharp

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikestopcontinues picture mikestopcontinues  路  3Comments

rossPatton picture rossPatton  路  3Comments

timbrandin picture timbrandin  路  3Comments

kalinchernev picture kalinchernev  路  3Comments

dustinhorton picture dustinhorton  路  3Comments