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

本文是小编为大家收集整理的关于GraphQL联盟和冲突类型的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

  • 我有2种类型的联合(DataResult)(Teaser&Program)
  • 我有一个Zone带有data字段(阵列DataResult)
  • 的类型
  • Teaser&Program具有相同的title字段,具有不同的类型(String vs String!)

这是Zone,Teaser和Program模式的零件:

type Program {
    title: String
}

type Teaser {
    title: String!
}

union DataResult = Teaser | Program

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

当我尝试如下查询部分中所述查询区域数据时,我从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

我不能使用别名,因为规格需要所有"数据库"实体的属性名称.我该怎么办?

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

ps:我将香草apollo用作graphql客户端(在服务器端)

推荐答案

我知道您不能使用别名,但是对于其他可以(包括我在内)的人:

github "> github 使用现场别名:

zones {
    ... on Program {
        programTitle: title
    }
    ... on Teaser {
        teaserTitle: title
    }
}

其他推荐答案

使用 interface 为我解决了这个问题,包括"丢失"字段" - 错误(尽管这意味着我认为字段必须是相同类型的).​​

的线
interface DataResult {
    title: String!
}

type Program implements DataResult {
    // Not sure if this can be empty?
}

type Teaser implements DataResult {
    // Not sure if this can be empty?
}

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

其他推荐答案

它是根据规范实施的: 请参阅3.a,网址: http://face.githbook.github.io/graphql/draft/#sameresponseshape()

请参阅 js/essess/1361#issuecomment-393489320 有关更多详细信息.

本文地址:https://www.itbaoku.cn/post/1938057.html

问题描述

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

  • 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!)

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!]
}

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
        }
    }
}

Here is the error:

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

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

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

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

推荐答案

I know that you cannot use an alias, but for everyone else who can (including me):

The simple answer taken from github is to use field aliases:

zones {
    ... on Program {
        programTitle: title
    }
    ... on Teaser {
        teaserTitle: title
    }
}

其他推荐答案

Using an interface solved this problem for me, including the "missing field" - errors (although this means the fields have to be of the same type I think).

Something along the lines of

interface DataResult {
    title: String!
}

type Program implements DataResult {
    // Not sure if this can be empty?
}

type Teaser implements DataResult {
    // Not sure if this can be empty?
}

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

其他推荐答案

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

See https://github.com/graphql/graphql-js/issues/1361#issuecomment-393489320 for more details.