如果我有这样的联合类型:
union AllOnboardingQuestionTypes = StepFinal | TimeRangesQuestion | PercentQuestion | SingleSelectQuestion | MultiSelectQuestion | InformationOnly
其中某些类型具有相同的字段名称但类型不同(例如
PercentAnswer
的答案是 float
而 SingleSelect 的答案是 string
),这是进行查询的正确方法吗?query OnboardingQuestionsQuery {
onboardingQuestions {
... on InformationOnly {
title
description
questionID
inputType
}
... on StepFinal {
title
description
}
... on TimeRangesQuestion {
title
description
timePeriods {
timePeriod
estimatedEnd
estimatedStart
}
}
... on SingleSelectQuestion {
title
description
questionID
inputType
singleSelectAnswer: answer
singleSelectOptions: options {
value
label
}
}
... on MultiSelectQuestion {
title
description
questionID
inputType
multiSelectAnswer: answer
multiSelectOptions: options
}
... on PercentQuestion {
title
description
questionID
inputType
percentAnswer: answer
min
max
}
}
}
别名对我来说似乎很麻烦,并且会使事情变得棘手,因为在改变数据时我必须处理
我希望我可以使用
answer
而不是 singleSelectAnswer
.但是当我尝试这样做时,我收到此错误:
{"errors":[{"message":"Fields \"answer\" conflict because they return conflicting types String! and [Boolean!]!. Use different aliases on the fields to fetch both if this was intentional.","locations":[{"line":64,"column":7},{"line":69,"column":7}]},{"message":"Fields \"answer\" conflict because they return conflicting types String! and Float!. Use different aliases on the fields to fetch both if this was intentional.","locations":[{"line":64,"column":7},{"line":74,"column":7}]},{"message":"Fields \"answer\" conflict because they return conflicting types [Boolean!]! and Float!. Use different aliases on the fields to fetch both if this was intentional.","locations":[{"line":69,"column":7},{"line":74,"column":7}]}]}
我是 GraphQL 的新手,不知道这是否是处理它的正确方法。在我的脑海中,我希望遵循多态方法,但也许这不是它的完成方式。
最佳答案
is this the right way to do the query?
是的。工会按照现行(2018年6月)GraphQL specification这样限制.另请参阅 graphql.js 上的相应问题
The aliases seem cumbersome
避免这种情况的最佳方法是让架构在返回类型不同时指定不同的字段名称。
关于GraphQL - 在联合中使用不同类型的相同字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44170603/