Apollo 客户端的代码生成器在我的类型中添加了不需要的 "or null"
Apollo client's codegen adds unwanted "or null" in my types
Apollo 客户端的 codegen 在生成的类型中添加了 | null
,我不明白它们为什么在那里以及如何去除它们。
我看不出为什么 API 会 return 一个空数组,所以我不想每次都检查我的代码天气对象是否为空。
来自 apollo codegen 的违规生成类型:
export interface MusicGenres_musicGenres {
name: string;
}
export interface MusicGenres {
musicGenres: (MusicGenres_musicGenres | null)[];
^^^^^^
WHY ?
}
我的 Graphql 模式:
type Query {
musicGenres: [MusicGenre]!
}
type MusicGenre {
id: ID!
name: String!
}
在我的 TypeScript 代码中查询,从中生成类型:
gql`
query MusicGenres {
musicGenres { name }
}
`
在您的架构中,您具有以下字段定义:
musicGenres: [MusicGenre]!
这意味着虽然 musicGenres
将是一个列表并且它本身永远不会为空,但列表中的任何项目 都可以 为空。如果你想表明列表中的所有项目也是不可为空的,你的字段定义应该改为:
musicGenres: [MusicGenre!]!
有关详细说明,请参阅 。
Apollo 客户端的 codegen 在生成的类型中添加了 | null
,我不明白它们为什么在那里以及如何去除它们。
我看不出为什么 API 会 return 一个空数组,所以我不想每次都检查我的代码天气对象是否为空。
来自 apollo codegen 的违规生成类型:
export interface MusicGenres_musicGenres {
name: string;
}
export interface MusicGenres {
musicGenres: (MusicGenres_musicGenres | null)[];
^^^^^^
WHY ?
}
我的 Graphql 模式:
type Query {
musicGenres: [MusicGenre]!
}
type MusicGenre {
id: ID!
name: String!
}
在我的 TypeScript 代码中查询,从中生成类型:
gql`
query MusicGenres {
musicGenres { name }
}
`
在您的架构中,您具有以下字段定义:
musicGenres: [MusicGenre]!
这意味着虽然 musicGenres
将是一个列表并且它本身永远不会为空,但列表中的任何项目 都可以 为空。如果你想表明列表中的所有项目也是不可为空的,你的字段定义应该改为:
musicGenres: [MusicGenre!]!
有关详细说明,请参阅