0

Problem

Hello friends,

I am in the process of converting my cinemanight API to be compatible with graphql. I have the problem when I try to generate the structure of the Extra type, where you can see the example in the following link https://cinemanight.chrismichael.now.sh/api/v1/series/1

It shows me an error related to the Field \" extra \ "of type \" [Extra!]! \ "must have a selection of subfields. Did you mean \ "extra {...} \"?

According to the example of the endpoint route that I show you; Extra is of a list type, so I defined it as[Extra], but apparently it should not be of that type.

Any help I would appreciate!

Error

{
  "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
}

Chris Michael
  • 1,121
  • 1
  • 9
  • 16
  • Is it a query issue - https://stackoverflow.com/questions/46111514/field-me-of-type-user-must-have-a-selection-of-subfields? Can you add subfields in query and check again? – Prakhar Sep 22 '19 at 04:00
  • { extra { channel } } – Prakhar Sep 22 '19 at 04:01
  • @Prakhar Excuse me friend, I do not understand how the structure should be with the solution you are considering – Chris Michael Sep 22 '19 at 04:05
  • I assume errors you receive are appearing when your query? Can you share the way you are querying? I mean can you share your query in question. – Prakhar Sep 22 '19 at 04:07
  • @Prakhar This is the way query {    series (page: 1) {      id      title      synopsis      poster      poster      rating      year      extra    } } – Chris Michael Sep 22 '19 at 04:09

2 Answers2

3

Please add subfield in query when you query object. This is how it works.

Try with channel inside extra in your query

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

You have to put all fields and sub-fields in query when you want answers for it. Or, you can query without object field at all -

{ 
  series (page: 1) 
  { 
    id 
  }
}
Prakhar
  • 1,096
  • 1
  • 16
  • 41
  • What a fool I was hehe, how I didn't realize that mistake of mine! You are absolutely right I need to refer to the values. – Chris Michael Sep 22 '19 at 04:17
  • 1
    You can up-vote if it worked for you. – Prakhar Sep 22 '19 at 04:19
  • Hi @Prakhar I have a new problem related to duplicate types. Link here: https://stackoverflow.com/questions/58052328/how-to-fix-the-problem-related-to-error-type-extra-was-defined-more-than-once – Chris Michael Sep 22 '19 at 21:17
0

What I think is happening here is that GraphQL is asking you to list what fields you need. You cant just return a whole data field without listing the sub field you need. That is the essence of GraphQL to only return the datas you need.

Based on your code sample you can do something like this.

query allseries{
    id
    title
    sinopsis
    poster
    extra{
        channel
        first_air_date
        season_list
        cast_members
    }
}

With the above above query you have listed what you need from the Series and Extra field. If you need all the sub field you will lost it out in the query. Hope it helps.