使用[apollo/graphql]构造对象属性的问题

使用[apollo/graphql]构造对象属性的问题,graphql,apollo,apollo-server,Graphql,Apollo,Apollo Server,问题 朋友们好, 我正在使用Apollo服务器开发一个api。 我遇到的问题是如何只显示一次nextEpisodeDate属性。我的解决方案在剧集属性的所有子数组中显示nextEpisodeDate,它不应该是这样的 我希望有人能帮助我 JSON示例 "episodes": [ { "nextEpisodeDate": "2020-01-17" }, { "episode": 3, "id": "53789/dorohedoro-3",

问题

朋友们好,

我正在使用Apollo服务器开发一个api。 我遇到的问题是如何只显示一次
nextEpisodeDate
属性。我的解决方案在
剧集
属性的所有子数组中显示
nextEpisodeDate
,它不应该是这样的

我希望有人能帮助我

JSON示例

"episodes": [
   {
     "nextEpisodeDate": "2020-01-17"
   },
   {
     "episode": 3,
     "id": "53789/dorohedoro-3",
     "imagePreview": "https://cdn.animeflv.net/screenshots/3274/3/th_3.jpg"
   },
   {
     "episode": 2,
     "id": "53755/dorohedoro-2",
     "imagePreview": "https://cdn.animeflv.net/screenshots/3274/2/th_3.jpg"
   },
   {
     "episode": 1,
     "id": "53705/dorohedoro-1",
     "imagePreview": "https://cdn.animeflv.net/screenshots/3274/1/th_3.jpg"
   }
 ]
typeDefs

const resolvers = require('./resolvers');
const {gql} = require('apollo-server');

const typeDefs = gql `
  extend type Query{
    latest_anime: [Animes]
  }

  type Animes{
    title: String
    poster: String
    synopsis: String
    debut: String
    type: String
    rating: String
    genres: [String]
    episodes: [Episodes]
  }

  type Episodes{
    nextEpisodeDate: String
    episode: String
    id: String
    imagePreview: String
  }
`

module.exports = {
  typeDefs,
  resolvers
};

阿波罗游乐场

query{
  latest_anime{
    title
    poster
    synopsis
    debut
    type
    rating
    genres
    episodes{
      nextEpisodeDate
      episode
      id
      imagePreview
    }
  }
}
阿波罗游乐场输出

{
  "data": {
    "latest_anime": [
      {
        "title": "Tsugumomo OVA",
        "poster": "https://animeflv.net/uploads/animes/covers/3275.jpg",
        "synopsis": "OVA 4.6Kazuya Kagami nunca va a ningún lado sin su preciada “Sakura Obi” que su madre le regaló. Un día, una hermosa chica vestida con un kimono llamada Kiriha aparece ante él. Naturalmente, ella comienza a vivir en su habitación. ¿Naturalmente? ¡Esto solo es el inicio de la embarazosa y confusa...",
        "debut": null,
        "type": "OVA",
        "rating": "4.6",
        "genres": [
          "accion",
          "comedia",
          "ecchi",
          "escolares",
          "seinen",
          "sobrenatural"
        ],
        "episodes": [
          {
            "nextEpisodeDate": null,
            "episode": null,
            "id": null,
            "imagePreview": null
          },
          {
            "nextEpisodeDate": null,
            "episode": "1",
            "id": "53753/tsugumomo-ova-1",
            "imagePreview": "https://cdn.animeflv.net/screenshots/3275/1/th_3.jpg"
          }
        ]
      },
    ]
  }
}

获得所需响应结构的唯一方法是使用两种不同的类型。字段必须只有一种类型,但您可以使用诸如联合或接口之类的抽象类型,以便在运行时将列表中的每个单独项解析为多种类型之一

type AiredEpisode implements Episode {
  id: String
  episode: String
  imagePreview: String
}

type UpcomingEpisode implements Episode {
  id: String
  nextEpisodeDate: String
}

interface Episode {
  id: String
}

type Anime {
  episodes: [Episode]
  # other fields
}
然后,您可以像这样查询剧集:

query {
  latest_anime {
    episodes {
      # fields on the interface itself like id are common to all
      # implementing types so they don't need to be inside a fragment
      id
      # fields specific to one of the types need to be inside a fragment
      ... on UpcomingEpisode {
        nextEpisodeDate
      }
      ... on AiredEpisode {
        id
        episode
        imagePreview
      }
    }
  }
}

旁注:如果您的API没有为即将播出的剧集返回
id
,您仍然应该提供一个(您可以使用节目的id,例如,您只想确保它是唯一的)。这将确保在前端使用Apollo这样的客户端时不会遇到缓存问题。

工作正常,
剧集
类型包含
nextEpisodeDate
。。。查询(您请求的字段)包含
nextEpisodeDate
-然后提供-
null
如果undefined@xadm我理解,但是这个属性应该只出现在初始索引中,而不是数组的所有索引中。解析器必须返回typed(使用已定义的结构)object@xadm对但我还是认为我会让它这样。在客户端,我可以指定值是否不同于null。否则,我会忽略它。当然,即使在JSX f.e.`{nextEpisodeDate&}