Skip to content
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

Open
zhigang1992 opened this issue Apr 19, 2020 · 2 comments
Open

Strong typed return value #31

zhigang1992 opened this issue Apr 19, 2020 · 2 comments

Comments

@zhigang1992
Copy link

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.

@zhigang1992
Copy link
Author

zhigang1992 commented Apr 19, 2020

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.

@helios1138
Copy link
Owner

@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 graphql-zeus managed to solve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants