ruo*_*ola 3 typescript graphql graphql-codegen
GraphQL 代码生成器在创建的 TypeScript 文件顶部创建此类型:
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
Run Code Online (Sandbox Code Playgroud)
并将其用于所有客户端创建的查询变量:
src/foo.graphql:
query Foo($id: ID!) {
foo(id: $id) {
bar
}
}
Run Code Online (Sandbox Code Playgroud)
generated/foo.ts:
...
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
...
export type FooQueryVariables = Exact<{
id: Scalars['ID'];
}>;
...
Run Code Online (Sandbox Code Playgroud)
这种类型的目的是什么Exact<T>?它有何影响FooQueryVariables(相对于如果不存在的话)?
https://www.graphql-code-generator.com/#live-demo的完整演示:
schema.graphql:
schema {
query: Query
}
type Query {
foo(id: ID!): Foo
}
type Foo {
bar: String!
}
Run Code Online (Sandbox Code Playgroud)
operation.graphql:
query Foo($id: ID!) {
foo(id: $id) {
bar
}
}
Run Code Online (Sandbox Code Playgroud)
codegen.yml:
generates:
operations-types.ts:
plugins:
- typescript
- typescript-operations
Run Code Online (Sandbox Code Playgroud)
生成operations-types.ts:
export type Maybe<T> = T | null;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
};
export type Query = {
__typename?: 'Query';
foo?: Maybe<Foo>;
};
export type QueryFooArgs = {
id: Scalars['ID'];
};
export type Foo = {
__typename?: 'Foo';
bar: Scalars['String'];
};
export type FooQueryVariables = Exact<{
id: Scalars['ID'];
}>;
export type FooQuery = { __typename?: 'Query', foo?: Maybe<{ __typename?: 'Foo', bar: string }> };
Run Code Online (Sandbox Code Playgroud)
它的目的是使人们无法将具有任何附加属性(除了 之外id)的对象作为传递FooQueryVariables。但它未能这样做:https://github.com/dotansimha/graphql-code-generator/issues/4577
| 归档时间: |
|
| 查看次数: |
1282 次 |
| 最近记录: |