Clojurescript-node.js mount module for a district server, that sets up GraphQL server. It uses expressjs and express-graphql to set up the server.
Include [district.server.graphql]
in your CLJS file, where you use mount/start
Warning: district0x modules are still in early stages, therefore API can change in a future.
You can pass following args to graphql module:
:port
Port at which HTTP server will start:path
Path of GraphQL endpoint:middlewares
Collection of expressjs middlewares you want to install.
See list of district-server-middlewares.:gql-name->kw
Function for converting GraphQL names into keywords. Default: gql-name->kw:kw->gql-name
Function for converting keywords into GraphQL names. Default: kw->gql-name(ns my-district
(:require [mount.core :as mount]
[district.server.graphql]))
(def schema "type Query { hello: String}")
(def root {:hello (constantly "Hello world")})
(-> (mount/with-args
{:graphql {:port 6200
:path "/graphql"
:schema schema
:root-value root
:graphiql true}})
(mount/start))
That's all you need to do to set up GraphQL server!
district-server-graphql
gets initial args from config provided by district-server-config/config
under the key :graphql
. These args are then merged together with ones passed to mount/with-args
.
If you wish to use custom modules instead of dependencies above while still using district-server-graphql
, you can easily do so by mount's states swapping.
This namespace contains mount module as well as some helper functions
run-query [query]
Will run GraphQL query. Transforms response from JS objects into CLJS data structures. You can pass query string or graphql-query data structure.
(run-query "{hello}")
;; => {:data {:hello "Hello world"}}
(run-query {:queries [:hello]})
;; => {:data {:hello "Hello world"}}
This namespace contains function for creating GraphQL expressjs middleware
create-graphql-middleware [opts]
Creates expressjs graphql middleware. Pass same opt as you'd pass into GraphQL options. For schema you can pass either string or built GraphQL object.
build-schema [schema-str resolvers-map {:keys [kw->gql-name gql-name->kw]}]
Builds a GraphQLSchema from a schema string and a resolvers map.
(let [schema "type Author {
id: ID!
firstName: String
lastName: String
posts: [Post]
}
type Post {
id: ID!
title: String
author: Author
votes: Int
}
type Query {
posts(minVotes: Int): [Post]
}
type Mutation {
upvotePost (postId: ID!): Post
}
schema {
query: Query
mutation: Mutation
}"
resolvers {:Query {:posts (fn [obj {:keys [min-votes] :as args}])}
:Mutation {:upvote-post (fn [obj {:keys [post-id] :as args}])}
:Author {:posts (fn [{:keys [posts] :as author}])}
:Post {:author (fn [{:keys [author] :as post}])}}]
(build-schema schema resolvers {:kw->gql-name kw->gql-name
:gql-name->kw gql-name->kw}))
# To start REPL and run tests
lein deps
lein repl
(start-tests!)
# In other terminal
node tests-compiled/run-tests.js
# To run tests without REPL
lein doo node "tests" once
Can you improve this documentation? These fine people already did:
madvas, Juan Monetta & filipEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close