GraphQL联合和冲突类型 [英] GraphQL union and conflicting types

查看:315
本文介绍了GraphQL联合和冲突类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目遇到问题,我在互联网上找不到任何解决方案.这是情况.

I've got an issue on my project and I can't find any solution on the internet. Here is the situation.

  • 我有2种类型的联合(DataResult)(Teaser& Program)
  • 我有一个Zone类型,带有一个data字段(一个数组DataResult)
  • Teaser& Program具有相同的title字段,但类型不同(StringString!)
  • I've an Union (DataResult) of 2 types (Teaser & Program)
  • I've a Zone type with a data field (an array DataResult)
  • Teaser & Program have the same title field with a different type (String vs String!)

这是ZoneTeaserProgram模式的一部分:

Here is parts from Zone, Teaser and Program schemas:

type Program {
    title: String
}

type Teaser {
    title: String!
}

union DataResult = Teaser | Program

type Zone {
    (...)
    data: [DataResult!]
}

当我尝试按照下面查询部分所述查询区域数据时,出现了GraphQL错误.

When I tried to query zone data as described in the query part below, I've got an error from GraphQL.

zones {
    ...zoneFieldsWithoutData
    data {
        ... on Program {
            ...programFields
        }
        ... on Teaser {
            ...teaserFields
        }
    }
}

这是错误:

Error: GraphQL error: Fields \"title\" conflict because they return conflicting types String and String!. Use different aliases on the fields to fetch both if this was intentional

我不能使用别名,因为所有"DataResult"实体的规范都需要相同的属性名称.我该怎么办?

I can't use an alias because the specs needs the same attribute name for all "DataResult" entities. What can I do ?

此外,即使我为title设置了相同的类型",我也会收到很多关于控制台中缺少字段"的警告....

Moreover, even if I set the same Type for title, I've a lot of warning about "missing fields" in the console....

PS:我使用Vanilla Apollo作为GraphQL客户端(在服务器端)

PS: I use Vanilla Apollo as GraphQL client (on the server side)

推荐答案

它是根据规范实现的: 参见3.a: http://facebook.github.io/graphql/draft/#SameResponseShape()

It's implemented according to the specification: See 3.a at: http://facebook.github.io/graphql/draft/#SameResponseShape()

请参见 https://github.com/graphql/graphql- js/issues/1361#issuecomment-393489320 了解更多详情.

这篇关于GraphQL联合和冲突类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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