Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/green-labs/tools.graphql

Clojure tools for GraphQL
https://github.com/green-labs/tools.graphql

clojure graphql lacinia

Last synced: about 1 month ago
JSON representation

Clojure tools for GraphQL

Awesome Lists containing this project

README

        

# tools.graphql

[![Clojars Project](https://img.shields.io/clojars/v/org.clojars.greenlabs/tools.graphql.svg)](https://clojars.org/org.clojars.greenlabs/tools.graphql)

This library provides various tools related to GraphQL.

1. [Convert EDN Schema to SDL](#convert-edn-schema-to-sdl)
2. [Merge EDN Schemas](#merge-edn-schemas)
3. [Schema Static Analysis](#schema-static-analysis)

## Installation

You can install this library using the Clojure CLI tool.

```sh
clojure -Ttools install-latest :lib io.github.green-labs/tools.graphql :as graphql
```

### Convert EDN Schema to SDL

When using the Lacinia library, you need to write the schema in EDN format. If you want to convert it to GraphQL SDL format, you can use the `edn2sdl` API.

```sh
clojure -Tgraphql edn2sdl \
:input-path '"path/to/schema.edn"' \
:output-path '"path/to/schema.graphql"'
```

### Merge EDN Schemas

As your service grows, it may be convenient to write the EDN schema in multiple files. However, Lacinia can only read a single schema.

Although it is possible to merge and load these at runtime, this approach is not recommended. It can be difficult to ensure that there are no conflicts between the schemas or that all necessary fields are defined.

Therefore, you might want to have a separate build step to merge the schemas. You can use the `stitch-schemas` API for this purpose.

```sh
clojure -Tgraphql stitch-schemas \
:input-paths '["path/to/schema1/" "path/to/schema2/"]' \
:output-path '"path/to/schema.edn"'
```

The `stitch-schemas` function will only generate the final schema if it can merge the schemas reliably.

If you frequently need to merge schemas during development, you can add the `:watch true` parameter to enable the watch feature.

Note: It is recommended to version control the stitched schema alongside with subschemas.

### Schema Static Analysis

To maintain consistency and avoid using incorrect fields or types when writing your schema, you might need a tool to assist you, especially as the schema grows larger and more complex. For this, you can use the `validate` API.

```sh
clojure -Tgraphql validate :input-path '"path/to/schema.edn"'
```

This command will identify unused `type`, `input`, `enum`, `union`, and `interface`.

Example:

```text
Unreachable type UrlConnection bases/core-api/resources/schema/common.edn:62:30
Unreachable type UserAddressConnection
Unreachable type YoutubeConnection
Unreachable input GroupMembershipUpdateInput bases/core-api/resources/schema/farming_group.edn:238:3
Unreachable input ProductionCategoryCodeInput bases/core-api/resources/schema/pesticide.edn:141:47
Unreachable interface Node bases/core-api/resources/schema/common.edn:6:1
```

To output the location in the subschema, you must set `:source-map?` option to true when stitching.