OREIlly not to stumble upon the book First GraphQL
Published: 2022-05-21 18:56:17

Hey guys!
I'm an engineer in Japan.
Although my engineering experience is still limited, I am inspired by developers from all over the world  I am writing my first article for "DEV".

## GraphQL

GraphQL is a query language and runtime for APIs designed to improve the speed, flexibility, and ease of use for developers.
Features include the following

- Only the "necessary data" requested by the client can be retrieved.

- A single request can be made to retrieve the required data. (Only one endpoint is required)

- Defining the data type prevents data type mismatch with the server-side.

Apart from the above features, you need to define a schema when implementing GraphQL, and it is very nice that this schema is directly documented.
After much research on where to start, I decided to purchase OREIlly's book "GraphQL for Beginners".
Although subjective, it is organized in a way that allows you to learn systematically in a reasonable number of pages, and I prefer the practical approach.

[First GraphQL](https://www.oreilly.co.jp/books/9784873118932/)

However, it has been a little while since this book was published, and I had some difficulty because some of the links in the text are not currently available, and some of the codes listed are not available as is.

Below are some of the sections of this book that I found to be stumbling blocks. I hope this will be helpful to those who are wasting their time in similar areas.

## About the book's composition

This book consists of seven chapters.

Chapters 1 and 2 are primarily an introduction to the history and background of GraphQl and graph theory.

Chapters 3 and 4 explain how to write GraphQL, etiquette, client tools, and schema definitions.

Chapters 5 and 6 describe the implementation of GraphQL servers and GraphQL clients.

Chapter 7 describes the security part and the implementation of WebSockets for real-time updates, assuming the use of GraphQL in a production environment.

Many "systematic learning" books begin by instilling prior knowledge of terms and concepts in the first half of the book, and then try them out in the second half. The second half of this book is devoted to actually trying it out.
If you already know the basics and want to write code right away, you may skip the first half. (In that case, you will need to start reading this book from Chapter 5.)

## Custom scalar type

In the book, this is the chapter "5.2.5 Custom scalar type.
GraphQL has standard scalars such as Boolean, Int, Float, String, and ID.
You can also define your own scalar (created as a DateTime type in this document).

Custom scalars appear in Chapter 5, where three methods are presented.
serialize
	→Process responses according to defined functions.
parseValue
	→Perform some processing before adding values to arguments sent from the client-side.
parseLiteral
	→Performs the process to be executed when the value sent by the client-side is a literal (hard code).

Each of these methods is used when a request with arguments is received from the client-side. The book starts explaining them out of the blue, so it will be easier to understand if you look at the documentation first.

[apollographql](https://www.apollographql.com/docs/apollo-server/schema/custom-scalars/)

## Installing MongoDB

In the book, this is chapter 5.4.1 MongoDB Installation.
I considered a Docker approach without an installer.
However, this method is not recommended because it affects the content of the explanation.
Please consider this method if you are able to solve the problem on your own to some extent.

Create a new docker-compose

```
version: '3'

services:
  mongo:
    image: mongo:5.0.3
    ports:
      - "27017:27017"
    volumes:
      - ./mongo:/docker-entrypoint-initdb.d
    environment:
      MONGO_INITDB_ROOT_USERNAME: user
      MONGO_INITDB_ROOT_PASSWORD: password
      MONGO_INITDB_DATABASE: example
      TZ: Asia/Tokyo
```

In the book, the environment variable for DB_HOST in .env is expressed as follows.

```
DB_HOST=mongodb://localhost:27017/<Your-Database-Name>
```

Replaces the above.

```
DB_HOST=mongodb://user:password@127.0.0.1:27017/example?authSource=admin
```

The reason for specifying "authSource=admin" is that if authSource is not specified, authSource defaults to defaultauthdb specified in the connection string.
If defaultauthdb is not specified, authSource defaults to admin. (Official link is attached below).

[mongodb](https://www.mongodb.com/docs/manual/reference/connection-string/#examples)

## githubAuth mutation

In the book, this is the chapter "5.5.3 Mutation of githubAuth".

Here, the process proceeds through user authentication with the Github API to obtain information.

This is the procedure for sending a githubAuth mutation from the client after obtaining the authentication code, but the access token is not returned as is.

This is because Github's API authentication is in a deprecated form with the existing code.
You can check it out at the following link

[github](https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param/)

As you can see from the contents, tokens are not included in the query parameters but are passed in the headers, so modify the code described. (In the book, lib.js is the target file).

lib.js
```
const requestGithubUserAccount = token => fetch(`https://api.github.com/user?access_token=${token}`) .then(res => res.json())
```

Rewrite this as follows

```
const requestGithubAccount = token =>
    fetch(`https://api.github.com/user`,
        {
            method: 'POST',
            headers: {
                'Authorization': `token ${token}`,
            }
        }
    )
```

Thank you for reading to the end.

As a first time GraphQL user, this book was once again a great book for me.

Hopefully, I am very much looking forward to new GraphQL books being published.