【发布时间】:2020-05-20 14:55:19
【问题描述】:
问题
朋友们好,
我正在使用 Apollo Server 开发一个 api。
我遇到了如何只显示一次nextEpisodeDate 属性的问题。我的解决方案在episodes 属性的所有子数组中显示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"
}
]
},
]
}
}
【问题讨论】:
-
工作正常,
Episodes类型包含nextEpisodeDate,...查询(您要求的字段)包含nextEpisodeDate- 然后提供 -null如果未定义则返回 -
@xadm 我明白了,但是这个属性应该只出现在初始索引中,而不是出现在数组的所有索引中。
-
解析器必须返回类型化(具有定义的结构)对象
-
@xadm 是的,但我仍然认为我会这样。在客户端,我可以指定该值是否不同于 null。否则,我忽略它。
-
当然,即使在 JSX f.e. `{nextEpisodeDate &&
}
标签: graphql apollo apollo-server