Updating our Schema
Learn how to update a schema by using MongoDB with an endpoint.
We'll cover the following...
Schema types and inputs
Our MongoDB database has three entities: Book
, Author
, and Review
. Let’s consider the following schema:
Press + to interact
const types = gql`type Book {_id: Stringtitle: Stringprice: Floatidentifier: Stringdescription: Stringauthor: Authorreviews: [Review]}type SimpleBook {_id: Stringtitle: Stringprice: Floatidentifier: Stringdescription: String}type Author {_id: StringfirstName: StringlastName: String}type Review {_id: StringbookId: Stringrating: Intcomment: String}`;
MongoDB schema
To access our database, we will need to define our schema as follows (they can be different from the GrapghQL ones):
Press + to interact
// Defining schema interfaceinterface BookSchema {_id: Bson.ObjectId;title: string;price: number;identifier: string;description: string;authorId: Bson.ObjectId;reviewIds: Bson.ObjectId[];}interface AuthorSchema {_id: Bson.ObjectId;firstName: string;lastName: string;}interface ReviewSchema {_id: Bson.ObjectId;rating: number;comment: string;bookId: string;}
In the schema definition given above, we try to use different approaches when handling the relationship between entities. It’s important to note the following:
authorId
is a field in the book