![](/img/trans.png)
[英]Can one have different types for same field between Prisma GraphQL schema and datamodel?
[英]GraphQL Root Query: Same schema, different types
我是GraphQL的新手,在我的根查詢中,除了我的“ type”屬性外,我還有兩個要合並的字段。
allPosts
返回一個post對象數組,而post
返回單個post。
每個字段都使用相同的架構,並且根據傳入的參數在相應字段中確定裝入程序/資源。
const RootQuery = new graphql.GraphQLObjectType({
name: 'Query',
description: 'Root Query',
fields: {
allPosts: {
type: new graphql.GraphQLList(postType),
args: {
categoryName: {
type: graphql.GraphQLString
}
},
resolve: (root, args) => resolver(args)
},
post: {
type: postType,
args: {
slug: {
type: graphql.GraphQLString
}
},
resolve: (root, args) => resolver(args)
},
});
是否可以將這兩個字段組合為一個字段,並根據傳入的參數或其他變量確定類型?
不,你不能!
一旦將字段定義為GraphQLList
,就總會得到一個數組。 您不可能突然得到一個object
而不是的array of
。 當您將字段定義為GraphQLObjectType
(或任何其他標量類型)並且想要獲取數組作為結果時,同樣適用於其他情況。
這兩個領域的目的確實不同。
無論如何,您始終可以在allPosts
字段中添加一個限制邏輯,並將結果限制為一個。 但是,盡管如此,您總是只發表一篇文章
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.