GraphQL-在联合体内以不同类型使用相同的字段名称 [英] GraphQL - Using same field names in different types within union

查看:44
本文介绍了GraphQL-在联合体内以不同类型使用相同的字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have a union type like this:

union AllOnboardingQuestionTypes = StepFinal | TimeRangesQuestion | PercentQuestion | SingleSelectQuestion | MultiSelectQuestion | InformationOnly

其中某些类型具有相同的字段名称但类型不同(例如, PercentAnswer 的答案是 float ,而SingleSelect的答案是 string ),这是执行查询的正确方法吗?

where some of the types have the same field names but different types (e.g. answer for PercentAnswer is float whereas SingleSelect's answer is a string), is this the right way to do the query?

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 .

The aliases seem cumbersome to me and will make things tricky as I'll have to dealias when mutating data my hope was that I could just use answer instead of singleSelectAnswer.

但是当我尝试这样做时,出现此错误:

But when I try that, I get this error:

{"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的新手,不知道这是否是处理它的正确方法.在我的脑海里,我希望遵循多态性方法,但是也许那不是这样做的方法.

I'm new to GraphQL and don't know if this is the correct way to handle it. In my head I was hoping to follow a polymorphism approach but maybe that's not the way it's done.

推荐答案

我遇到了同样的问题,并找到了三种不同的解决方案:

I had the same problem and find three different solutions to it:

  1. 使用界面
  2. 使用联盟
  3. 使用带有其他类型信息的字符串

一切都不是很好,但是可以工作.我在Github上放置了一个带有所有解决方案的简单示例项目: https://github.com/chnoack/apollo-dynamic-types-example

All not very nice but working. I put a simple example project with all solutions on Github: https://github.com/chnoack/apollo-dynamic-types-example

这篇关于GraphQL-在联合体内以不同类型使用相同的字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