GraphQL ou APIs RESTful - DevDay 2017

78

Transcript of GraphQL ou APIs RESTful - DevDay 2017

@marcosbrizeno

O que é GraphQL

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/

Type Query {

me: User

}

Type User {

id: ID

name: String

}

Type Query {

me: User

}

Type User {

id: ID

name: String

}

{ me { name }}

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

Type Query {

me: User

}

Type User {

id: ID

name: String

}

{ me { name }}

Como funciona na prática?

POST http://localhost/graphql

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

Como funciona na prática?

POST http://localhost/graphql

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

Response

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

De onde veio GraphQL?

● Protótipo em 2012

https://youtu.be/zVNrqo9XGOs

De onde veio GraphQL?

● Protótipo em 2012● 2015 Falaram sobre GraphQL

https://youtu.be/zVNrqo9XGOs

De onde veio GraphQL?

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

https://youtu.be/zVNrqo9XGOs

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

Explorando APIs GraphQL

Com GraphiQL!

GraphiQL is a graphical interactive in-browser GraphQL

IDE.

https://github.com/graphql/graphiql

https://github.com/graphql/graphiql

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

GraphiQL

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

Show me the Code!

Acessando a

API do Github

Quais linguagens tenho utilizado nos

meus últimos repositórios?

API Rest

GET /user/repos

Response

API Rest

GET /user/repos

Response

API Rest

GET /repos/:owner/:repo/languages

Response

API Rest

10x

API GraphQL do Github

https://developer.github.com/v4

API GraphQL do Github

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

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

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

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

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

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

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

Show me the Code! [2]

Criando uma

API GraphQL

Gemfile

Gemfile

$> bundle

Gemfile

$> bundle$> rails g graphql:install

Gemfile

$> bundle$> rails g graphql:install

routes.rb

app/controllers/graphql_controller.rb

app/controllers/graphql_controller.rb

app/graphql/leave_management_schema.rb

app/graphql/leave_management_schema.rb

app/graphql/types/query_type.rb

app/graphql/types/query_type.rb

app/graphql/types/query_type.rb

app/graphql/types/query_type.rb

app/graphql/types/leave_query_type.rb

app/graphql/types/leave_query_type.rb

app/graphql/types/leave_query_type.rb

app/graphql/types/leave_query_type.rb

app/graphql/types/consultant_query_type.rb

app/graphql/types/consultant_query_type.rb

http://localhost:3001/graphiql

E aí?

GraphQL

ou

REST

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

consumidores

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

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

resultado no google

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

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

definido, é fácil reutilizá-lo

GraphiQL facilita muito a Developer Experience- Consumidores podem

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

GraphQL facilita muito a Developer Experience- APIs REST precisam

que você implemente customizações da resposta

GraphQL + BFF = <3- GraphQL reduz o

payload (essencial para apps mobile)

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

tão direto assim

Beleza!

Quero usar GraphQL, #comofaz?

Preciso de uma linguagemnova só pro GraphQL?

Preciso de uma linguagemnova só pro GraphQL?

Não!

http://graphql.org/code/

Preciso de um servidorsó pro GraphQL?

Preciso de um servidorsó pro GraphQL?

Não!

REST API

GraphQL API

GraphQL precisa estar junto com os dados?

GraphQL precisa estar junto com os dados?

Não!

POST /graphql

GraphQL é apenasuma (fina) camada

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

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

Valeu!@marcosbrizeno