GraphQL com GoLang
GraphQLSP 01
Schema First vs Code First
Schema First
- Design the schema first (duuuh)
- A tool reads the schema and generates the code
- Types are already defined in code level (no interface{} types)
- Needs a configuration file to determine how to generate code
- Library / Tool: https://github.com/99designs/gqlgen
Code First
- Define the models in code level
- Types can only be inferred by runtime (lots of interface{} type)
- Schema is generated at runtime
- Needs some workarrounds for cyclic reference
- Library / Tool: https://github.com/graphql-go/graphql
![](./GraphQL-Golang_files/giphy.gif)
Schema First
![](./GraphQL-Golang_files/73013032-40679200-3df6-11ea-8ebc-f7ccc1da48de.png)
GraphQL Model
![](./GraphQL-Golang_files/73013078-57a67f80-3df6-11ea-84e3-97697c2ff086.png)
Generated GoLang Model
*developer calls gqlgen*
Code First
![](./GraphQL-Golang_files/73013221-a9e7a080-3df6-11ea-9c6f-9fa8f8c07828.png)
![](./GraphQL-Golang_files/73013286-c71c6f00-3df6-11ea-817b-1ee2b8da73a9.png)
Declared GraphQL Model
Declared Golang Model
Code First Design
Pokemon GraphQL
Based on Javascript
Pokemon Gopher
- Has a cyclic reference (pokemons have evolutions)
- Search Pokemon by Name or ID
- List all first N Pokemon
- We have to deal with interface{} types
Pokemon Gopher
Go Models
![](./GraphQL-Golang_files/73014127-63934100-3df8-11ea-9b37-b86b001c02b0.png)
![](./GraphQL-Golang_files/73014261-99382a00-3df8-11ea-8ad4-b8136d532741.png)
Pokemon Gopher
GraphQL Models
![](./GraphQL-Golang_files/73014589-32ffd700-3df9-11ea-9096-708aa198b061.png)
Pokemon Gopher
GraphQL Models
![](./GraphQL-Golang_files/73014656-532f9600-3df9-11ea-9e42-e2ba60f932ab.png)
Pokemon Gopher
Cyclic Reference Issue
![](./GraphQL-Golang_files/73014817-9ee23f80-3df9-11ea-9d53-db027241d8c9.png)
Pokemon model has evolutions which ...
are Pokemons as well!
In Javascript, that would work fine due Lazy References.
But GoLang does not have lazy references
![](./GraphQL-Golang_files/giphy(1).gif)
![](./GraphQL-Golang_files/giphy(2).gif)
![](./GraphQL-Golang_files/giphy(3).gif)
![](./GraphQL-Golang_files/73016715-2a110480-3dfd-11ea-9e6e-9f535d709dbf.png)
Pokemon Gopher
Cyclic Reference Issue
The golang init function is a built-in that runs before any code
![](./GraphQL-Golang_files/73018346-3c407200-3e00-11ea-8ea6-d00e4e8b665d.png)
Pokemon Gopher
Deep into Resolver
![](./GraphQL-Golang_files/73018281-16b36880-3e00-11ea-848d-fcc70d3b5546.png)
Pokemon Gopher
Deep into Resolver
Pokemon Gopher
Deep into Resolver
![](./GraphQL-Golang_files/73018434-67c35c80-3e00-11ea-8e8c-943011aa566c.png)
- Casting p.Source to models.Pokemon so we can access its fields
- Result in variable d and a boolean ok if the cast was successfull
![](./GraphQL-Golang_files/73018560-a527ea00-3e00-11ea-818a-dbc6304be494.png)
- If the casting wasn't ok, the received model is wrong
- Return an error saying the model wasn't the one we expected
![](./GraphQL-Golang_files/73018624-c983c680-3e00-11ea-878c-378fc1297f3e.png)
![](./GraphQL-Golang_files/73018835-27181300-3e01-11ea-8a02-0d4435ace071.png)
If everything is alright we can fetch the evolutions from the database
![](./GraphQL-Golang_files/73018981-6e9e9f00-3e01-11ea-8a5f-e5601b4111c7.png)
Pokemon Gopher
Queries
Pokemon Gopher
Queries
![](./GraphQL-Golang_files/73019090-b02f4a00-3e01-11ea-87d9-484ac0980a3f.png)
![](./GraphQL-Golang_files/73019206-e79df680-3e01-11ea-8258-ec16684f4dac.png)
Pokemon Gopher
Queries
![](./GraphQL-Golang_files/73019262-00a6a780-3e02-11ea-9c1b-9da5afe26db2.png)
Pokemon Gopher
Schema
![](./GraphQL-Golang_files/73019356-321f7300-3e02-11ea-82d6-aec33dd6823c.png)
Pokemon Gopher
Program
![](./GraphQL-Golang_files/giphy(4).gif)
Running
![](./GraphQL-Golang_files/73019519-7d398600-3e02-11ea-8e68-cb077094b97d.png)
Questions?
- Twitter @lucasteske
- Telegram @lucasteske
- Github racerxdl
- Telegram @lucasteske
- Github racerxdl
Source Code: https://github.com/racerxdl/pokemon-gopher
Thanks for Watching!