百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

graphql-schema-typescript

> 编程语言
开源

从 GraphQL 的计划类型定义生成 TypeScript

191 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

从 GraphQL 的计划类型定义生成 TypeScript

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.6 ^16.0.0
1.5 ^14.0.0, ^15.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

javascript
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

  • schema: your graphql schema
  • outputPath: where the types is generated
  • options: see GenerateTypescriptOptions

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:

gql
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:

…

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:

javascript
// smartTParent: true
// smartTResult: true
// TParent is undefined because it uses the value of 'rootValueType' in options
export interface RootQueryToUsersResolver {
  (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>. Therefore, another option , asyncResult, was implemented. This option basically allow resolver to return promises

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

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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言