【发布时间】:2020-10-16 19:19:03
【问题描述】:
如何将简单的对象类型 [1](如接口和/类)从 GraphQL 模式定义生成到 Dart 中?
- 动机
- 单一事实来源:GraphQL 架构
- 从 GraphQL 所有普通简单类型都生成为多种语言
- 在我们的例子中是 Typescript 和 Dart
- 到目前为止我们已经尝试过什么
- 所有与 [2] 相关的解决方案 - 甚至分叉 - 都不做简单类型
- 我们知道的可能的 DIY 方法
- GraphQL 代码生成器 [3]
- 快速键入类似的替代方案 [4]
GraphQL 到 Typescript 示例
鉴于此架构定义
type Person {
age: Int
name: String!
}
运行这个脚本
import { GraphQLDefinitionsFactory } from '@nestjs/graphql';
import { join } from 'path';
const definitionsFactory = new GraphQLDefinitionsFactory();
definitionsFactory.generate({
typePaths: ['./**/*.graphql'],
path: join(process.cwd(), './class.ts'),
outputAs: 'class'
});
definitionsFactory.generate({
typePaths: ['./src/**/*.graphql'],
path: join(process.cwd(), './interface.ts'),
outputAs: 'interface'
});
输出
export interface Person {
age?: number;
name: string;
}
export class Person {
age?: number;
name: string;
}
1:简单对象类型应该只是简单的普通对象类型,没有注释、编码和解码逻辑等。
2:https://github.com/gql-dart/gql
3:https://graphql-code-generator.com
4:https://app.quicktype.io
【问题讨论】:
-
你可以试试我的包graphql_to_dart。它正在进行中,但适用于输出类型。
-
非常感谢您如此快速地回复,也感谢您提供这个包裹,我们将把我们的问题放在那里。
标签: flutter dart types graphql code-generation