Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL schema with nested objects

How to write schema for this kind of response.

{
  "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"
    }
  ]
}
like image 584
Ivan Baranov Avatar asked Aug 31 '18 08:08

Ivan Baranov


People also ask

How do you query nested objects in GraphQL?

When setting up a field whose value is a custom type, we have to define a function that tells GraphQL how to get that custom type. In our case, we want to tell GraphQL how to get the posts if we have the author. We do that by defining a new root property inside resolvers.

Can GraphQL have multiple schemas?

Schema stitching is the idea that you can take two or more GraphQL schemas, and merge them into one endpoint that can pull data from all of them.

Does GraphQL have a schema?

Your GraphQL server uses a schema to describe the shape of your available data. This schema defines a hierarchy of types with fields that are populated from your back-end data stores. The schema also specifies exactly which queries and mutations are available for clients to execute.

What two pieces make up a GraphQL schema?

In an app using GraphQL, there are two core pieces: your GraphQL schema and your GraphQL server (independent from your HTTP server). The schema is attached to the server so that when a request comes in, the server understands how to process it.


2 Answers

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]!
  }
`;
like image 137
slideshowp2 Avatar answered Oct 16 '22 07:10

slideshowp2


Soon after I asked this question I figured it out. Here is the schema:

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  } 
  })
});
like image 41
Ivan Baranov Avatar answered Oct 16 '22 07:10

Ivan Baranov