Create a Module
Themis is a CLI Layer on top of Apollo Server and GraphQL Tools, to encapsulate subsets of a Graph into modules. Modules can also be NPM packages.
Note: To run the examples you can install themis globally yarn global add themis-graphql
, or create a new package in an empty folder with yarn init --yes
, install themis locally yarn add themis-graphql
and use it in the shell examples like ./node_modules/.bin/themis --help
.
All examples can be found in the repository as well.
Creating a GraphQL Module
A module
in its most basic form consists of a GraphQL schema (Type Definition) and resolvers. If you don't know what a schema and resolvers are, checkout graphql.org/learn. For convenience, Themis exposes the packages it uses, like graphql-tag
, graphql-tools
and apollo-server
, plus other convenvient helpers for testing.
Create a file called hello.js
with the following contents:
const { gql } = require('themis-graphql');
module.exports = {
name: 'hello',
typeDefs: gql`
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: () => 'world'
}
}
}
You can point themis to a file/directory containing a module
and it will start up an apollo-graphql server:
themis hello.js
By default, Themis will start a server at port 8484
and mount the server at the path /api/graphql
. Goto localhost:8484/api/graphql
to open GraphQL Playground. You can create a query on the left:
{
hello
}
Mutations and Subscriptions work as well.
Combining Modules
Create a second file called bye.js
with the following contents:
const { gql } = require('themis-graphql');
module.exports = {
name: 'bye',
typeDefs: gql`
type Query {
bye: String
}
`,
resolvers: {
Query: {
bye: () => 'world'
}
}
}
You can point themis to multiple modules:
themis hello.js bye.js
In Playground you should now be able to query:
{
hello
bye
}
If you have a bunch of modules, you can also specify which ones to run via a config file. You can find more advanced examples with remote modules in its API docs.
No lock-in
If the need for deeper server customisation may arise, or an important feature is missing, Themis GraphQL modules can be used manually with Apollo Server and GraphQL Tools at any time. You just have to set up an Apollo Server and stitch them yourself. We'd be happy to integrate support for advanced use cases though.