接口类型实现的GraphQL查询片段

时间:2018-06-20 23:59:15

标签: graphql

假设我有两种类型,即Comment和Query,它们实现了公共接口UserItem。我有一个查询,它将返回一个UserItem对象数组,该数组可以是Comment或Query类型。模式如下:

enum ItemType {
  Comment
  Question
}

interface UserItem {
  id: ID!
  __typename: ItemType
}

type CommentContent {
  text: String!
}

type Comment implements UserItem {
  id: ID!
  __typename: ItemType
  content: CommentContent!
}

type QuestionContent {
  text: String!
  answer: String
}

type Question implements UserItem {
  id: ID!
  __typename: ItemType
  content: QuestionContent!
}

type Query {
  getUserItems(ids: [ID!]): [UserItem]
}

schema {
  query: Query
}

查询:

query getUserItems($ids: [ID!]) {
  getUserItems(ids: $ids) {
    id
    __typename
  }
}

工作正常,并且返回两个类型对象,且两个对象中都没有content字段。

查询:

query getUserItems($ids: [ID!]) {
  getUserItems(ids: $ids) {
    id
    __typename
    ... on Comment {
      content {
        text
      }
  }
}

工作正常,并返回content以获得评论,但不返回问题。和查询:

query getUserItems($ids: [ID!]) {
  getUserItems(ids: $ids) {
    id
    __typename
    ... on Question {
      content {
        text
        answer
      }
  }
}

工作正常,并返回content的问题,但不返回评论。

但是我想要的是每个字段上合适的content字段,例如:

query getUserItems($ids: [ID!]) {
  getUserItems(ids: $ids) {
    id
    __typename
    ... on Comment {
      content {
        text
      }
    }
    ... on Question {
      content {
        text
        answer
      }
    }
  }
}

此查询无效,并且返回错误消息,例如(来自AWS AppSync):Validation error of type FieldsConflict: content: they return differing types null and null

是否可以实现我希望为每种类型使用合适的context对象的愿望?还是我的模式方法注定要失败?

0 个答案:

没有答案
相关问题