Skip to content
Vinicius Reif Biavatti edited this page Jan 20, 2020 · 4 revisions

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:

TypeDescriptionSyntax
EntityThe entity is the data structure in which the GraphQL will read and used for query data manipulationtype Library
QueryThe section to define the query functions that will be implemented with the language usedtype Query
MutationThe section to define the mutation functions, that is, the functions will create, delete or update the datatype 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:

EntityFields
Libraryid: Integer
name: String
books: Book Array
Bookid: Integer
title: String
number: Integer
author: Author
Authorid: 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.

Clone this wiki locally