• Document Up to Date

Custom GraphQL Schema

CrafterCMS provides a simple way to customize the built-in GraphQL schema. This feature can be used for integrating external services or transforming values to match special requirements. Once the schema has been customized it is possible to create apps or sites that interact only using GraphQL for getting both authored content & external services.

Note

This guide assumes you are familiar with GraphQL concepts like types, fields, resolvers & fetchers, you can find more information in the GraphQL documentation

After Crafter Engine builds the types corresponding to the Content Types in the site repository it will look for a Groovy script that allows you to make customizations to the schema before making it available to the clients. By default the full path of the script is /scripts/graphql/init.groovy.

In this script you will be able to use most of the global variables described in Groovy API (except the ones for the request scope). Additionally there is a global variable specific for this script:

Name

Description

Type

schema

Holds custom types, fields, fetchers & resolvers that
will be added to the GraphQL schema

Note

All customizations to the GraphQL schema need to be done programmatically, you can find more details & examples in the GraphQL Java documentation

Example

The following example shows how to customize the schema to integrate a service written in Groovy.

Note

The example uses the public OMDb API that requires a key, to make the code work in your local environment you can get a free key here

  1. Update the site configuration to include the needed information to connect to the OMDb API:

    /config/engine/site-config.xml
    1<site>
    2  <omdb>
    3    <baseUrl>http://www.omdbapi.com</baseUrl>
    4    <apiKey>XXXXXXX</apiKey>
    5  </omdb>
    6</site>
    
  2. Update the site context to include a new service bean:

    /config/engine/application-context.xml
     1  <beans xmlns="http://www.springframework.org/schema/beans"
     2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
     4         xmlns:context="http://www.springframework.org/schema/context">
     5
     6        <!-- Enable placeholders support -->
     7        <context:property-placeholder/>
     8
     9        <!-- Define the service bean -->
    10        <bean id="omdbService" init-method="init"
    11              class="org.craftercms.movies.omdb.OmdbService">
    12          <property name="baseUrl" value="${omdb.baseUrl}"/>
    13          <property name="apiKey" value="${omdb.apiKey}"/>
    14        </bean>
    15  </beans>
    
  3. Add the Groovy class for the service:

    /scripts/classes/org/craftercms/movies/omdb/OmdbService.groovy
     1package org.craftercms.movies.omdb
     2
     3// include a third-party library for easily calling the API
     4@Grab(value='io.github.http-builder-ng:http-builder-ng-core:1.0.4', initClass=false)
     5import groovyx.net.http.HttpBuilder
     6
     7class OmdbService {
     8
     9  // the base URL for all API calls
    10  String baseUrl
    11
    12  // the API key needed for the calls
    13  String apiKey
    14
    15  // The http client
    16  HttpBuilder http
    17
    18  // creates an instance of the http client with the configured base URL
    19  def init() {
    20    http = HttpBuilder.configure {
    21      request.uri = baseUrl
    22    }
    23  }
    24
    25  // performs a search call, returns the entries as maps
    26  def search(String title) {
    27    return [
    28      http.get() {
    29        // include the needed parameters
    30        request.uri.query  = [ apiKey: apiKey, t: title ]
    31      }
    32    ].flatten() // return a list even if the API only returns a single entry
    33  }
    34
    35}
    

    Note

    Notice that the service is not performing any mapping or transformation to the values returned by the API. It will only parse the response from JSON into Groovy map instances. This means that the GraphQL schema needs to match the field names returned by the API.

  4. Define the GraphQL schema to use:

    First you need to know what the API will return to create a matching schema, in any browser or REST client execute a call to http://www.omdbapi.com/?t=XXXX&apikey=XXXXXXX. The result will look like this:

    OMDb API response for movies
     1{
     2  "Title": "Hackers",
     3  "Year": "1995",
     4  "Rated": "PG-13",
     5  "Released": "15 Sep 1995",
     6  "Runtime": "107 min",
     7  "Genre": "Comedy, Crime, Drama, Thriller",
     8  "Director": "Iain Softley",
     9  "Writer": "Rafael Moreu",
    10  "Actors": "Jonny Lee Miller, Angelina Jolie, Jesse Bradford, Matthew Lillard",
    11  "Plot": "Hackers are blamed for making a virus that will capsize five oil tankers.",
    12  "Language": "English, Italian, Japanese, Russian",
    13  "Country": "USA",
    14  "Awards": "N/A",
    15  "Poster": "https://m.media-amazon.com/images/M/MV5BNmExMTkyYjItZTg0YS00NWYzLTkwMjItZWJiOWQ2M2ZkYjE4XkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_SX300.jpg",
    16  "Ratings": [
    17    {
    18      "Source": "Internet Movie Database",
    19      "Value": "6.2/10"
    20    },
    21    {
    22      "Source": "Rotten Tomatoes",
    23      "Value": "33%"
    24    },
    25    {
    26      "Source": "Metacritic",
    27      "Value": "46/100"
    28    }
    29  ],
    30  "Metascore": "46",
    31  "imdbRating": "6.2",
    32  "imdbVotes": "62,125",
    33  "imdbID": "tt0113243",
    34  "Type": "movie",
    35  "DVD": "24 Apr 2001",
    36  "BoxOffice": "N/A",
    37  "Production": "MGM",
    38  "Website": "N/A",
    39  "Response": "True"
    40}
    
    OMDb API response for series
     1{
     2  "Title": "Friends",
     3  "Year": "1994–2004",
     4  "Rated": "TV-14",
     5  "Released": "22 Sep 1994",
     6  "Runtime": "22 min",
     7  "Genre": "Comedy, Romance",
     8  "Director": "N/A",
     9  "Writer": "David Crane, Marta Kauffman",
    10  "Actors": "Jennifer Aniston, Courteney Cox, Lisa Kudrow, Matt LeBlanc",
    11  "Plot": "Follows the personal and professional lives of six twenty to thirty-something-year-old friends living in Manhattan.",
    12  "Language": "English, Dutch, Italian, French",
    13  "Country": "USA",
    14  "Awards": "Won 1 Golden Globe. Another 68 wins & 211 nominations.",
    15  "Poster": "https://m.media-amazon.com/images/M/MV5BNDVkYjU0MzctMWRmZi00NTkxLTgwZWEtOWVhYjZlYjllYmU4XkEyXkFqcGdeQXVyNTA4NzY1MzY@._V1_SX300.jpg",
    16  "Ratings": [
    17    {
    18      "Source": "Internet Movie Database",
    19      "Value": "8.9/10"
    20    }
    21  ],
    22  "Metascore": "N/A",
    23  "imdbRating": "8.9",
    24  "imdbVotes": "696,324",
    25  "imdbID": "tt0108778",
    26  "Type": "series",
    27  "totalSeasons": "10",
    28  "Response": "True"
    29}
    

    The API also has support for single episodes but those will not be included in this example. Not all fields returned by the API might be needed in the GraphQL schema, for this example we will include a small subset.

    1. The first step is to define a generic entry type that includes all common fields present in movies and series:

      GraphQL interface for all entries
      1interface OmdbEntry {
      2  Title: String!
      3  Genre: String!
      4  Plot: String!
      5  Actors: [String!]
      6}
      

      Notice that the API returns a single string for the Actors fields but in the GraphQL schema it will be defined as a list of strings, a custom data fetcher will handle this transformation.

    2. Next step is to define the concrete types for movies and series, those will have all fields from the parent type but include new ones:

      GraphQL type for movies
      1type OmdbMovie implements OmdbEntry {
      2  Title: String!
      3  Genre: String!
      4  Plot: String!
      5  Actors: [String!]
      6
      7  Production: String!
      8}
      
      GraphQL type for series
      1type OmdbSeries implements OmdbEntry {
      2  Title: String!
      3  Genre: String!
      4  Plot: String!
      5  Actors: [String!]
      6
      7  totalSeasons: Int!
      8}
      
    3. Finally the service call will be exposed using a wrapper type:

      GraphQL type for the service
      1type OmdbService {
      2
      3  search(title: String): [OmdbEntry!]
      4
      5}
      
  5. Add the GraphQL schema customizations to create the schema defined in the previous step:

    /script/graphql/init.groovy
      1package graphql
      2
      3import static graphql.Scalars.GraphQLInt
      4import static graphql.Scalars.GraphQLString
      5import static graphql.schema.GraphQLArgument.newArgument
      6import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
      7import static graphql.schema.GraphQLInterfaceType.newInterface
      8import static graphql.schema.GraphQLList.list
      9import static graphql.schema.GraphQLNonNull.nonNull
     10import static graphql.schema.GraphQLObjectType.newObject
     11
     12// Define the fields common to all types
     13def entryFields = [
     14  newFieldDefinition()
     15    .name('Title')
     16    .description('The title of the entry')
     17    .type(nonNull(GraphQLString))
     18    .build(),
     19  newFieldDefinition()
     20    .name('Genre')
     21    .description('The genre of the entry')
     22    .type(nonNull(GraphQLString))
     23    .build(),
     24  newFieldDefinition()
     25    .name('Plot')
     26    .description('The plot of the entry')
     27    .type(nonNull(GraphQLString))
     28    .build(),
     29  newFieldDefinition()
     30    .name('Actors')
     31    .description('The main cast of the entry')
     32    .type(list(nonNull(GraphQLString)))
     33    .build()
     34]
     35
     36// Define the parent type
     37def entryType = newInterface()
     38  .name('OmdbEntry')
     39  .description('The generic entry returned by the API')
     40  .fields(entryFields)
     41  .build()
     42
     43// Define the type for movies
     44def movieType = newObject()
     45  .name('OmdbMovie')
     46  .description('The entry returned for movies by the API')
     47  // Use the parent type
     48  .withInterface(entryType)
     49  // GraphQL required to repeat all fields from the interface
     50  .fields(entryFields)
     51  .field(newFieldDefinition()
     52    .name('Production')
     53    .description('The studio of the entry')
     54    .type(nonNull(GraphQLString))
     55  )
     56  .build()
     57
     58def seriesType = newObject()
     59  .name('OmdbSeries')
     60  .description('The entry returned for series by the API')
     61  // Use the parent type
     62  .withInterface(entryType)
     63  // GraphQL required to repeat all fields from the interface
     64  .fields(entryFields)
     65  .field(newFieldDefinition()
     66    .name('totalSeasons')
     67    .description('The number of seasons of the entry')
     68    .type(nonNull(GraphQLInt))
     69  )
     70  .build()
     71
     72// Add the resolver for the new types
     73schema.resolver('OmdbEntry', { env ->
     74  // The API returns the type as a field
     75  switch(env.object.Type) {
     76    case 'movie':
     77      return movieType
     78    case 'series':
     79      return seriesType
     80  }
     81})
     82
     83// Add the child types to the schema
     84// (this is needed because they are not used directly in any field)
     85schema.additionalTypes(movieType, seriesType)
     86
     87// Add the new fields to the top level type
     88schema.field(newFieldDefinition()
     89  .name('omdb') // this field is used to wrap the service calls
     90  .description('All operations related to the OMDb API')
     91  .type(newObject() // inline type definition
     92    .name('OmdbService')
     93    .description('Exposes the OMDb Service')
     94    .field(newFieldDefinition()
     95      .name('search')
     96      .description('Performs a search by title')
     97      // uses the parent type, the resolver will define the concrete type
     98      .type(list(nonNull(entryType)))
     99      .argument(newArgument()
    100        .name('title')
    101        .description("The title to search")
    102        .type(GraphQLString)
    103      )
    104    )
    105  )
    106)
    107
    108// Add the fetcher for the search field,
    109schema.fetcher('OmdbService', 'search', { env ->
    110  // calls the Groovy bean passing the needed parameters
    111  applicationContext.omdbService.search(env.getArgument('title'))
    112})
    113
    114// Define a fetcher to split the value returned by the API for the Actors
    115def actorsFetcher = { env -> env.source.Actors?.split(',')*.trim() }
    116
    117// Add the fetcher to the concrete types
    118schema.fetcher('OmdbMovie', 'Actors', actorsFetcher)
    119schema.fetcher('OmdbSeries', 'Actors', actorsFetcher)
    
  6. Verify how the GraphQL schema has changed:

    The new field odmb.search is now available and can be called with different parameters, you can requests different fields depending on the type of each result.

    For movies the Production field is returned:

    GraphQL OMDb query returning movies

    For series the totalSeasons is returned:

    GraphQL OMDb query returning series

This is a very simple example that shows the basic features to integrate a service in the schema, but it is possible to use any GraphQL feature such as mutations to wrap a full REST API or database.