Top Banner
Migration microservices to GraphQL
26

Migration microservices to GraphQL

Apr 14, 2017

Download

Technology

Roman Krivtsov
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: Migration microservices to GraphQL

Migration microservices to

GraphQL

Page 2: Migration microservices to GraphQL

API

Page 3: Migration microservices to GraphQL

function getUser(name: string): User { return db.getUserByName(name);}

Typed request argumentes

Type of response

Page 4: Migration microservices to GraphQL

RPC (1960s, CORBA, XML, JSON, Thrift)

SOAP (WSDL, XML)

REST (HTTP)

Page 5: Migration microservices to GraphQL

REST

+ People got sick of SOAP

+ HTTP 1/1

+ Extra simplicity = fast development

+ Good for CRUD

Page 6: Migration microservices to GraphQL

Resources

POST /users

{name: "Homer"}

OPTIONS /users Access-Control-Request-Method:POST

Page 7: Migration microservices to GraphQL

Resources

GET /users?name=??

GET /users?name=Homer,Marge

GET /users?name=..&limit&offcet

GET /users?name&limit&token

• 1 nesting level• Characters escaping• 2048 characters

Page 8: Migration microservices to GraphQL

Resources

PUT/PATCH /users/1 {name: "Homer2"}

{ "name": "Homer", "children": [ { "name": "Bart", }, { "name": "Lisa" } ]}

Page 9: Migration microservices to GraphQL

Backend

app.post((req, res) => { const user = getUser(req.headers.user_id); const user = getUser(req.query.user_id); const user = getUser(req.path.user_id); const user = getUser(req.body.user);});

Page 10: Migration microservices to GraphQL

function getUser(name: string): User { return db.getUserByName(name);}

Typed request argumentes

Type of response

Page 11: Migration microservices to GraphQL

We are at wrong level of abstraction

Page 12: Migration microservices to GraphQL

GraphQL

• Get what you really need• Client specific• Transport agnostic• Rich type system• A lot of web features

Page 13: Migration microservices to GraphQL

REST GraphQL

Auto documented

+/-Swagger, ApiDoc,

Blueprint

+

Easy to use +/- +

Easy to develop

- +

Flexible - +

Powerful - +

Page 14: Migration microservices to GraphQL

But REST is still an industry standart

Page 15: Migration microservices to GraphQL

const QueryType = new GraphQLObjectType({ name: 'DataQuery', fields: () => ({ post: { type: PostType, args: { id: { name: 'id', type: GraphQLInt }, journal: { name: 'journal', description: 'The journal slug', type: GraphQLString }, post: { name: 'post', description: 'The post slug', type: GraphQLString } }, resolve: (user, {id, journal, post}) => { return Post.authorise(user, {id, journal, post}) .then(post => post && post.toJSON()); } } })});

Page 16: Migration microservices to GraphQL
Page 17: Migration microservices to GraphQL

• JSON Schema• Good specification• Bad ecosystem

Page 18: Migration microservices to GraphQL

"/users/findByName": { "get": { "description": "Multiple status values can be provided with comma separated strings", "operationId": "findUsersByStatus", "produces": ["application/xml", "application/json"], "parameters": [{ "name": "name", "in": "query", "description": "User name", "required": true, "type": "array", "items": {"type": "string"}, }], "responses": { "200": { "description": "successful operation", "schema": {"type": "array", "items": {"$ref": "#/definitions/User"}} }, "400": {"description": "Invalid status value"} }, "security": [{"store_auth": ["read:users"]}] }

Page 19: Migration microservices to GraphQL

Building types

Page 20: Migration microservices to GraphQL

new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: () => ({ get_user_id: { type: UserType, args: { id: { name: 'id', type: GraphQLInt } }, resolve: (id) => buildRequestWithSwagger() } }) })});

GET /user/123

Swagger parameters

Page 21: Migration microservices to GraphQL

Mutations

x-mutationPOST/PUT/DELETE

Page 22: Migration microservices to GraphQL

Gateway

GQL request

Adapter

ms1

ms2

REST request

GQL/REST

GQL/REST

function getUser(name: string): User { return db.getUserByName(name);}

Page 23: Migration microservices to GraphQL

Frontend

Smart caching:GET /usersGET /user/123

Helpful tools and debugging(Client knows about our schema and types)

Get only data that you need

Page 24: Migration microservices to GraphQL

Frontend

• Cursor connections• Subscriptions• Batching• Defered delivering• Fragments composition

Page 25: Migration microservices to GraphQL

PrecautionsNo GraphQL types namespaces

campaigns_get_campaigns_shop_shop_id_products_ids

No validation for input types

lokka

ReactApolloVanila

Page 26: Migration microservices to GraphQL

Thanks

github.com/yarax/typelint

twiitter.com/raxpost

yarax.ru