• Stars
    star
    197
  • Rank 197,722 (Top 4 %)
  • Language
    Clojure
  • License
    Eclipse Public Li...
  • Created over 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Clojure(Script) graphql query generation

venia

Clojars Project

Build Status

A Clojure(Script) qraphql query client library. Generate valid graphql queries with Clojure data structures.

Usage

Venia is originally supposed to be used in Clojurescript apps, but can be used as well in Clojure, as the core is written in CLJC. The sole purpose of this library is graphql query string generation from Clojure data, so that strings concatenations and manipulations could be avoided when using grapqhl. It is up to developers to hook it up to frontend apps. However, at least some sort of re-frame-graphql-fx library is on a roadmap.

Simple query

The easiest way to start with venia, is simple's query generation.

(ns my.project
  (:require [venia.core :as v]))

(v/graphql-query {:venia/queries [[:employee {:id 1 :active true} [:name :address [:friends [:name :email]]]]]})

=> "{employee(id:1,active:true){name,address,friends{name,email}}}"

Obviously, If we would like to fetch employees and projects within the same simple query, we would do it this way:

(v/graphql-query {:venia/queries [[:employee {:id 1 :active true} [:name :address [:friends [:name :email]]]]
                                  [:projects {:active true} [:customer :price]]]})

=> "{employee(active:true){name,address},project(active:true){customer,price}}"

Field arguments

In the example above, :employee and :projects fields have arguments {:id 1 :active true} and {:id 1 :active true} respectively.

We can add arguments to other fields easily by wrapping field name and its arguments to vector [:customer {:id 2}]:

(v/graphql-query {:venia/queries [[:projects {:active true} [[:customer {:id 2}] :price]]]})

=> "{project(active:true){customer(id:2),price}}"

Query with alias

Now, if we need to have an alias for query, it can be easily achieved by using venia's query-with-data map

(v/graphql-query {:venia/queries [{:query/data [:employee {:id 1 :active true} [:name :address [:friends [:name :email]]]]
                                   :query/alias :workhorse}
                                  {:query/data  [:employee {:id 2 :active true} [:name :address [:friends [:name :email]]]]
                                   :query/alias :boss}]})
     
=> prettified:
{
  workhorse: employee(id: 1, active: true) {
    name
    address
  },
  boss: employee(id: 2, active: true) {
    name
    address
  }
}

In the query above, we use :query/data key for query definition and :query/alias for query's alias definition.

Query with fragments

What about fragments? Just add :venia/fragments vector with fragments definitions

(v/graphql-query {:venia/queries   [{:query/data  [:employee {:id 1 :active true} :fragment/comparisonFields]
                                     :query/alias :workhorse}
                                    {:query/data  [:employee {:id 2 :active true} :fragment/comparisonFields]
                                     :query/alias :boss}]
                  :venia/fragments [{:fragment/name   "comparisonFields"
                                     :fragment/type   :Worker
                                     :fragment/fields [:name :address]}]})

=> prettified:
{
  workhorse: employee(id: 1, active: true) {
    ...comparisonFields
  }
  boss: employee(id: 2, active: true) {
    ...comparisonFields
  }
}

fragment comparisonFields on Worker {
  name
  address
}

Query with variables

Now you can generate really complex queries with variables as well. In order to define variables, we need to define an operation type and name.

(v/graphql-query {:venia/operation {:operation/type :query
                                    :operation/name "employeeQuery"}
                  :venia/variables [{:variable/name    "id"
                                     :variable/type    :Int
                                     :variable/default 1}
                                    {:variable/name "name"
                                     :variable/type :String}]
                  :venia/queries   [{:query/data  [:employee {:id     :$id
                                                              :active true
                                                              :name   :$name}
                                                   :fragment/comparisonFields]
                                     :query/alias :workhorse}
                                    {:query/data  [:employee {:id     :$id
                                                              :active false}
                                                   :fragment/comparisonFields]
                                     :query/alias :boss}]
                  :venia/fragments [{:fragment/name   "comparisonFields"
                                     :fragment/type   :Worker
                                     :fragment/fields [:name :address [:friends [:name :email]]]}]})

=> prettified:
query employeeQuery($id: Int = 1, $name: String) {
  workhorse: employee(id: $id, active: true, name: $name) {
    ...comparisonFields
  }
  boss: employee(id: $id, active: false) {
    ...comparisonFields
  }
}

fragment comparisonFields on Worker {
  name
  address
  friends {
    name
    email
  }
}

Mutation

Mutations are also supported, just use :mutation operation type:

