• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

dangcuuson/graphql-schema-typescript: Generate TypeScript from GraphQL's sch ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

dangcuuson/graphql-schema-typescript

开源软件地址(OpenSource Url):

https://github.com/dangcuuson/graphql-schema-typescript

开源编程语言(OpenSource Language):

TypeScript 90.0%

开源软件介绍(OpenSource Introduction):

A GraphQL utility to generate Typescript from Schema Definition

This project is inspired by Apollo-codegen. Currently apollo-codegen only generate TypeScripts for GraphQL Client. The shape of the generated type is based on the client's query strings.

This module aim to do the Server counterpart: from a Schema Definition, generate the types to make it type-safed when developing GraphQL server (mainly resolvers)

Compatibility

graphql-schema-typescript graphql
1.5 ^14.0.0
1.4 0.11~0.13

Features

Generate Typescript from Schema Definition (1-1 mapping from GQL type to TypeScript)

Convert GraphQL description into JSDoc, include deprecated directive

Generate TypeScripts to support writing resolvers

VSCode extension (credit to @liyikun)

Usage

import { generateTypeScriptTypes } from 'graphql-schema-typescript';

generateTypeScriptTypes(schema, outputPath, options)
    .then(() => {
        console.log('DONE');
        process.exit(0);
    })
    .catch(err =>{
        console.error(err);
        process.exit(1);
    });

You can then bootstrap this script on your dev server, or use something like ts-node to execute it directly

CLIs

  • You can also use CLIs to generate your TypeScript instead of writing code
  • Instead of providing a schema, you need to provide a folder that contains your type definitions, written in .gql or .graphql extensions
  • Use graphql-schema-typescript generate-ts --help for more details

Type Resolvers

The file generated will have some types that can make it type-safed when writing resolver:

  • Args type in your resolve function is now type-safed
  • Parent type and resolve result is default to any, but could be overwritten in your code

For example, if you schema is like this:

schema {
    query: RootQuery
}

type RootQuery {
    Users(input: UserFilter): [User!]!
    # ... some more fields here
}

input UserFilter {
    username: [String]
}

type User {
    firstName: String!
    # ... some more fields here
}

Then the tools will generate TypeScripts like this:

/**
 * This interface define the shape of your resolver
 * Note that this type is designed to be compatible with graphql-tools resolvers
 * However, you can still use other generated interfaces to make your resolver type-safed
 */
export interface GQLResolver {
  RootQuery?: GQLRootQueryTypeResolver;
  User?: GQLUserTypeResolver;
}

export interface GQLRootQueryTypeResolver {
  Users?: RootQueryToUsersResolver;
}

export interface RootQueryToUsersArgs {
  Users?: GQLUserFilter;
}

export interface RootQueryToUsersResolver<TParent = any, TResult = any> {
  (parent: TParent, args: RootQueryToUsersArgs, context: any, info: GraphQLResolveInfo): TResult;
}

In this example, if you are not using graphql-tools, you can still use RootQueryToUsersResolver type to make your args type safed.

Default TParent & TResult

In version 1.2.2, a strategy for generating default TParent and TResult has been implemented by setting smartTParent and smartTResult options to true.

If both options are set to true, the resolver will be generated as follow:

// smartTParent: true
// smartTResult: true
// TParent is undefined because it uses the value of 'rootValueType' in options
export interface RootQueryToUsersResolver<TParent = undefined, TResult = Array<GQLUser> {
  (parent: TParent, args: RootQueryToUsersArgs, context: any, info: GraphQLResolveInfo): TResult;
}

However, since RootQueryToUsersResolver usually will be asynchronous operation, the default TResult would not be too helpful, as developers would most likely overwrite it to Promise<Array<GQLUser>>. Therefore, another option , asyncResult, was implemented. This option basically allow resolver to return promises

// smartTParent: true
// smartTResult: true
// asyncResult: true
export interface RootQueryToUsersResolver<TParent = undefined, TResult = Array<GQLUser> {
  (parent: TParent, args: RootQueryToUsersArgs, context: any, info: GraphQLResolveInfo): Promise<TResult> | TResult; // the different is here
}
// in v1.12.11, asyncResult also accept string value 'always', 
// which will make returns value of resolve functions to be `Promise<TResult>`,
// due to an issue with VSCode that not showing auto completion when returns is a mix of `T | Promise<T>` (see [#17](https://github.com/dangcuuson/graphql-schema-typescript/issues/17))

// smartTParent: true
// smartTResult: true
// asyncResult: 'always'
export interface RootQueryToUsersResolver<TParent = undefined, TResult = Array<GQLUser> {
  (parent: TParent, args: RootQueryToUsersArgs, context: any, info: GraphQLResolveInfo): Promise<TResult>; // the different is here



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap