gpt4 book ai didi

javascript - 使用 graphql 在 apollo 服务器中指定类型时出现问题

转载 作者:行者123 更新时间:2023-12-01 00:36:54 25 4
gpt4 key购买 nike

问题

friend 们大家好,

我正在将我的cinemanight API 转换为与graphql 兼容。当我尝试生成 Extra type 的结构时遇到问题,您可以在以下链接中查看示例 https://cinemanight.chrismichael.now.sh/api/v1/series/1

它向我显示了与 Field \" extra \ "of type \" [Extra!]! \ "must have a selection of subfields. Did you mean \ "extra {...} \"? 相关的错误

根据我向您展示的端点路由示例;额外的是 list类型,所以我将其定义为 [Extra] ,但显然它不应该是那种类型。

如有任何帮助,我将不胜感激!

错误

{
"error": {
"errors": [
{
"message": "Field \"extra\" of type \"[Extra!]!\" must have a selection of subfields. Did you mean \"extra { ... }\"?",
"locations": [
{
"line": 10,
"column": 5
}
],
"extensions": {
"code": "GRAPHQL_VALIDATION_FAILED",
"exception": {
"stacktrace": [
"GraphQLError: Field \"extra\" of type \"[Extra!]!\" must have a selection of subfields. Did you mean \"extra { ... }\"?",
" at Object.Field (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\validation\\rules\\ScalarLeafs.js:45:31)",
" at Object.enter (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\language\\visitor.js:324:29)",
" at Object.enter (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\language\\visitor.js:375:25)",
" at visit (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\language\\visitor.js:242:26)",
" at Object.validate (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\validation\\validate.js:73:24)",
" at validate (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\apollo-server-core\\dist\\requestPipeline.js:212:32)",
" at Object.<anonymous> (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\apollo-server-core\\dist\\requestPipeline.js:125:42)",
" at Generator.next (<anonymous>)",
" at fulfilled (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\apollo-server-core\\dist\\requestPipeline.js:5:58)",
" at process._tickCallback (internal/process/next_tick.js:68:7)"
]
}
}
}
]
}
}
const {gql} = require('apollo-server');

const typeDefs = gql `
extend type Query{
series(page: Int!): [Series!]!
}

type Series{
id: String!
title: String!
sinopsis: String!
poster: String!
rating: String!
year: String!
extra: [Extra!]!
}

type Extra{
channel: String!
first_air_date: String!
last_air_date: String!
total_seasons: String!
total_episodes: String
season_list: [SeasonList!]!
cast_members: CastMembers!
similar_series: [SimilarSeries!]!
}

type SeasonList{
season: Int
episodes: [String]
}

type SimilarSeries{
id: String!
poster: String!
}

type CastMembers{
creator: Creator!
members_list: [MembersList!]!
}

type MembersList{
members_info: [MembersInfo!]!
}

type MembersInfo{
characters: Characters!
}

type Characters{
real_name: String!
character: String!
}

type Creator{
name: String!
poster: String!
}
`;

const resolvers ={
Query:{
series: async(_source , {page} , { dataSources }) =>{
return dataSources.API.getAllSeries(page)
.then(doc =>{
return doc.series
});
}
}
}

module.exports = {
typeDefs,
resolvers
}

最佳答案

查询对象时,请在查询中添加子字段。这就是它的工作原理。

尝试在查询的extra内使用channel

{
series(page: 1)
{
id
title
synopsis
poster
rating
year
extra {
channel
}
}
}

当您想要得到答案时,您必须将所有字段和子字段放入查询中。或者,您可以完全不使用对象字段进行查询 -

{ 
series (page: 1)
{
id
}
}

关于javascript - 使用 graphql 在 apollo 服务器中指定类型时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58045875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com