如何修复错误:使用 graphql 在 apollo-server 中多次定义类型“Extra”

Posted

技术标签:

【中文标题】如何修复错误:使用 graphql 在 apollo-server 中多次定义类型“Extra”【英文标题】:How to fix Error: Type "Extra" was defined more than once in apollo-server, using graphql 【发布时间】:2020-01-22 22:14:41 【问题描述】:

问题

各位开发者,

我已经定义了两个在技术上具有相同类型的架构,但我收到以下错误:

 Error: Type "Extra" was defined more than once.

有没有办法解决这个问题?

抱歉,阿波罗对我来说是新的,我非常感谢您帮助解决这个问题。提前致谢!

架构 #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)

【问题讨论】:

【参考方案1】:

解决方案

使用接口的概念和在同一文件中定义类型更正了问题。


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

【讨论】:

以上是关于如何修复错误:使用 graphql 在 apollo-server 中多次定义类型“Extra”的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Mongoose Schema 修复 GraphQL Mutation 中的构造函数错误 [重复]

如何修复错误:使用 graphql 在 apollo-server 中多次定义类型“Extra”

如何修复这个 typescript 和 graphql 错误?

如何修复python中graphql查询中的“解析错误(VAR_SIGN)”

如何使用 GraphQL Relay 连接修复循环依赖

如何修复“未捕获的错误:查询不再有模拟响应”