How to fix Error: Type “Extra” was defined more than once in apollo-server, using graphql

拥有回忆 提交于 2020-01-07 09:32:52

问题


Problem

Hi devs,

I have defined two schemas that technically have the same type, but I get the following error:

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

Is there a way to solve this problem?

My apologies, apollo is new to me and I would really appreciate your help to solve this. Thanks in advance!

Schema #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
}

Schema #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
}

Problem Error

[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:


SOLUTION

Problem corrected using the concept of interfaces and defining types in the same file.


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
}


来源:https://stackoverflow.com/questions/58052328/how-to-fix-error-type-extra-was-defined-more-than-once-in-apollo-server-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!