GraphQL - 在联合中使用不同类型的相同字段名称

标签 graphql

如果我有这样的联合类型:

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/

相关文章:

reactjs - Gatsby (Gatsby)和斯特拉皮(Strapi)实现国际化的最佳途径

reactjs - 如何限制 useQuery 的 api 调用只能调用一次?

jmeter - graphQL API 的性能测试

javascript - Gatsby `createPage` 正在生成具有相同数据的页面

node.js - 如何为现有数据库构建Graphql后端服务器?

reactjs - Webpack 将 'no exports provided' 添加到具有导出的模块

node.js - STRAPI 是否有助于开发 REST api 以轻松且经济有效地连接 Flutter 应用程序

django - 具有多对多和直通表的 Graphene-django

graphql - 错误消息: Instance Data is not valid whilst updating a GraphQL schema

graphql - 如何修复 EntitySchema 和 Graphql 中的 'Cannot read property ' joinColumns' of undefined' 错误