Need help with serene?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

133 Stars 2 Forks 33 Commits 2 Opened issues


Generate clojure.spec with GraphQL and extend GraphQL with clojure.spec

Services available


Need anything else?

Contributors list

# 629,797
2 commits

Clojars Project


Generate clojure.spec with GraphQL and extend GraphQL with clojure.spec.

  • 100% GraphQL schema spec coverage
  • Works with any GraphQL API
  • Extend GraphQL with your own specs
  • Works with Clojure & ClojureScript

Clojure/conj 2018 - Robust APIs with clojure.spec & GraphQL

Robust APIs with clojure.spec & GraphQL - Clojure/conj 2018

Table of Contents


Serene can generate specs for an entire GraphQL API in one line of code.

Let's say you have a project called "Serenity" and a GraphQL API available at

   [clojure.spec.alpha :as s]
   [paren.serene :as serene]
   [paren.serene.schema :as schema]))

;; Define specs (serene/def-specs (schema/fetch "http://localhost:3000/graphql"))

;; Use specs (s/valid? ::User {:firstName "Frank" :lastName "Costanza"}) ;=> true


It is our experience that GraphQL is superior to REST for most APIs used for web and mobile applications. We also think that clojure.spec provides a good balance of expressiveness and strictness.

GraphQL's type system provides a point of leverage for API providers and consumers. Because GraphQL schemas are introspectable, GraphQL tooling tends to be very powerful. Some languages, like OCaml/Reason, can even validate queries and response code at compile time.

If other languages can leverage GraphQL to this extent, Clojure should be able to as well. Serene aims to address this.


Generated Spec Names

Spec names are keywords that are prefixed and namespaced by their position in the schema. For the example below, let's assume a prefix of

, though the prefix is customizable.
# Built-in scalars are defined: :gql/Boolean, :gql/Float , :gql/ID , :gql/Int, :gql/String

scalar Email # :gql/Email

enum Mood { # :gql/Mood SERENE # :gql.Mood/SERENE ANNOYED # :gql.Mood/ANNOYED ANGRY # :gql.Mood/ANGRY }

type User { # :gql/User id: ID! # :gql.User/id username: String! # :gql.User/username email: Email! # :gql.User/email mood: Mood # :gql.User/mood }

type Mutation { # :gql/Mutation createUser( username: String!, # :gql.Mutation.createUser/username email: Email! # :gql.Mutation.createUser/email mood: Mood # :gql.Mutation.createUser/mood # :gql.Mutation.createUser/&args is an anonymous s/keys spec for args map ): User! # :gql.Mutation/createUser }

Generating Specs


Note: All arguments to


is a macro that will define specs for a GraphQL schema. It takes compilation options as an optional second argument. Compilation options are documented below.
(serene/def-specs gql-schema options)


is like
, but outputs
forms to a file. The file path and namespace are the first two arguments to
(serene/spit-specs "src/api/specs.cljc" 'api.specs gql-schema options)

Getting your GraphQL Schema


takes a GraphQL server endpoint and optional configuration.
(schema/fetch "" {:headers {"Authorization" (str "bearer " gh-access-token)}})


is this GraphQL introspection query string.

works by asking the HTTP GraphQL server to execute

You can use

directly if your GraphQL API is not accessible via HTTP.

Compilation Options


is a function or map of spec names to spec forms. If a spec form is returned, it will be combined with default specs using

For example, if you have a custom

scalar you could use the following to add custom scalar validation:
(serene/def-specs gql-schema {:extend {:Keyword `keyword?}})


is a function or map which receives unprefixed spec names and returns aliases for those names.
(serene/def-specs gql-schema {:alias {:Query #{:api/query :api.query/root}
                                      :Query/node :api.query/get-node}})

This would cause both

to be defined as aliases of the
type spec and would cause
to be defined as an alias of the
field spec.


is a wrapper around
for the common case of altering default

For example, instead of having a long namespace prefix, you might want to prefix your specs with

(serene/def-specs gql-schema {:prefix :gql})

This will produce specs like

, etc.


will cause test.check generators for object types to generate all fields, even though all object fields are optional.

This is necessary if you are using test.check to generate data for object, interface, or union types because all object fields are optional (clients can query for any combination of fields).

However, generating data for map specs where all keys are optional can be frustrating because you often end up with empty or nearly empty maps. It is also not possible to always generate all fields, because objects can be cyclic, so you have to stop after some predetermined level.

solves this by always generating all fields up to
is a configurable depth that defaults to
;; modify all objects to generate `s/*recursion-limit*` levels deep
(serene/def-specs gql-schema {:gen-object-fields true})

;; modify only Query to generate 5 levels deep (serene/def-specs gql-schema {:gen-object-fields {:Query 5}})

Custom Compilation Options

All compilation options are implemented and documented in


Custom compilation options can be added in the same way that default options are provided.

How It Works

Serene works in much the same way as GraphiQL and other GraphQL tools; it uses GraphQL's introspection capabilities. GraphQL schemas are introspectable, meaning that you can query a running API to determine all of the capabilities of that API.

Serene uses this introspection query, which is conveniently defined as

, to generate specs that match your API.


If clojure.spec is alpha, then Serene is extra alpha.

Consider everything to be an implementation detail unless it is explicitly documented here.

Serene uses Break Versioning.


Copyright © Paren, LLC

Distributed under the Eclipse Public License version 2.0.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.