Top Banner
78

GraphQL ou APIs RESTful - DevDay 2017

Jan 29, 2018

Download

Technology

Marcos Brizeno
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: GraphQL ou APIs RESTful - DevDay 2017
Page 2: GraphQL ou APIs RESTful - DevDay 2017

@marcosbrizeno

Page 3: GraphQL ou APIs RESTful - DevDay 2017

O que é GraphQL

Page 4: GraphQL ou APIs RESTful - DevDay 2017

GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data.

http://graphql.org/learn/

Page 5: GraphQL ou APIs RESTful - DevDay 2017

Type Query {

me: User

}

Type User {

id: ID

name: String

}

Page 6: GraphQL ou APIs RESTful - DevDay 2017

Type Query {

me: User

}

Type User {

id: ID

name: String

}

{ me { name }}

Page 7: GraphQL ou APIs RESTful - DevDay 2017

{ "me": { "name": "Luke Skywalker" }}

Type Query {

me: User

}

Type User {

id: ID

name: String

}

{ me { name }}

Page 8: GraphQL ou APIs RESTful - DevDay 2017

Como funciona na prática?

POST http://localhost/graphql

{ "query" : "query { me { name } }"}

Page 9: GraphQL ou APIs RESTful - DevDay 2017

Como funciona na prática?

POST http://localhost/graphql

{ "query" : "query { me { name } }"}

Response

{ "data" : { "me" : { "name" : "Luke Skywalker" } }}

Page 10: GraphQL ou APIs RESTful - DevDay 2017

De onde veio GraphQL?

● Protótipo em 2012

https://youtu.be/zVNrqo9XGOs

Page 11: GraphQL ou APIs RESTful - DevDay 2017

De onde veio GraphQL?

● Protótipo em 2012● 2015 Falaram sobre GraphQL

https://youtu.be/zVNrqo9XGOs

Page 12: GraphQL ou APIs RESTful - DevDay 2017

De onde veio GraphQL?

● Protótipo em 2012● 2015 Falaram sobre GraphQL● Mais tarde (Julho) foi OSS (JavaScript)

https://youtu.be/zVNrqo9XGOs

Page 13: GraphQL ou APIs RESTful - DevDay 2017

De onde veio GraphQL?

● Protótipo em 2012● 2015 Falaram sobre GraphQL● Mais tarde (Julho) foi OSS (JavaScript)● 2016 Production Ready

https://youtu.be/zVNrqo9XGOs

Page 14: GraphQL ou APIs RESTful - DevDay 2017

Explorando APIs GraphQL

Com GraphiQL!

Page 15: GraphQL ou APIs RESTful - DevDay 2017

GraphiQL is a graphical interactive in-browser GraphQL

IDE.

https://github.com/graphql/graphiql

Page 16: GraphQL ou APIs RESTful - DevDay 2017

https://github.com/graphql/graphiql

Page 17: GraphQL ou APIs RESTful - DevDay 2017

http://graphql.org/swapi-graphql/

Page 18: GraphQL ou APIs RESTful - DevDay 2017

GraphiQL

http://graphql.org/swapi-graphql/

Page 19: GraphQL ou APIs RESTful - DevDay 2017

Show me the Code!

Acessando a

API do Github

Page 20: GraphQL ou APIs RESTful - DevDay 2017

Quais linguagens tenho utilizado nos

meus últimos repositórios?

Page 21: GraphQL ou APIs RESTful - DevDay 2017

API Rest

GET /user/repos

Response

Page 22: GraphQL ou APIs RESTful - DevDay 2017

API Rest

GET /user/repos

Response

Page 23: GraphQL ou APIs RESTful - DevDay 2017

API Rest

GET /repos/:owner/:repo/languages

Response

Page 24: GraphQL ou APIs RESTful - DevDay 2017

API Rest

10x

Page 25: GraphQL ou APIs RESTful - DevDay 2017

API GraphQL do Github

https://developer.github.com/v4

Page 26: GraphQL ou APIs RESTful - DevDay 2017

API GraphQL do Github

https://developer.github.com/v4/explorer/

Page 27: GraphQL ou APIs RESTful - DevDay 2017

https://developer.github.com/v4/explorer/

Page 28: GraphQL ou APIs RESTful - DevDay 2017

https://developer.github.com/v4/explorer/

Page 29: GraphQL ou APIs RESTful - DevDay 2017

https://developer.github.com/v4/explorer/

Page 30: GraphQL ou APIs RESTful - DevDay 2017

https://developer.github.com/v4/explorer/

Page 31: GraphQL ou APIs RESTful - DevDay 2017

https://developer.github.com/v4/explorer/

Page 32: GraphQL ou APIs RESTful - DevDay 2017

https://developer.github.com/v4/explorer/

Page 33: GraphQL ou APIs RESTful - DevDay 2017

Show me the Code! [2]

Criando uma

API GraphQL

Page 34: GraphQL ou APIs RESTful - DevDay 2017

Gemfile

Page 35: GraphQL ou APIs RESTful - DevDay 2017

Gemfile

$> bundle

Page 36: GraphQL ou APIs RESTful - DevDay 2017

Gemfile

$> bundle$> rails g graphql:install

Page 37: GraphQL ou APIs RESTful - DevDay 2017

