我们正在研究在我们正在开发的无头CMS的版本2中使用GraphQL。
在CMS的第1版中,在将文档保存到数据库之前,我们使用JSON schema根据模式验证每个文档--例如,如果是博客文章,则根据Article
模式进行验证;如果是舍入(“最佳”列表),则根据Roundup
模式进行验证。
对于版本2,我们正在考虑为应用程序接口使用GraphQL。然后,我们发现GraphQL模式基本上与JSON模式是平行的--它描述了文档结构、字段类型等。
因此,我们可以简单地使用“一个模式真值来源”,即GraphQL模式,并将其用于查询文档和在保存新修订时验证新文档。(请注意,我说的是针对GraphQL模式验证JSON数据,而不是针对模式验证GraphQL查询。)
我认为数据将针对模式中的所有字段进行验证,但不推荐使用的字段除外,因为您只想针对字段的“最新版本”进行验证。
我们可以做以下三件事之一:
问题:是#1和#2愚蠢的想法吗?有没有可以做这种数据验证的GraphQL工具?有没有其他方法可以在不两次定义模式的情况下实现这一点?
作为参考,我们的后端将使用Python语言编写,但管理UI将是客户端React和JavaScript。这是我们正在讨论的那种GraphQL模式的简化版本(支持“文章”和"Roundup“文档类型):
schema {
query: Query
}
type Query {
documents: [Document!]!
document(id: Int): Document!
}
interface Document {
id: Int!
title: String!
}
type Article implements Document {
id: Int!
title: String!
featured: Boolean!
sections: [ArticleSection!]!
}
union ArticleSection = TextSection | PhotoSection | VideoSection
type TextSection {
content: String!
heading: String
}
type PhotoSection {
sourceUrl: String!
linkUrl: String
caption: String
content: String
}
type VideoSection {
url: String!
}
type Roundup implements Document {
id: Int!
title: String!
isAward: Boolean!
intro: String
hotels: [RoundupHotel!]!
}
type RoundupHotel {
url: String!
photoUrl: String @deprecated(reason: "photoUrl is deprecated; use photos")
photos: [RoundupPhoto!]!
blurb: String!
title: String
}
type RoundupPhoto {
url: String!
caption: String
}
https://stackoverflow.com/questions/45964170
复制相似问题