All Projects → oliyh → Lacinia Gen

oliyh / Lacinia Gen

Generators for GraphQL

Programming Languages

clojure
4091 projects
clojurescript
191 projects

Projects that are alternatives of or similar to Lacinia Gen

Blaze Apollo
Blaze integration for the Apollo Client
Stars: ✭ 56 (-9.68%)
Mutual labels:  graphql
Nextjs Auth0 Hasura
Template project for Nextjs + Auth0 + Hasura with Apollo.
Stars: ✭ 59 (-4.84%)
Mutual labels:  graphql
Github Graphql React
A Github client built with React / GraphQL 💻👩‍💻💽 👨‍💻
Stars: ✭ 60 (-3.23%)
Mutual labels:  graphql
Graphql Api Gateway
An open-sourced example of a GraphQL API Gateway. This service queries and joins data across different back-ends into one GraphQL schema.
Stars: ✭ 57 (-8.06%)
Mutual labels:  graphql
Next React Graphql Apollo boostrap
React + GraphQL + Next.js project architecture that I play with right now
Stars: ✭ 59 (-4.84%)
Mutual labels:  graphql
Locksmith
Want to use GraphQL with Clojure/script but don't want keBab or snake_keys everywhere? Use locksmith to change all the keys!
Stars: ✭ 59 (-4.84%)
Mutual labels:  graphql
Laravel Graphql
GraphQL implementation with power of Laravel
Stars: ✭ 56 (-9.68%)
Mutual labels:  graphql
Headfon.es
a mini Spotify clone 🎧
Stars: ✭ 62 (+0%)
Mutual labels:  graphql
Graphql Apollo Next.js
A server-rendering GraphQL client with Apollo Client and Next.js
Stars: ✭ 59 (-4.84%)
Mutual labels:  graphql
Graphql Serverless
Example boilerplates for GraphQL backends hosted on serverless platforms
Stars: ✭ 60 (-3.23%)
Mutual labels:  graphql
Erxes Api
API for erxes
Stars: ✭ 57 (-8.06%)
Mutual labels:  graphql
Up
Up focuses on deploying "vanilla" HTTP servers so there's nothing new to learn, just develop with your favorite existing frameworks such as Express, Koa, Django, Golang net/http or others.
Stars: ✭ 8,439 (+13511.29%)
Mutual labels:  graphql
Iban.im
Shorten, create and share memorable links for IBANS
Stars: ✭ 60 (-3.23%)
Mutual labels:  graphql
Starter
Opinionated SaaS quick-start with pre-built user account and organization system for full-stack application development in React, Node.js, GraphQL and PostgreSQL. Powered by PostGraphile, TypeScript, Apollo Client, Graphile Worker, Graphile Migrate, GraphQL Code Generator, Ant Design and Next.js
Stars: ✭ 1,082 (+1645.16%)
Mutual labels:  graphql
Instapy Research
📄 Research repository for InstaPy
Stars: ✭ 60 (-3.23%)
Mutual labels:  graphql
Store Graphql
GraphQL schema and resolvers for the VTEX Commerce Stores API
Stars: ✭ 56 (-9.68%)
Mutual labels:  graphql
Guide To Graphql
A Frontend Developer's Guide to GraphQL (Fluent Conf 2018)
Stars: ✭ 59 (-4.84%)
Mutual labels:  graphql
Graphql To Sparql.js
Converts GraphQL queries to SPARQL queries
Stars: ✭ 62 (+0%)
Mutual labels:  graphql
Neo4j Graphql
An optimized neo4j query resolver for Facebook's GraphQL
Stars: ✭ 60 (-3.23%)
Mutual labels:  graphql
Wsjs.saic.mmewmd
中国商标网加密接口。解析网页中的<meta id="9DhefwqGPrzGxEp9hPaoag">等加密内容,生成包含FSSBBIl1UgzbN7N80T, MmEwMD, y7bRbp, c1K5tw0w6_等密文的合法HTTP请求。
Stars: ✭ 60 (-3.23%)
Mutual labels:  graphql

lacinia-gen

lacinia-gen lets you generate GraphQL responses using your lacinia schema and GraphQL queries, allowing you to make your tests more rigorous in both Clojure and Clojurescript.

Clojars Project

Usage

There are two main ways to use lacinia-gen:

  • Full graph generates the entire graph from any entry point
  • Query result generates a response for a particular query

Full graph

You can create a generator for a full graph from any root by using the generator function in lacinia-gen.core. This resolves all reachable nodes in the graph up to the desired recursion depth.