(v/graphql-query {:venia/operation {:operation/type :mutation
                                    :operation/name "AddProjectToEmployee"}
                  :venia/variables [{:variable/name "id"
                                     :variable/type :Int!}
                                    {:variable/name "project"
                                     :variable/type :ProjectNameInput!}]
                  :venia/queries   [[:addProject {:employeeId :$id
                                                  :project    :$project}
                                     [:allocation :name]]]})
                                     
=> prettified:
mutation AddProjectToEmployee($id:Int!,$project:ProjectNameInput!) {
  addProject(employeeId:$id, project:$project) {
    allocation,
    name
  }
}

Validation

Venia will verify that you don't use undefined variables or fragments.

For example, the following v/graphql-query calls will throw exceptions:

(v/graphql-query {:venia/queries [[:employee {:id 1 :active true} :fragment/undefined]]}

(v/graphql-query {:venia/queries [[:employee {:id 1 :active :$undefined} [:name]]]}))

because fragment and variable are never defined.

Meta fields

You can use graphql's __typename meta field anywhere inside of your query. For example:

(v/graphql-query {:venia/queries [[:employee [:meta/typename :name :address]]}

=> prettified:

{
  employee {
    __typename,
    name,
    address
  }
}

License

Copyright ยฉ 2017 Vincit

Distributed under the Eclipse Public License, the same as Clojure.

More Repositories

1

objection.js

An SQL-friendly ORM for Node.js
JavaScript
7,249
star
2

tarn.js

Simple and robust resource pool for node.js
JavaScript
472
star
3

objection-graphql

GraphQL schema generator for objection.js
JavaScript
307
star
4

knex-db-manager

Utility for create, drop, truncate etc. administrative database operations.
JavaScript
142
star
5

objection-find

Build search queries for objection.js models using HTTP query parameters.
JavaScript
102
star
6

db-errors

Unified node.js error API for mysql, postgres and sqlite3
JavaScript
85
star
7

objection-db-errors

A plugin that provides better database error handling for objection.js
JavaScript
71
star
8

wordpress-theme-base

Theme base for WordPress, ES2017+ & PHP7+
PHP
63
star
9

objection-rest

REST API generator for objection.js models
JavaScript
30
star
10

satakieli

Satakieli is a i18n library that provides identical API for ClojureScript and Clojure programmers. Localized messages can be written using ICU MessageFormat syntax.
Clojure
26
star
11

wordpress

Addons for WordPress development, using Seravo/wordpress as base
JavaScript
24
star
12

logspout-gelf

Logspout with GELF adapter
Dockerfile
15
star
13

gocd-slack-task

Go CD plugin allowing to send messages to Slack
Java
13
star
14

travis-oracledb-xe

Script and guide to setup Oracle Database Express Edition on Travis CI (without need to create Oracle account)
Shell
13
star
15

summer-2018

JavaScript
6
star
16

spring-boot-word-to-html-example

Java
6
star
17

alkobot

TypeScript
6
star
18

dodo.js

JavaScript
5
star
19

teatime-2018

Teatime 2018 demo project. See https://vincitteatime.fi/
JavaScript
5
star
20

heroku-buildpack-java-nodejs

Heroku buildpack for Java applications with Node.JS and npm.
Shell
4
star
21

multi-user-test-runner

JUnit test runner for running authorization unit/integration tests with multiple users and roles
Java
4
star
22

slurp

JavaScript
3
star
23

proxios

Magical ES6 Proxy wrapper for axios
3
star
24

ffmpeg

C
2
star
25

opendatatre

Open Data TRE Meetup example
JavaScript
2
star
26

wordpress-demo

Demo project, hosted in 5$ Digital Ocean LEMP one click app
PHP
2
star
27

get-a-room

PWA for making ad hoc room reservations easy in Google Workspace environment.
TypeScript
1
star
28

curd

Hassle-free CRUD operations
Clojure
1
star
29

VISDOM-Roadmapper

Roadmapper tool developed as part of ITEA 3 VISDOM project.
TypeScript
1
star
30

pfsense-fauxer

Simple client lib for communicating with pfSense faux-api qith node.
JavaScript
1
star
31

dodo-core-features

Dodo.js framework's core features (cors, auth, gzip, cookies, logging, etc.)
JavaScript
1
star
32

mml-mapserver

Docker image and instructions to serve MML maastokartta
HTML
1
star
33

multi-user-test-runner-examples

Test project for testing multi-user-test-runner
Java
1
star
34

dodo-objection

Objection.js ORM plugin for Dodo.js framework
JavaScript
1
star
35

fastest-tester

Tester for the Fastest test framework
JavaScript
1
star
36

lunch-assistant

Hackfest 2023 Kuopio team lunch assistant
TypeScript
1
star