StellateHQ / fuse

Fuse: The fastest way to build and query great APIs with TypeScript
https://fusedata.dev
MIT License
550 stars 13 forks source link

RFC: Allow overriding the `id` field #128

Closed mxstbr closed 9 months ago

mxstbr commented 9 months ago

Summary

I have a type called Story that has an ID that is always a string. However, Fuse auto-generates id: ID!, which (in TypeScript) converts to string | number. That, in turn, means that I'm getting errors from typechecking when I do story.id.replace(…) because .replace doesn't exist on number—but my ID will always be a string!

CleanShot 2024-01-18 at 07 14 48@2x

Proposed Solution

Allow overriding the id field manually:

export const Story = node({
  // …
  fields: t => ({
    id: t.exposeString('id', { nullable: false })
  })
})
JoviDeCroock commented 9 months ago

That should just require you to override the scalar though in your codegen/tada init

export const graphql = initGraphQLTada<{
  introspection: typeof introspection
  scalars: {
    ID: string
  }
}>()
mxstbr commented 9 months ago

That would work, but override it for all types, right?

I have other types that do have numeric IDs.

JoviDeCroock commented 9 months ago

How is that possible, the base64 identifier is always a string, no? So by extension when using ID! and hence encoding type:id to base64 it should always be a string. I guess the replace in general isn't the best idea here as this will be a base64 encoded global id which won't be the value you are looking for. ID in GraphQL isn't meant to be manipulated, what you can however do is send down an _id that exposes id if you want that but generally ID is meant to be opaque.

mxstbr commented 9 months ago

Oh right, I forgot that we make IDs globally unique while I was reworking an existing app that does story.id.replace—but of course, story.id used to be the non-opaque ID.