简体   繁体   English

用于查询/变异的 GraphQl 打字稿类?

[英]GraphQl typescript class to query/mutation?

Is something like this possible where we create a graphql query out of a typescript class potentially using reflection?在我们可能使用反射从打字稿类中创建 graphql 查询的情况下,这样的事情是否可能?

export class GraphQlMagicGenerator {

    fetch(query: string, args: any) {

    }
}
export class PatientsQuery extends GraphQlMagicGenerator {
    constructor(patientId: number) {
        super()

        super.fetch("patient", {
            id: patientId
        })
    }

id:()=> number
name:()=> string
age:()=> number
}

// ^ behind the scenes generates a query for patient with an id argument and fills in id, name and age

Don't do this.不要这样做。 because因为

  1. Every query needs to develop a class to match it.每个查询都需要开发一个类来匹配它。 class will affect the size of the target code. class 会影响目标代码的大小。

  2. GraphQL is highly flexible and dynamic, its query parameters determine which fields need to be queried. GraphQL 具有高度的灵活性和动态性,它的查询参数决定了需要查询哪些字段。 Even for the same query, different parameters will lead to different return types.即使是同一个查询,不同的参数也会导致不同的返回类型。 It will be particularly tiring if you try to manually define the return type for every query like this.如果您尝试像这样为每个查询手动定义返回类型,那将特别累人。

  3. The data types corresponding to different queries contain some of the same information, there will be a lot of redundancy in the code.不同查询对应的数据类型包含一些相同的信息,代码中会有很多冗余。

I suggest my framework, it brings the magical development experience for GraphQL Client.我推荐我的框架,它为 GraphQL Client 带来了神奇的开发体验。

https://github.com/babyfish-ct/graphql-ts-client https://github.com/babyfish-ct/graphql-ts-client

  1. A code generator is supported to download the schema information from server and generate client TypeScript files.支持代码生成器从服务器下载架构信息并生成客户端 TypeScript 文件。 It won't generate "class" for functionalities without logic so that the size of target code will not be affected.它不会为没有逻辑的功能生成“类”,因此不会影响目标代码的大小。

  2. It allows you to write strongly-typed GraphQL request, not only arguments, but also a strongly typed tree structure to describe which fields should be queried.它允许你编写强类型的 GraphQL 请求,不仅是参数,还有一个强类型的树结构来描述应该查询哪些字段。

  3. Be different with other similar frameworks, it does not require user to define all possible queries in advance, for each specific query, it can infer the type of returned data according to the strongly type request .与其他类似框架不同,它不需要用户预先定义所有可能的查询,对于每个特定的查询,它可以根据强类型请求推断返回数据的类型

like this像这样在此处输入图片说明

You should be doing the opposite: defining your schema in .graphql and then codegening your TypeScript types from there.你应该做相反的事情:在.graphql定义你的架构,然后从那里编码你的 TypeScript 类型。 I am using gql2ts (though there are others.) Then just create a script as part of your build phase.我正在使用gql2ts (尽管还有其他的。)然后只需创建一个脚本作为构建阶段的一部分。

You can customize the codegen'd portion to fit your team's particular style as well.您也可以自定义代码生成部分以适合您团队的特定风格。 For example, I've removed namespacing, overriden enum generation, and marked the generated types all as readonly .例如,我删除了命名空间,覆盖了enum生成,并将生成的类型全部标记为readonly But your schema is your API and so it should define your types.但是您的架构就是您的 API,因此它应该定义您的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM