问题
嗨,开发人员,
我定义了两个技术上具有相同类型的架构,但是出现以下错误:
Error: Type "Extra" was defined more than once.
有没有办法解决这个问题?
对不起,阿波罗(Apollo)对我来说是新手,非常感谢您为解决此问题提供的帮助。预先感谢!
架构#1
const {gql} = require('apollo-server');
typeDefs = gql `
extend type Query{
search(q: String!): [Content!]!
}
type Content{
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:{
search: async(_source , {q} , { dataSources}) =>{
return dataSources.API.search(q)
.then(doc =>{
return doc.content
});
}
}
}
module.exports ={
typeDefs,
resolvers
}
架构#2
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
}
问题错误
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
C:\Users\c\Desktop\cinemanight-graphql\node_modules\apollo-server-core\dist\ApolloServer.js:234
throw new Error(errors.map(error => error.message).join('\n\n'));
^
Error: Type "Extra" was defined more than once.
Type "SeasonList" was defined more than once.
Type "SimilarSeries" was defined more than once.
Type "CastMembers" was defined more than once.
Type "MembersList" was defined more than once.
Type "MembersInfo" was defined more than once.
Type "Characters" was defined more than once.
Type "Creator" was defined more than once.
at ApolloServer.initSchema (C:\Users\c\Desktop\cinemanight-graphql\node_modules\apollo-server-core\dist\ApolloServer.js:234:23)
at new ApolloServerBase (C:\Users\c\Desktop\cinemanight-graphql\node_modules\apollo-server-core\dist\ApolloServer.js:202:30)
at new ApolloServer (C:\Users\c\Desktop\cinemanight-graphql\node_modules\apollo-server-express\dist\ApolloServer.js:59:9)
at new ApolloServer (C:\Users\c\Desktop\cinemanight-graphql\node_modules\apollo-server\dist\index.js:24:9)
at Object.<anonymous> (C:\Users\c\Desktop\cinemanight-graphql\index.js:4:16)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
答案 0 :(得分:0)
解决方案
使用接口的概念并在同一文件中定义类型来纠正问题。
const resolvers = require('./resolvers.js');
const {gql} = require('apollo-server');
const typeDefs = gql `
extend type Query{
series(page: Int!): [Series!]!
movies(page: Int!): [Movies!]!
}
interface MainContent{
id: String!
title: String!
sinopsis: String!
poster: String!
rating: String!
year: String!
}
type Series implements MainContent {
id: String!
title: String!
sinopsis: String!
poster: String!
rating: String!
year: String!
extra: [SerieExtra!]!
}
type Movies implements MainContent{
id: String!
title: String!
sinopsis: String!
poster: String!
rating: String!
year: String!
extra: [MovieExtra!]!
}
type SerieExtra {
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 MovieExtra {
air_date: String!
country: String!
runtime: String!
rated: String!
cast_members: CastMembers!
similar_movies: [SimilarMovies!] !
}
type SeasonList {
season: Int
episodes: [String]
}
type SimilarSeries {
id: String!
poster: String!
}
type SimilarMovies {
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!
}
`;
module.exports = {
typeDefs,
resolvers
}