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"
}
]
}
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.
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.
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.
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.
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]!
}
`;
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 }
})
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With