Giter Site home page Giter Site logo

Comments (2)

helios1138 avatar helios1138 commented on May 23, 2024 3

@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.

from graphql-typed-client.

zhigang1992 avatar zhigang1992 commented on May 23, 2024

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.

from graphql-typed-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.