RFC: Allow overriding the `id` field
mxstbr opened this issue · comments
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](https://private-user-images.githubusercontent.com/7525670/297789324-3a6e55a2-28b3-4d5e-954f-81170b45b99f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI5NDgwNTYsIm5iZiI6MTcyMjk0Nzc1NiwicGF0aCI6Ii83NTI1NjcwLzI5Nzc4OTMyNC0zYTZlNTVhMi0yOGIzLTRkNWUtOTU0Zi04MTE3MGI0NWI5OWYucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgwNiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MDZUMTIzNTU2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9M2IwNTk2ZWIzZWQwZGU0NjRhYjJlMDg0NjYxMjFmNjU0Mzk4NjhiOThlMTFlZGMwOTU4MTM2NmZlMjFiMGNmYyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.uWZTsdh-SkWsGdy5qKzZqYa2f1Dy9OhGzLahOzUsk_w)
Proposed Solution
Allow overriding the id
field manually:
export const Story = node({
// …
fields: t => ({
id: t.exposeString('id', { nullable: false })
})
})
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
}
}>()
That would work, but override it for all types, right?
I have other types that do have numeric IDs.
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.
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.