GraphQL IntroductionSerge Huber, Jahia CTO
#graphqlMake some noise
1 WHAT IS GRAPHQL ?I like opposites
It is not:• A query language like SQL, JCR-SQL,
etc…• A URL specification• A finalized standard• A standard submitted to a standards
body• A 100% JSON standard• A mature standard, changes are still
happening
1 WHAT IS GRAPHQL ?Some users
Courtesy of the GraphQL.org home page
See more on http://graphql.org/users
1 WHAT IS GRAPHQL ?Schemas• A schema that describes: • Objects• Object fields• Field arguments & directives• Interfaces• Unions
1 WHAT IS GRAPHQL ?Fields, the key to understanding GraphQL !A field may be:• a static value• a value resolved through
code
Field may have:• Arguments that are like
parameters to a method call• Constraints to filter data• Directives that make the field
conditional in the result
Don’t think of fields as variables, think of them as methods !
1 WHAT IS GRAPHQL ?Queries• Queries• Specify what fields are part of
the result• Only fields specified in the
schema are acceptable• Field may be the result of
computations• Fields may specify arguments
that will constrain the results (such as id:10)
• Directives may also be used (such as @include)
• Variables may be used• Fragments• Type casts !• Pagination (Relay schema)
1 WHAT IS GRAPHQL ?Mutations• Mutations• Make it possible to
write/modify data• Very similar syntax to
queries
1 WHAT IS GRAPHQL ?Compared to REST APIs
REST GraphQL
Specifies URL structure YES NO
Specifies request / response body structure NO YES
Validation NO YES
Requires a declared schema NO YES
Key-Value friendly YES NO
Extra (non CRUD) methods standardized NO YES
2 GRAPHQL INTEGRATIONTypical architecture
Client
Server
React / React Native
Relay
GraphQL
Data & Services
2 GRAPHQL INTEGRATIONAngular 2 and Relay
https://tech.small-improvements.com/2016/02/25/angular-2-and-relay/
2 GRAPHQL INTEGRATIONJava Libraries
• graphql-java : Java implementation of GraphQL
• graphql-java-servlet : A relay-compatible OSGI Java servlet
• graphql-java-annotations : Can generate GraphQL schemas from Pojo annotations !
3 GRAPHQL TOOLINGGraphiQL
Features• Auto-completion based on GraphQL
schema• Execution of GraphQL queries and
mutations• Input of variables• Result viewer• Built-in documentation browser
3 GRAPHQL TOOLINGGraphQLHubA hub to test public GraphQL services such as :• Hacker News• Reddit• GitHub• Twitter• Giphy• Relay-compatible Key-
Value store
But (strangely) not Facebook (probably because of API keys)
3 GRAPHQL TOOLINGAwesome GraphQLBest list of tools & libraries at :
https://github.com/chentsulin/awesome-graphql
Some cool examples:
SQL Libraries• GraphpostgresQL - GraphQL for Postgres.• sql-to-graphql - Generate a GraphQL API based on your SQL
database structure.• PostGraphQL - A GraphQL schema created by reflection over a
PostgreSQL schema.
4 DX INTEGRATION DEMOThe sourceProject available here:
http://www.github.com/Jahia/graphql-core-feature
• Built as a Karaf features, requires DX 7.2 to run• Embeds GraphiQL as an administration UI !• Extensible through other modules, they simply provide query and
mutation providers !• Dynamic schema, re-generated every time a new mutation or query
provider is registered in the OSGi services registry• Based on the graphql-java-servlet project, available at:
http://www.github.com/graphql/graphql-java-servlet• Still a lot to do, just beginning• Code is clearly a proof of concept prototype, lots of things will need
cleaning up and refactoring• Mapping to JCR content still to be defined.