-
Notifications
You must be signed in to change notification settings - Fork 18
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
Strong typed return value #31
Comments
Here is a gist of it export interface UserRequest {
name?: boolean
age?: boolean
blogs?:
| [{ first?: number }, BlogRequest]
| BlogRequest
__typename?: boolean
__scalar?: boolean
}
export interface UserOutput {
name: String
age: number
blogs: BlogOutput[];
__typename: 'UserOutput'
}
export interface BlogRequest {
title?: boolean;
article?: boolean;
createdAt?: boolean;
__typename?: boolean
__scalar?: boolean
}
export interface BlogOutput {
title: string;
article: string;
createdAt: string;
__typename: 'BlogOutput'
}
type Anify<T> = { [P in keyof T]?: any };
type LastMapTypeSRCResolver<Output, Filter> = Output extends undefined
? undefined
: Output extends Array<infer AR>
? LastMapTypeSRCResolver<AR, Filter>[]
: Filter extends boolean
? Output
: MapType<Output, Filter>;
type MapType<Output extends Anify<Filter>, Filter> = Output extends boolean ? boolean : {
[Key in keyof Filter]: Filter[Key] extends [any, infer PAYLOAD]
? LastMapTypeSRCResolver<Output[Key], PAYLOAD>
: LastMapTypeSRCResolver<Output[Key], Filter[Key]>;
}
type Execute<Request, Output> = <Z extends Request>(subFields: Z) => MapType<Output, Z>
let execute: Execute<UserRequest, UserOutput>
const a = execute({ name: true, blogs: {title: true} });
a.name;
a.blogs[0].title;
a.age // type script complains But I don't have enough knowledge on this code base to create a PR yet. |
@zhigang1992 I tried something similar in the past but was not able to make it work for union/interface types and didn't have the time to work on it since. I will take a look how |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
graphql-zeus was able to strong type return result will using pretty much the same syntax as this library. Would be nice to have that support as well.
The text was updated successfully, but these errors were encountered: