- Notifications
You must be signed in to change notification settings - Fork 0
Schema
Here we will start with GraphQL. The schema is the structure that the GraphQL will compile and interpret. In the schema, we will define the types of our application. The type is the keyword used to create entities, to define query functions and to create mutations. Check the table bellow for description and syntax of each type:
| Type | Description | Syntax |
|---|---|---|
| Entity | The entity is the data structure in which the GraphQL will read and used for query data manipulation | type Library |
| Query | The section to define the query functions that will be implemented with the language used | type Query |
| Mutation | The section to define the mutation functions, that is, the functions will create, delete or update the data | type Mutation |
For the first thing we will import the needed dependencies to link the express with the GraphQL and the function that will be used to create the schema:
// Importsconstexpress=require('express');constexpress_graphql=require('express-graphql');const{ buildSchema }=require('graphql');// Expressletapp=express();app.listen(3000,()=>console.log('Express running on port 3000'));//So now, we will create the schema using the buildSchema() function available through the GraphQL. Our schema will follow the structure below:
| Entity | Fields |
|---|---|
| Library | id: Integer |
| name: String | |
| books: Book Array | |
| Book | id: Integer |
| title: String | |
| number: Integer | |
| author: Author | |
| Author | id: Integer |
| firstname: String | |
| lastname: String |
The buildSchema() functions expects a string datatype that will be read and compiled with GraphQL:
// Schemaletschema=buildSchema(` type Library { id: ID name: String books: [Book] } type Book { id: ID title: String number: Int author: Author } type Author { id: ID firstname: String lastname: String }`);The second thing to do is to define the Query function that will be available for usage.
NOTE: The GraphQL is strongly typed so we need to define the type of the function return. The return of our functions is an array, so, we have to define it with
[]characteres:
typeQuery{
libraries: [Library]
books: [Book]
authors: [Author]}For parameterized queries we can specify the arguments of the functions we defined. To make the argument required, we can use the ! character with the type:
typeQuery{library(id: Int!): Librarylibraries: [Library]books: [Book]authors: [Author]}The last thing that is missing is the Mutation type. This type is used for insert, update and delete actions. In this section, like Query section, we will create the available functions for these operations that will be implemented later:
typeMutation{createLibrary(name: String!): LibrarycreateBook(title: String!,number: Int!,authorId: Int!): BookcreateAuthor(firstname: String!,lastname: String!): AuthorlinkBook(libraryId: Int!,bookId: Int!): Library}NOTE: Note that we cannot use the type as the parameter, like Book for example, otherwise the GraphQL will return an error. Unfortunately, the GraphQL does not support this feature.
Well done. As the result, our Schema will be this below:
// Schemaletschema=buildSchema(` type Library { id: ID name: String books: [Book] } type Book { id: ID title: String number: Int author: Author } type Author { id: ID firstname: String lastname: String } type Query { library(id: Int!): Library libraries: [Library] books: [Book] authors: [Author] } type Mutation { createLibrary(name: String!): Library createBook(title: String!, number: Int!, authorId: Int!): Book createAuthor(firstname: String!, lastname: String!): Author linkBook(libraryId: Int!, bookId: Int!): Library }`);In the next step, we will learn how to define our physical data for the application usage.