ris*_*ott 5 graphql graphql-js apollo-server
我今天在生产中有以下 graphql 模式定义:
type BasketPrice {
amount: Int!
currency: String!
}
type BasketItem {
id: ID!
price: BasketPrice!
}
type Basket {
id: ID!
items: [BasketItem!]!
total: BasketPrice!
}
type Query {
basket(id: String!): Basket!
}
Run Code Online (Sandbox Code Playgroud)
我想重命名BasketPrice
为 just Price
,但是这样做会对架构造成重大更改,因为客户端可能会在片段中引用它,例如
fragment Price on BasketPrice {
amount
currency
}
query Basket {
basket(id: "123") {
items {
price {
...Price
}
}
total {
...Price
}
}
}
Run Code Online (Sandbox Code Playgroud)
我曾希望可以为向后兼容起别名,例如
type Price {
amount: Int!
currency: String!
}
# Remove after next release.
type alias BasketPrice = Price;
type BasketPrice {
amount: Int!
currency: String!
}
type BasketItem {
id: ID!
price: BasketPrice!
}
type Basket {
id: ID!
items: [BasketItem!]!
total: BasketPrice!
}
type Query {
basket(id: String!): Basket!
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不是一个功能。是否有推荐的方法来安全地重命名 graphql 中的类型而不会导致重大更改?
由于您已经指定的原因,无法在不进行重大更改的情况下重命名类型。重命名类型是一种表面的更改,而不是功能性的更改,因此没有实际理由这样做。
处理架构的任何重大更改的最佳方法是在不同的端点上公开新架构,然后将客户端转换为使用新端点,从而有效地实现 API 的版本控制。
我能想到解决此问题的唯一其他方法是为使用旧类型的任何字段创建新字段,例如:
type BasketItem {
id: ID!
price: BasketPrice! @ deprecated(reason: "Use itemPrice instead")
itemPrice: Price!
}
type Basket {
id: ID!
items: [BasketItem!]!
total: BasketPrice! @ deprecated(reason: "Use basketTotal instead")
basketTotal: Price!
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3571 次 |
最近记录: |