Posted on • Originally published atlearnwithparam.com
Practical example of GraphQL schema design
In this article, we will create a GraphQL schema for a notes app. We will create a schema for creating, updating, deleting and fetching notes. We will then extend the schema to support pagination, custom reports and custom mutations.
typeNote{id:ID!title:String!content:String!categoryId:ID!tags:[String!]!}
Let's create a schema for creating a note,
typeMutation{createNote(title:String!content:String!categoryId:ID!tags:[String!]!):Note!}
Let's create a schema for updating a note,
typeMutation{updateNote(id:ID!title:String!content:String!categoryId:ID!tags:[String!]!):Note!}
Let's create a schema for deleting a note,
typeMutation{deleteNote(id:ID!):Boolean!}
Let's create a schema for fetching a note,
typeQuery{noteById(id:ID!):Note!}
Let's create a schema for fetching notes by category,
typeQuery{notesByCategory(categoryId:ID!):[Note!]!}
Let's create a schema for fetching notes by tags,
typeQuery{notesByTags(tags:[String!]!):[Note!]!}
Let's create a schema for fetching all notes and support paginated list,
typeQuery{notes(page:Int,limit:Int):[Note!]!}
Let's create custom reports on notes,
typeQuery{notesReport:NotesReport!reportsByCategory:[CategoryReport!]!}typeNotesReport{totalNotes:Int!totalCategories:Int!totalTags:Int!}typeCategoryReport{categoryId:ID!totalNotes:Int!}
Now, let's create dedicated custom mutations to
- update catogory
- add / remove tag
typeMutation{updateCategory(id:ID!,categoryId:ID!):Note!addTag(id:ID!,tag:String!):Note!removeTag(id:ID!,tag:String!):Note!}
Hope you enjoyed the article and learn how to create and extend your graphql schema for your application needs 🙏
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse