build federated graphql schemas in java using osgi and jahia · osgi modules with support for...

34
Build federated GraphQL schemas in Java using OSGi and Jahia

Upload: others

Post on 19-Mar-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Build federated GraphQL schemas in Java using OSGi and Jahia

Page 2: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

About me

Senior Full Stack Developer at Jahia Solutions

Apache Unomi Committer

https://github.com/DameniMilo

2

Page 3: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Context

3

Page 4: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

OSGi

https://www.osgi.org

Java framework

Hot deployment of modules

Used in many domains

4

Page 5: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

GraphQL

https://graphql.org/

Open Source

Query language for APIs

Flexible

5

Page 6: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Jahia

https://www.jahia.com

Open Source CMS

Modular runtime (OSGi)

GraphQL Gateway

6

Page 7: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

How does this work together

GraphQL Gateway

GraphQL Extension 1

GraphQL Extension 2

7

Page 8: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Federation VS Stitching

8

Page 9: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

What is schema stitching?

“ Schema stitching is the process of creating a single GraphQL schema

from multiple underlying GraphQL APIs. “

9

Page 10: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

So, what’s the problem?● Monolithic

● Multiple requests

● “Glue” code

● Extend query only

10

Page 11: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Maybe better with a little example

type Movie {id: ID!title: Stringoverview: String

}type Query {

movieById(id: ID!): Movie}

11

Page 12: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Let’s extend it

type Actor { id: ID! name: String}type Query { actorById(id: ID!): Actor actorsByMovieId(id: ID!): [Actor]}

12

Page 13: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Result

type Query {movieById(id: ID!): MovieactorById(id: ID!): ActoractorsByMovieId(id: ID!): [Actor]

}

13

Page 14: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

What is schema federation?The same… but different!

14

● Build declarative graph

● Code can be split

● Graph are easy to consume

Page 15: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Back to our example

type Movie {id: ID!title: Stringoverview: String

}type Query {

movieById(id: ID!): Movie}

15

Page 16: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

With its extension

extend type Movie { actors: [Actor]}type Actor { id: ID! name: String}

16

Page 17: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Federation with Java and OSGi

17

Page 18: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Why?

Because of dynamic federation:

OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime.

18

Page 19: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

GraphQL schema evolution rulesNever remove a field

Never change a field’s arguments

Add new fields if new arguments or semantics of a field must change

No need to version GraphQL schemas, unless major rewrites happen

Don’t split schemas, prefer aggregation layers instead

19

Page 20: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Let get cracking!3 modules retrieving movie data from TMDB and IMDB:

20

● Team A provides a new entry tmdb in query and add a new type Movie

● Team B needs to extend the Movie type with an Actor type to get the actors

list of a movie

● Team C needs to enrich the movie data with information coming from

another API

Page 21: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Common partRegistering the extension

21

Page 22: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Common part Declaring the extension

22

Page 23: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Team A

23

Page 24: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Team A

24

Page 25: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Team A

25

Page 26: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Team B

26

Page 27: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Team B

27

Page 28: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Team B

28

Page 29: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Team C

29

Page 30: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Team C

30

Page 31: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Summary

● Extend an existing schema

● Not overload the query type

● Enrich an existing scheme with data from another API

● Split the code

31

With schema federation and OSGi, we are able to:

Page 32: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Resourceshttps://www.jahia.com/jcontent

https://github.com/Jahia/graphql-core

https://github.com/DameniMilo/graphql-tmdb-provider

https://github.com/DameniMilo/graphql-tmdb-extension

https://github.com/DameniMilo/graphql-imdb-extension

32

Page 33: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

Useful linkshttps://en.wikipedia.org/wiki/OSGi

https://karaf.apache.org/

https://unomi.apache.org/

https://blog.apollographql.com/apollo-federation-f260cf525d21

https://www.apollographql.com/docs/graphql-tools/schema-stitching/

33

Page 34: Build federated GraphQL schemas in Java using OSGi and Jahia · OSGi modules with support for GraphQL federation make it possible to evolve a GraphQL schema at runtime. 18. GraphQL

THANK YOU!