Skip to content

alumbra/alumbra.generators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

alumbra.generators

A collection of test.check generators for GraphQL queries.

Build Status Clojars Project

There are two types of queries that can be generated: random ones that show syntactic correctness but might be utter nonsense, and those based on an analyzed schema, being both syntactically and semantically correct.

Usage

Documentation

Valid Data

Given a GraphQL schema (parsed and analyzed), a generator for valid GraphQL operations can be built:

(require '[alumbra.generators :as gen])

(def gen-operation
  (gen/operation
    (-> "type Person { name: String!, pets: [Pet!] }
         type Pet { name: String!, meows: Boolean }
         type QueryRoot { person(name: String!): Person }
         schema { query: QueryRoot }"
     (alumbra.analyzer/analyze-schema alumbra.parser/parse-schema))))

This is a function that takes the operation type and, optionally, operation name and produces a string value representing our desired operation, conforming to the above schema.

(rand-nth
  (clojure.test.check.generators/sample (gen-operation :query "Q")))
;; => "query Q {
;;       __schema {
;;         queryType { inputFields { defaultValue, description }, ... }
;;       },
;;       person(name: \"Mu90ChZ1ht\") { name }
;;     }"

As you can see, the implicitly given introspection fields will be accessed just as well.

Random Data

Query Documents

This generates a GraphQL document as described in the GraphQL specification.

(clojure.test.check.generators/sample (gen/raw-document) 1)
;; => ("mutation X($h: [T]! = 0.8e-57753886, $Q: [K]! = 0.1693) { ...")

Schema Documents

This generates a GraphQL IDL document. There is, as of the writing of this README, no complete specification on this, so it is based on the Schemas and Types guide, as well as the current state of this PR.

(clojure.test.check.generators/sample (gen/raw-schema) 1)
;; => ("schema {query: O, mutation: B}\ninterface G {D(c: [I]): [O]}\nenum F {O}")

License

MIT License

Copyright (c) 2016 Yannick Scherer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.