(require '[lacinia-gen.core :as lgen])
(require '[clojure.test.check.generators :as g])

(let [schema {:enums {:position {:values [:goalkeeper :defence :attack]}}
              :objects {:team {:fields {:wins {:type 'Int}
                                        :losses {:type 'Int}
                                        :players {:type '(list :player)}}}
                        :player {:fields {:name {:type 'String}
                                          :age {:type 'Int}
                                          :position {:type :position}}}}}]

  (let [gen (lgen/generator schema)]

    (g/sample (gen :position) 5)
    ;; => (:defence :goalkeeper :defence :goalkeeper :goalkeeper)

    (g/sample (gen :player) 5)
    ;; => ({:name "", :age 0, :position :attack}
           {:name "", :age 0, :position :attack}
           {:name "", :age 1, :position :attack}
           {:name "–m", :age -2, :position :defence}
           {:name "¤", :age 4, :position :defence})

    (g/sample (gen :team) 5)
    ;; => ({:wins 0, :losses 0, :players ()}
           {:wins 1,
            :losses 1,
            :players ({:name "", :age -1, :position :defence})}
           {:wins 1,
            :losses 2,
            :players
            ({:name "", :age 1, :position :attack}
             {:name "q", :age -2, :position :attack})}
           {:wins -3, :losses 1, :players ()}
           {:wins 0,
            :losses -3,
            :players
            ({:name "‘ßéÅ", :age 3, :position :attack}
             {:name "", :age -4, :position :defence})})))

It supports recursive graphs, so the following works too:

(let [schema {:objects {:team {:fields {:name {:type 'String}
                                        :players {:type '(list :player)}}}
                        :player {:fields {:name {:type 'String}
                                          :team {:type :team}}}}}]

    (let [gen (lgen/generator schema)]
      (g/sample (gen :team) 5)))

;; => {:name "ö–",
       :players
       ({:name "³2",
         :team
         {:name "ºN",
          :players
          ({:name "Ïâ¦", :team {:name "¤¼J"}}
           {:name "o", :team {:name "æ‚8‹"}}
           {:name "/ç", :team {:name "ãL"}}
           {:name "éíª6", :team {:name "v‡"}})}}

If you want to limit the depth to which certain objects recurse, you can do so with the following option:

(lgen/generator schema {:depth {:team 0}})

This will ensure that :team only appears once in the graph; the team will have players, but it prevents the players from having a team. The default value for all objects is 1, meaning each will recur once (a team will have players which have a team which has players, but no further). You can set any integer value you wish.

If you want to limit the number of items in lists, you can do so with the following option:

(lgen/generator schema {:width {:player 2}})

This will ensure that lists of :player will have a maximum size of 2.

Query result

If you have a GraphQL query and wish to generate data for its result, you can use lacinia-gen.query.

(require '[lacinia-gen.query :as query])

(def schema '{:enums {:position {:values [:goalkeeper :defence :attack]}}
              :objects {:team {:fields {:wins {:type Int}
                                        :losses {:type Int}
                                        :players {:type (list :player)}}}
                        :player {:fields {:name {:type String}
                                          :age {:type Int}
                                          :position {:type :position}}}}
              :queries {:teams {:type (list :team)
                                :resolve :resolve-teams}}})

(let [f (query/generate-fn schema {})]
    (f "query { teams { wins players { name } } }" {}))

;; => {:data
       {:teams
        ({:wins 0, :players ()}
         {:wins 0, :players ({:name ""})}
         {:wins 1, :players ()}
         {:wins 0, :players ({:name "÷ "} {:name "¢"})}
         {:wins 1, :players ()})}}

Currently the queries are interpreted by Lacinia and as such require the JVM. This means generate-fn cannot be used from Clojurescript. The two macros generate-query and generate-query* may be used in Clojurescript and will evaluate to the generated result of the query.

(ns my.test
  (:require-macros [lacinia-gen.query :refer [generate-data*]]))

(def schema '{:objects {:team {:fields {:wins {:type Int}
                                        :losses {:type Int}}}}
              :queries {:teams {:type (list :team)
                                :resolve :resolve-teams}}})

(def query "query { teams { wins } }")

(def data (generate-data* schema query {} {}))

data
;; => {:data
       {:teams
        ({:wins 0}
         {:wins 0}
         {:wins 0)}
         {:wins -3}
         {:wins 0})}

Custom scalars

If your schema contains custom scalars you will need to provide generators for them. You can do so in the following way:

(let [schema {:scalars {:Custom {:parse :parse-custom
                                 :serialize :serialize-custom}}
              :objects {:obj-with-custom
                        {:fields
                         {:custom {:type :Custom}
                          :custom-list {:type '(list :Custom)}}}}}]

  (let [gen (lgen/generator schema {:scalars {:Custom gen/int}})]
    (g/sample (gen :obj-with-custom) 10)))

;; => {:custom 23
       :custom-list (-1 4 16)}

Development

CircleCI

License

Copyright © 2018 oliyh

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].