GRAPHQL AS AN ALTERNATIVE APPROACH TO - REST - CODEMONSTERS NOV 2018
←
→
Page content transcription
If your browser does not render page correctly, please read the page content below
About me Luis Weir CTO at Capgemini UK Oracle Ace Director & Groundbreaker Ambassador luis.weir@capgemini.com uk.linkedin.com/in/lweir http://www.soa4u.co.uk Want the slides? I’ll share via: @luisw19 tinyurl.com/eapim18 apiplatform.cloud/ Goes to Print Q2 2019 Released Q2 2018 Latest articles: • The 7 Deadly Sins of API Design • Five Minutes with Luis Weir • Setting the vision, strategy and direction — the CTO’s role • 2nd vs 3rd Generation API Platforms - A Comprehensive • How can you design, deploy and manage your APIs? Comparison • The Spotify's Engineering Culture. My interpretation and summary • Podcast: Are Microservices and APIs Becoming SOA 2.0? • A comparison of API Gateways communication styles • 3rd-Generation API Management: From Proxies to Micro-Gateways • Is BPM Dead, Long Live Microservices? • Oracle API Platform Cloud Service Overview
GraphQL as an alternative appraoch to REST GraphQL – context & key 01 concepts 02 Demos 03 GraphQL vs REST PoV 04 Conclusions ©32018 Capgemini. All rights reserved. @luisw19
6 01 Why GraphQL? Source: https://dev-blog.apollodata.com/graphql-vs-rest- 5d425123e34b by Sashko Stubailo 6 @luisw19
7 01 GraphQL - Background • Created by Facebook in 2012 to get around a common constraints in the REST approach when fetching data • Publicly released in 2015 • GraphQL Schema Definition Language (SDL) added to spec in Feb’18 https://GraphQL.org Latest release: http://facebook.github.io/graphql Latest draft: http://facebook.github.io/graphql/draft/ 7 @luisw19
8 01 GraphQL – What is it NOT? necessarily a in spite of its name, it replacement for REST. has nothing to do with Both can work together Graphs DBs a query language for a databases A silver Bullet 8 Roy from the IT Crowed à @luisw19
9 01 GraphQL – What is it then? A consumer oriented query language, a strongly typed schema language and a runtime to implement GraphQL services. Define Schema Quickly write and Get exactly what run queries you asked for type Country { { id: ID! { "data": { name: String! getCountries(name:"great") "countries": [ code: String! { { } name "name": "United type query { } Kingdom" countries: } } [Country] ] } } } GraphQL Service GraphQL Client GraphQL Client 9 @luisw19
10 01 Who is using it? Lots of organisations are embracing GraphQL: http://graphql.org/users 10 @luisw19
11 01 Increasing rapidly in Popularity GraphQL REST API OData Jan 2016 Dec 2018 https://trends.google.com/trends/explore?date=2016-01-01%202018-12-01&q=GraphQL,REST%20API,OData 11 @luisw19
12 01 Let’s put it into perspective GraphQL REST API OData WSDL trend Feb 2004 Sep 2018 https://trends.google.com/trends/explore?date=2004-01-10%202018-11-30&q=GraphQL,REST%20API,OData,WSDL 12 @luisw19
13 01 GraphQL – Key Concepts There are 5 key characteristics of GraphQL that are important to understand: 1 2 3 4 5 Hierarchical View-centric Strongly-typed Introspective Version-Free Queries as By design built to A GraphQL The type system GraphQL takes a hierarchies of satisfy frontend server defines a itself is strong opinion data definitions, application specific type queryable. Tools on avoiding shaped just how requirements. system. Queries are built around versioning by data is expected are executed this capability. providing the to be retuned. within this tools for the context. continuous evolution. 13 @luisw19
14 01 GraphQL – Anatomy GraphQL (Data) Types Schema (Object, Input, Scalar, Definition Enum, Interface, Union) Language GraphQL have Server Implementation Queries (operation type) Define GraphQL Types Schema have Execute Resolvers Mutations (operation type) f(x), f(x), f(x) have Subscriptions (operation type) 14 @luisw19
15 01 GraphQL – Anatomy GraphQL (Data) Types Schema (Object, Input, Scalar, Definition Enum, Interface, Union) Language GraphQL have Server Implementation Queries 1 Define (operation type) GraphQL Types Schema have Execute Resolvers Effectively the blueprint of Mutations f(x), f(x), f(x) a GraphQL API, it defines (operation type) the Types, Queries and Mutations supported in a have GraphQL service Subscriptions (operation type) 15 @luisw19
16 01 GraphQL – Anatomy GraphQL 2 (Data) Types (Object, Input, Scalar, Object, Input, Scalar, Enumeration, Schema Definition Enum, Interface, Union) Interfaces and Unions are all types Language to define data structures, which are GraphQL have used in Operation types. Server Implementation Queries (operation type) Define GraphQL Types Schema have Execute Resolvers Mutations (operation type) f(x), f(x), f(x) have Subscriptions (operation type) 16 @luisw19
17 01 GraphQL – Anatomy GraphQL (Data) Types Schema (Object, Input, Scalar, Definition Enum, Interface, Union) Language GraphQL have Entry point for operationsServer that fetch 3 data (read / search). Note that a Implementation Queries single Query type can define (operation type) GraphQL Define multiple query operations. Types Schema have Execute Resolvers Mutations (operation type) f(x), f(x), f(x) have Subscriptions (operation type) 17 @luisw19
18 01 GraphQL – Key Concepts GraphQL (Data) Types Schema (Object, Input, Scalar, Definition Enum, Interface, Union) Language GraphQL have Server Implementation Queries (operation type) Define GraphQL Types Schema have 4 Execute Resolvers Mutations (operation type) f(x), f(x), f(x) Entry point for operations that create/update data via a GraphQL service. Note that a single Mutation have type can define multiple mutation operations. Subscriptions (operation type) 18 @luisw19
19 01 GraphQL – Anatomy GraphQL (Data) Types Schema (Object, Input, Scalar, Definition Enum, Interface, Union) Language GraphQL have Server Implementation Queries (operation type) Define GraphQL Types Schema have Execute Resolvers Mutations (operation type) f(x), f(x), f(x) have 5 *new* Pub/sub system for near- Subscriptions realtime updates. Unlike queries or (operation type) mutations, it can deliver more than one result via push. 19 @luisw19
20 01 GraphQL – Anatomy GraphQL (Data) Types Schema (Object, Input, Scalar, Definition Enum, Interface, Union) Language GraphQL have Server Functions that define how each Implementation Queries field, within a GraphQL Query or (operation type) Define Operation is to be acted upon. GraphQL Types Schema have Execute Resolvers Mutations (operation type) f(x), f(x), f(x) 6 have Subscriptions (operation type) 20 @luisw19
21 01 GraphQL Schema Cheat Sheet https://github.com/sogko/graphql-schema-language-cheat-sheet 21 @luisw19
22 02 Simple GraphQL Query Demo (I) - Mock https://github.com/luisw19/graphql-samples/tree/master/graphql- countries-part1 Query Operation {JSON} [HTTP/POST] GraphQL Endpoint { { query data {JSON} GraphQL Schema } } http://.../graphiql Graphiql GraphQL Client GraphQL Service GraphQL Server: Browser Apollo/Express 22 @luisw19 Apollo Express: https://github.com/apollographql/apollo-server
23 02 Simple GraphQL Query Demo (II) – REST Backend https://github.com/luisw19/graphql-samples/tree/master/graphql- countries-part2 [HTTP/GET] https://restcountries.eu/rest/v2/{resource} Query Operation {JSON} [HTTP/POST] GraphQL Endpoint REST COUNTRIES { { data {JSON} {JSON} query GraphQL Schema } } Graphiql GraphQL Client GraphQL Service GraphQL Server: Browser Apollo/Express 23 @luisw19
24 02 Simple GraphQL Query Demo (III) - Mutation https://github.com/luisw19/graphql-samples/tree/master/graphql- countries-part3 [HTTP/GET] https://restcountries.eu/rest/v2/{resource} Query Operation {JSON} [HTTP/POST] GraphQL Endpoint REST COUNTRIES { { data {JSON} {JSON} query GraphQL Schema } } [HTTP/POST] http://localhost:8000/{resource} Graphiql RequestBIN GraphQL Client GraphQL Service {JSON} Docker Container GraphQL Server: Browser Apollo/Express 24 @luisw19
25 02 Simple GraphQL Query Demo (IV) – API Composition Not merged yet… (soon) [HTTP/GET] https://restcountries.eu/rest/v2/{resource} Query Operation {JSON} [HTTP/POST] GraphQL Endpoint REST COUNTRIES { { data {JSON} {JSON} query GraphQL Schema } } [HTTP/POST] Graphiql https://www.google.co.uk/search?q={search} GraphQL Client GraphQL Service {JSON} GraphQL Server: Browser Apollo/Express 25 @luisw19
26 03 GraphQL vs REST (++) Brilliant (+) Good (~) Neutral (it depends) (-) Not very good GraphQL REST (--) It sucks! Developer Experience API Gateway API Composition (design and consume APIs) (API routing, security, policies) (query data from multiple sources) API Design Build API Gateway Try API API API (-) Existing Gateways have rich (++) Usage: Best usage experience (++) Perfectly suited for API support for REST, not yet GraphQL - for developers (Graphiql is brilliant!) composition. Each field can be fetch but could be used. Alternative is to (~) API-first design: Tooling evolving (in parallel) from any source in a use a GraphQL Service as API (build a service to mock). single query. Gateway. (~) Usage: depends on the quality of (++) API Gateways take away from (--) The nature of REST makes it difficult API definition and documentation REST endpoints common tasks (e.g. to model resources that combine data (+) API-first design: good tools OAuth, API keys, throttling, structures from multiple sources. HATEOAS available (e.g. Apiary, Swagger results in chattiness. security). Hub). 26 @luisw19
27 03 GraphQL vs REST (++) Brilliant (+) Good (~) Neutral (it depends) (-) Not very good GraphQL REST (--) It sucks! Authentication / Authorization Caching Versioning Authorisation App Client Server Cache Network Caching 1 Client 2 Resource Back Resource End (-) Standards like OAuth, OpenID (--) Network: unsuitable as there is (++) Best practices are clear. can be used however because all ops a common URL for all operations. Versioning should be avoided and can be accessed by single URI, (+) Service: possible based on tools are provided (e.g. deprecation custom authorisation is typically Object Type (even fields) and in of fields) for continuous evolution. required. mem cache like REDIS. (++) Network: Caching is easy as each resource is a unique URI. (++) Major standards (OAuth 2, (-) Best practice less clear, in Common tools can be used (e.g. OpenId) supported by API Gateways practice URI based versioning very CDNs). and frameworks. popular although not encouraged. (+) Service: It’s equally possible at service Level. 27 @luisw19
28 03 GraphQL vs REST (completely subjective!) +++++++ (7) à But it will only improve! GraphQL ~ (1) ---- (4) ++++++++ (8) REST ~~ (2) --- (3) 28 @luisw19
04 Conclusions Still early days but GraphQL has huge potential GraphQL takes away many of the headaches of dealing with 1 REST from a client side -specially around complex queries (against multiple sources). However tooling specially around API Design and API Gateways is still evolving. So bear this in mind when considering GraphQL. GraphQL and REST can work nicely together There are thousands of REST APIs (external and internal) and 2 REST still is a viable and popular option. Instead of boiling the ocean, as Roy said, GraphQL is not necessarily a replacement for REST. As shown in this presentation both can be complementary and work together nicely. There is no silver bullets –do your own research 3 There is tons of information available in the GraphQL Communities page. Explore it, learn about it and adopt it based on your own criteria and requirements. And hope this presentation helps in the process! 29 © 2018 Capgemini. All rights reserved. @luisw19 29
30 Resources • GraphQL as an alternative approach to Rest recording at Devoxx’18 London https://www.youtube.com/watch?v=hJOOdCPlXbU • Github repository with the GraphQL tutorials https://github.com/luisw19/graphql-samples • Related articles: • GraphQL with Oracle Database and node-oracledb by Christopher Jones https://blogs.oracle.com/opal/demo%3a-graphql-with-node-oracledb • GraphQL+OracleDB by Steven B https://github.com/cloudsolutionhubs/oracledb-graphql-demo 30 @luisw19
About Capgemini With more than 190,000 people, Capgemini is present in over 40 countries and celebrates its 50th Anniversary year in 2018. A global leader in consulting, technology and outsourcing services, the Group reported 2016 global revenues of EUR 12.5 billion. Together with its clients, Capgemini creates and delivers business, technology and digital solutions that fit their needs, enabling them to achieve innovation and competitiveness. A deeply multicultural organization, Capgemini has developed its own way of working, the Collaborative Business Experience™, and draws on Rightshore®, its worldwide delivery model. Learn more about us at www.capgemini.com This message contains information that may be privileged or confidential and is the property of the Capgemini Group. Copyright © 2018 Capgemini. All rights reserved. This message is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please Rightshore® is a trademark belonging to Capgemini. notify the sender immediately and delete all copies of this message. 31 @luisw19
You can also read