web-dev-qa-db-fra.com

Schéma GraphQL avec des objets imbriqués

Comment écrire un schéma pour ce type de réponse.

{
  "adult": false,
  "backdrop_path": "/dnaitaoCh8MftfYEVnprcuYExZp.jpg",
  "belongs_to_collection": {
    "id": 256322,
    "name": "The Purge Collection",
    "poster_path": "/nP3c8mTSxlis4vfg0UjlkK8LRG9.jpg",
    "backdrop_path": "/quFWGOA4I5KCTsyDbvLh6PHNZwv.jpg"
  },
  "budget": 13000000,
  "genres": [
    {
      "id": 28,
      "name": "Action"
    },
    {
      "id": 27,
      "name": "Horror"
    },
    {
      "id": 878,
      "name": "Science Fiction"
    },
    {
      "id": 53,
      "name": "Thriller"
    }
  ]
}
6
Ivan Baranov

Peu de temps après avoir posé cette question, je l'ai compris. Voici le schéma:

`const MovieType = new GraphQLObjectType({
  name: 'Movie',
  fields: () => ({
    id: { type: GraphQLString },
    adult: { type: GraphQLBoolean },
    backdrop_path: { type: GraphQLString },
    belongs_to_collection: { type: BelongsToCollection },
    budget: { type: GraphQLInt },
    overview: { type: GraphQLString },
    popularity: { type: GraphQLInt },
    poster_path: { type: GraphQLString },
    production_companies: {
      type: new GraphQLList(CompaniesType)
    },
    genres: {
      type: new GraphQLList(GenreType)
    },
    release_date: { type: GraphQLString },
    tagline: { type: GraphQLString },
    title: { type: GraphQLString },
    vote_average: { type: GraphQLInt },
    vote_count: { type: GraphQLInt }
  })
});

const CompaniesType = new GraphQLObjectType({
  name: 'ProductionCompanies',
  fields: {
    id: { type: GraphQLInt },
    name: { type: GraphQLString },
    logo_path: { type: GraphQLString },
    original_country: { type: GraphQLString }
  }
});

const GenreType = new GraphQLObjectType({
  name: 'Genre',
  fields: () => ({
    id: { type: GraphQLInt },
    name: { type: GraphQLString }
  })
})

const BelongsToCollection = new GraphQLObjectType({
  name: 'BelongsToCollection',
  fields: () => ({
    id: { type: GraphQLInt },
    name: { type:  GraphQLString },
    poster_path: { type: GraphQLString },
    backdrop_path: { type: GraphQLString  } 
  })
});`
2
Ivan Baranov
const typeDefs = `
  type BelongsToCollectionType {
    id: ID!
    name: String
    poster_path: String
    backdrop_path: String
  }

  type GenreType {
    id: ID!
    name: String
  }

  type SomeType {
    adult: Boolean
    backdrop_path: String
    belongs_to_collection: BelongsToCollectionType
    budget: Int
    genres: [GenreType]!
  }
`;
1
slideshowp2