-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add GraphQL client type utilities to hydrogen-codegen #1584
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0327165
Extract type codegen utilities from Storefront Client
frandiox 5fc1726
Move codegen type utilities to hydrogen-codegen package
frandiox 22ba6b4
Changesets
frandiox e203014
Rename type
frandiox d90e76c
Make rest parameter optional only if other properties are not required
frandiox 7cabd87
Fix ClientReturn
frandiox d471849
Fix issues with optional variables
frandiox 0633b11
Use GenericVariables
frandiox 2edc04e
Fix variables
frandiox b5ac9ca
Add tests for client types
frandiox 27e4b39
Merge branch 'main' into fd-codegen-client-type-refactor
frandiox 07ed67c
Run type tests properly in Vitest v1
frandiox 764ef2a
Simplify types and add comments
frandiox 6c507c9
Simplify types in storefront client
frandiox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@shopify/hydrogen-codegen': minor | ||
--- | ||
|
||
Export type utilities to create GraphQL clients that can consume the types generated by this package. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* This file has utilities to create GraphQL clients | ||
* that consume the types generated by the Hydrogen preset. | ||
*/ | ||
|
||
import type {ExecutionArgs} from 'graphql'; | ||
import type {SetOptional, IsNever} from 'type-fest'; | ||
|
||
/** | ||
* A generic type for `variables` in GraphQL clients | ||
*/ | ||
export type GenericVariables = ExecutionArgs['variableValues']; | ||
|
||
/** | ||
* Use this type to make parameters optional in GraphQL clients | ||
* when no variables need to be passed. | ||
*/ | ||
export type EmptyVariables = {[key: string]: never}; | ||
|
||
/** | ||
* GraphQL client's generic operation interface. | ||
*/ | ||
interface CodegenOperations { | ||
// Real example: | ||
// '#graphql\n query TestQuery { test }': {return: R; variables: V}; | ||
// -- | ||
// However, since the interface passed as CodegenOperations might sitll be empty | ||
// (if the user hasn't generated the code yet), we use `any` here. | ||
[key: string]: any; | ||
} | ||
|
||
/** | ||
* Used as the return type for GraphQL clients. It picks | ||
* the return type from the generated operation types. | ||
* @example | ||
* graphqlQuery: (...) => Promise<ClientReturn<...>> | ||
* graphqlQuery: (...) => Promise<{data: ClientReturn<...>}> | ||
*/ | ||
export type ClientReturn< | ||
GeneratedOperations extends CodegenOperations, | ||
RawGqlString extends string, | ||
OverrideReturnType extends any = never, | ||
> = IsNever<OverrideReturnType> extends true | ||
? // Nothing passed to override the return type | ||
RawGqlString extends keyof GeneratedOperations | ||
? // Known query, use generated return type | ||
GeneratedOperations[RawGqlString]['return'] | ||
: // Unknown query, return 'any' to avoid red squiggly underlines in editor | ||
any | ||
: // Override return type is passed, use it | ||
OverrideReturnType; | ||
|
||
/** | ||
* Checks if the generated variables for an operation | ||
* are optional or required. | ||
*/ | ||
export type IsOptionalVariables< | ||
VariablesParam, | ||
OptionalVariableNames extends string = never, | ||
// The following are just extracted repeated types, not parameters: | ||
VariablesWithoutOptionals = Omit<VariablesParam, OptionalVariableNames>, | ||
> = VariablesWithoutOptionals extends EmptyVariables | ||
? // No expected required variables, object is optional | ||
true | ||
: GenericVariables extends VariablesParam | ||
? // We don't have information about the variables, so we assume object is optional | ||
true | ||
: Partial<VariablesWithoutOptionals> extends VariablesWithoutOptionals | ||
? // All known variables are optional, object is optional | ||
true | ||
: // Some known variables are required, object is required | ||
false; | ||
|
||
/** | ||
* Used as the type for the GraphQL client's variables. It checks | ||
* the generated operation types to see if variables are optional. | ||
* @example | ||
* graphqlQuery: (query: string, param: ClientVariables<...>) => Promise<...> | ||
* Where `param` is required. | ||
*/ | ||
export type ClientVariables< | ||
GeneratedOperations extends CodegenOperations, | ||
RawGqlString extends string, | ||
OptionalVariableNames extends string = never, | ||
VariablesKey extends string = 'variables', | ||
// The following are just extracted repeated types, not parameters: | ||
GeneratedVariables = RawGqlString extends keyof GeneratedOperations | ||
? SetOptional< | ||
GeneratedOperations[RawGqlString]['variables'], | ||
Extract< | ||
keyof GeneratedOperations[RawGqlString]['variables'], | ||
OptionalVariableNames | ||
> | ||
> | ||
: GenericVariables, | ||
VariablesWrapper = Record<VariablesKey, GeneratedVariables>, | ||
> = IsOptionalVariables<GeneratedVariables, OptionalVariableNames> extends true | ||
? // Variables are all optional: object wrapper is optional | ||
Partial<VariablesWrapper> | ||
: // Some variables are required: object wrapper is required | ||
VariablesWrapper; | ||
|
||
/** | ||
* Similar to ClientVariables, but makes the whole wrapper optional: | ||
* @example | ||
* graphqlQuery: (query: string, ...params: ClientVariablesInRestParams<...>) => Promise<...> | ||
* Where the first item in `params` might be optional depending on the query. | ||
*/ | ||
export type ClientVariablesInRestParams< | ||
GeneratedOperations extends CodegenOperations, | ||
RawGqlString extends string, | ||
OtherParams extends Record<string, any> = {}, | ||
OptionalVariableNames extends string = never, | ||
// The following are just extracted repeated types, not parameters: | ||
ProcessedVariables = OtherParams & | ||
ClientVariables<GeneratedOperations, RawGqlString, OptionalVariableNames>, | ||
> = Partial<OtherParams> extends OtherParams | ||
? // No required keys in OtherParams: keep checking | ||
IsOptionalVariables< | ||
GeneratedOperations[RawGqlString]['variables'], | ||
OptionalVariableNames | ||
> extends true | ||
? // No required keys in OtherParams and variables are also optional: rest param is optional | ||
[ProcessedVariables?] | ||
: // No required keys in OtherParams but variables are required: rest param is required | ||
[ProcessedVariables] | ||
: // There are required keys in OtherParams: rest param is required | ||
[ProcessedVariables]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
patch
?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.
It's OK to have
minor
in hydrogen-codegen. Since this is a new feature, it should be minor I think?