Gemfile

$> bundle$> rails g graphql:install

routes.rb

Page 38: GraphQL ou APIs RESTful - DevDay 2017

app/controllers/graphql_controller.rb

Page 39: GraphQL ou APIs RESTful - DevDay 2017

app/controllers/graphql_controller.rb

Page 40: GraphQL ou APIs RESTful - DevDay 2017

app/graphql/leave_management_schema.rb

Page 41: GraphQL ou APIs RESTful - DevDay 2017

app/graphql/leave_management_schema.rb

Page 42: GraphQL ou APIs RESTful - DevDay 2017

app/graphql/types/query_type.rb

Page 43: GraphQL ou APIs RESTful - DevDay 2017

app/graphql/types/query_type.rb

Page 44: GraphQL ou APIs RESTful - DevDay 2017

app/graphql/types/query_type.rb

Page 45: GraphQL ou APIs RESTful - DevDay 2017

app/graphql/types/query_type.rb

Page 46: GraphQL ou APIs RESTful - DevDay 2017

app/graphql/types/leave_query_type.rb

Page 47: GraphQL ou APIs RESTful - DevDay 2017

app/graphql/types/leave_query_type.rb

Page 48: GraphQL ou APIs RESTful - DevDay 2017

app/graphql/types/leave_query_type.rb

Page 49: GraphQL ou APIs RESTful - DevDay 2017

app/graphql/types/leave_query_type.rb

Page 50: GraphQL ou APIs RESTful - DevDay 2017

app/graphql/types/consultant_query_type.rb

Page 51: GraphQL ou APIs RESTful - DevDay 2017

app/graphql/types/consultant_query_type.rb

Page 52: GraphQL ou APIs RESTful - DevDay 2017

http://localhost:3001/graphiql

Page 53: GraphQL ou APIs RESTful - DevDay 2017

E aí?

GraphQL

ou

REST

Page 54: GraphQL ou APIs RESTful - DevDay 2017

Ninguém usa GraphQL- O Github já tá usando! - Retrabalho para novos

consumidores

Page 55: GraphQL ou APIs RESTful - DevDay 2017

O Github já usa GraphQL!- A versão nova da API

(v4) não é REST- Github v3 é o primeiro

resultado no google

Page 56: GraphQL ou APIs RESTful - DevDay 2017

GraphQL é mais fácil de evoluir- Evoluir os modelos do

grafo é mais fácil do que criar uma nova versão da API

- Mudanças destrutivas continuam quebrando os clientes, e GraphQL não versiona

Page 57: GraphQL ou APIs RESTful - DevDay 2017

GraphQL é mais fácil de codar- Uma vez que o grafo é

definido, é fácil reutilizá-lo

Page 58: GraphQL ou APIs RESTful - DevDay 2017

GraphiQL facilita muito a Developer Experience- Consumidores podem

explorar os dados e criar suas consultas(além de ver a documentação)

Page 59: GraphQL ou APIs RESTful - DevDay 2017

GraphQL facilita muito a Developer Experience- APIs REST precisam

que você implemente customizações da resposta

Page 60: GraphQL ou APIs RESTful - DevDay 2017

GraphQL + BFF = <3- GraphQL reduz o

payload (essencial para apps mobile)

Page 61: GraphQL ou APIs RESTful - DevDay 2017

GraphQL exige mudança na mentalidade- Pensar em grafos não é

tão direto assim

Page 62: GraphQL ou APIs RESTful - DevDay 2017

Beleza!

Quero usar GraphQL, #comofaz?

Page 63: GraphQL ou APIs RESTful - DevDay 2017

Preciso de uma linguagemnova só pro GraphQL?

Page 64: GraphQL ou APIs RESTful - DevDay 2017

Preciso de uma linguagemnova só pro GraphQL?

Não!

Page 65: GraphQL ou APIs RESTful - DevDay 2017

http://graphql.org/code/

Page 66: GraphQL ou APIs RESTful - DevDay 2017

Preciso de um servidorsó pro GraphQL?

Page 67: GraphQL ou APIs RESTful - DevDay 2017

Preciso de um servidorsó pro GraphQL?

Não!

Page 68: GraphQL ou APIs RESTful - DevDay 2017

REST API

GraphQL API

Page 69: GraphQL ou APIs RESTful - DevDay 2017

GraphQL precisa estar junto com os dados?

Page 70: GraphQL ou APIs RESTful - DevDay 2017

GraphQL precisa estar junto com os dados?

Não!

Page 71: GraphQL ou APIs RESTful - DevDay 2017

POST /graphql

Page 72: GraphQL ou APIs RESTful - DevDay 2017

GraphQL é apenasuma (fina) camada

Page 73: GraphQL ou APIs RESTful - DevDay 2017

http://graphql.org/learn/thinking-in-graphs/

Page 74: GraphQL ou APIs RESTful - DevDay 2017
Page 75: GraphQL ou APIs RESTful - DevDay 2017
Page 76: GraphQL ou APIs RESTful - DevDay 2017

https://martinfowler.com/articles/born-for-it.html

Page 77: GraphQL ou APIs RESTful - DevDay 2017
Page 78: GraphQL ou APIs RESTful - DevDay 2017

Valeu!@marcosbrizeno