Presented at Zwolle meetup: https://www.meetup.com/webdevzwolle/events/264744930/
--
In this talk you'll hear what GraphQL is, what the benefits are, and how ING Investments implemented a pilot with GraphQL, both on the front-end and back-end.
Overview
● A dataquery language specification
● Introduced by Facebook, open source 2015
● Implemented in JavaScript, Perl, Python, Ruby,
Java, C#, Scala, Go, PHP, etc
GraphQL pilot at ING Investments
Companies using
GraphQL include:
@legkoletat
Benefits of GraphQL
●Typed contract layer
● Several resources in single call
● Granular control over the data composition
● Arguments can be passed on any level of the request
● Versioning and documentation
● Solid ecosystem (GraphiQL, GraphQL Playground, eslint plugin, etc)
GraphQL pilot at ING Investments @legkoletat
Terminology
GraphQL pilot atING Investments
Types
Queries
Mutations
Client:
1. Map variables to query
2. Do a POST call to API
3. Handle response(s)
Server:
● Resolver functions
● Shared context data
@legkoletat
Subscriptions
10.
GraphQL types
● Int,Float, String, Boolean, ID
● Union types
● Lists
● Scalar types
● Non-Null markers
● __abc - system fields
GraphQL pilot at ING Investments
scalar Date
enum MessageStatus {
SENT
VIEWED
DELETED
}
type Picture {
width: Int!
height: Int!
url: String
status: MessageStatus
}
union Message = Text | Picture | Sound;
type Chat {
name: String!
picture: Picture
dateCreated: Date
messages: [Message]!
}
/simple-messenger-schema.graphql
@legkoletat
11.
GraphQL queries
● LikeGET in REST
● Name + Structure + Arguments
● Fragments
● @deffer for slow requests
● @deprecated and other
annotations
query {
getUsers(first:2) {
name
groups {
id,
title
...latest Post
}
}
}
GraphQL pilot at ING Investments @legkoletat
12.
GraphQL mutations
● LikePOST/DELETE/PUT
in REST
● Can return back the data
mutation newUserMutation {
UserCreate(firstName: "John", lastName: "Snow") {
id
isKing
}
}
mutation DeleteUser {
deleteUser(id: 1234) {
dateCreated
}
}
GraphQL pilot at ING Investments @legkoletat
Change:
● 1 callinstead of 2
● 1 endpoint instead of 2
Query:
query {
instrumentDetails
instrumentPriceHistory
}
Pilot page
GraphQL pilot at ING Investments @legkoletat
Architecture
● Java Springmicroservices
● Experience service
● graphql-java lib
GraphQL pilot at ING Investments
Experience service
Frontends
Service A
Service B
Service C
GraphQL
REST
REST
@legkoletat
27.
Graphql-spqr
● Schema firstvs Code-first
● graphql-spqr provides code first approach
● Code-first => no *.grapql file in backend
GraphQL pilot at ING Investments
class UserService {
@GraphQLQuery(name = "user")
public User getById(@GraphQLArgument(name = "id") Integer id) {
@legkoletat
28.
Errors
@Data
public class AppGraphQLErrorimplements GraphQLError {
private String message;
private int errorCode;
private ErrorType errorType;
private List<SourceLocation> locations;
// ...
}
GraphQL pilot at ING Investments
Map to custom error
Feign service
GraphQL: status 200 with
“errors” property
401/403/500 HTTP Code
@legkoletat
Further challenges
GraphQL pilotat ING Investments
● Adding mobile client
● Better tracing and logging
● Caching
● Possible Schema duplication
● Reuse of the REST services
@legkoletat