GraphQL in Apache Sling - but isn't it the opposite of REST?
Oct. 1, 2020•0 likes•665 views
Download to read offline
Report
Internet
Slides of my talk at ApacheCon @Home 2020 - code at https://github.com/apache/sling-org-apache-sling-graphql-core/ (look for the "sample website") - video recording at https://youtu.be/KTMObGt0YKU
GraphQL in Apache Sling - but isn't it the opposite of REST?
1. GraphQL in Apache Sling...
but isn't it the opposite of REST?
Bertrand Delacrétaz :: Principal Scientist, Adobe
Board Member, Apache :: @bdelacretaz
Images are from stock.adobe.com unless otherwise specified - slides revision: 2020-09-28
5. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020 5
Query with the
same "shape" as
the output JSON.
Also: Mutations and
Subscriptions
POST to server returns
JSON output with just
what you need.
6. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020
GraphQL Schema
6
In our case, the schema is
generated from an
internal Sling request to
<resource>.GQLschema
So it can be specific to a
given sling:resourceType
7. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020
GraphQL Schema Introspection
7
The schema information
helps us build this query:
9. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020
Sling-samples GraphQL website demo
9
Both server-side and client-side
rendering use GraphQL and
Handlebars.
GraphiQL and similar clients
supported out-of-the-box.
Clean JSON rendering
available via GraphQL.
Server-side (GraphQL + hbs)
Client-side
(GraphQL + hbs)
sling-samples/
org.apache.sling.graphql.samples.website
Client-side
queries
10. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020
Client-side query + rendering
10
GraphQL
Servlet
Content
Repository
Client Sling Server
POST
GraphQL query
JSON results
-> Handlebars
11. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020
Client Sling Server
11
GraphQL
Script Engine
Content
Repository
Sling Scripts
Resolver
article.hbs
script
Handlebars
Script Engine
Internal Request
<path>.GQLSchema
json.GQL
script
Server-side query + rendering The Handlebars
engine renders
the .json output of
the GraphQL query
GET
article.html
Internal Request
<path>.json
HTML
13. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020
Sling GraphQL Core 0.0.4
13
API
Script Engine
Servlet
Internally uses
https://github.com/graphql-java/graphql-java
package
o.a.s.graphql.core.api
14. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020
Default SchemaProvider
14
$ curl http://localhost:8080/content/articles/culture.GQLschema
type Query {
section: Section @fetcher(name:"website/currentResource")
navigation: Navigation @fetcher(name:"website/navigation")
}
type Navigation {
root: String
sections: [Section]
previous: String
next: String
search: String
}
...
Full power of the Sling rendering
pipeline for resource-specific
schemas.
The @fetcher directive points to
SlingDataFetcher services.
15. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020
SlingDataFetcher
15
Java:
SlingDataFetcher OSGi
service, returns a
Map or POJO
Server-side script:
selected by fetcher name,
returns a Map
16. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020 16
Sling GraphQL Core
Request Handling
Schema Acquisition
graphql-java
parsing + delegation
www.graphql-java.com
Content
Repository
Other
Data Sources
Under the hood: performance?
SlingDataFetcher Services
Data Acquisition + Processing
Caching?
Client Sling Server
Performance
Happens Here ->
Schema Generation
(Sling Request Processing)
18. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020
HTTP-friendly caching?
18
Queries
Store
GraphQL
Servlet
Default POST-based
interaction is generally
not cacheable.
POST query text
201 Created -> /p/bc6f
GET /p/bc6f
200 OK -> query results
+ Cache-Control headers
Stored
Query
Text
Client Sling Server
future requests
can start here:
20. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020
GraphQL and REST (+ RPC?)
20
Quoting Phil Sturgeon, builder of API design tools,
https://phil.tech/2017/graphql-vs-rest-overview/
We do need queries, but also
caching, hypertext, scalability.
GraphQL is certainly better than
a badly designed so-called REST
API...and it's a great query API.
22. GraphQL in Apache Sling - Bertrand Delacrétaz September 2020
CODA
GraphQL is a very nice query language
that also looks
useful on the server-side.
It's not better or worse than REST:
different animals that can
play well together!
I'm @bdelacretaz - thank you!
Code at https://github.com/apache/sling-org-apache-sling-graphql-core/
Fun text by https://github.com/apache/sling-whiteboard/tree/master/fake-content-generator
22
The Sling GraphQL Core is V0.0.4:
patches welcome!
Internal Sling requests FTW!