-
Notifications
You must be signed in to change notification settings - Fork 227
admin-graphql-api-utilities: Add Gid
type
#2622
Conversation
export type Gid< | ||
Namespace extends string, | ||
Type extends string, | ||
> = `gid://${Namespace}/${Type}/${number | string}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is great. It leverages Typescript really well to improve type-safety.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the ShopifyGid
idea makes sense since the exported composeGid
is using 'shopify'
as the namespace.
I don't have a strong feeling about the isGidFactory
and isGid
ideas.
I'll add these in a separate commit. If we decide we don't want them, that commit can just be reverted before merging. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks great! Just some small housekeeping needed:
Can you pease add a changeset using yarn changeset
to generate a changelog entry. As you're adding new features, this is a minor change.
As you're adding some type strictness, you might be interested in adding type tests (this will need a rebase atop #2652 to get it working with zero extra effort). Not critical, but might be a neat way of proving the strictness your adding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've took the liberty of adjusting the changeset to be more descriptive. Thanks for the PR!
Description
This PR adds a
Gid
type that can be used by consumers ofcomposeGid
andcomposeGidFactory
to provide strongly-typed GID values. This makes it a lot less likely for one GID type to accidentally be used in place of another.For example, with the current API, this is perfectly fine:
However, with this change we can easily make this catch the typo as a compile-time error:
Additionally, since
Gid
can implicitly cast down tostring
, this change should only affect those who opt into this behavior.Possible Additions
We could also introduce a Shopify-specific
Gid
type to reduce the amount of typing1 needed for users using this with the Shopify API:We could also include type guards like
isGidFactory
andisGid
to take in a string and check if it's the desiredGid
type:Code Sample
Any thoughts on these?
Footnotes
By "reduce the amount of typing", I'm referring to reducing the amount of keystrokes and potential typos in non-autocomplete scenarios. From my experience, IDEs won't autocomplete strings like
'shopify'
, but they will autocomplete types likeShopifyGid
(normally even with just a couple characters likeSG
). ↩