Getting started
GraphQL server library for Crystal.
- Boilerplate-free: Schema generated at compile time
- Type-safe: Crystal guarantees your code matches your schema
- High performance: See benchmarks
Getting Started
Install the shard by adding the following to our shard.yml:
Then run shards install. Crystal 1.4.0 or later is required.
The first step is to define a query object. This is the root type for all queries and it looks like this:
require "graphql"
@[GraphQL::Object]
class Query < GraphQL::BaseQuery
@[GraphQL::Field]
def hello(name : String) : String
"Hello, #{name}!"
end
end
Now we can create a schema object:
To verify we did everything correctly, we can print out the schema:
Which, among several built-in types, prints our query type:
To serve our API over HTTP we call schema.execute with the request parameters and receive a JSON string. Here is an example for Kemal:
post "/graphql" do |env|
env.response.content_type = "application/json"
query = env.params.json["query"].as(String)
variables = env.params.json["variables"]?.as(Hash(String, JSON::Any)?)
operation_name = env.params.json["operationName"]?.as(String?)
schema.execute(query, variables, operation_name)
end
Now we're ready to query our API:
curl \
-X POST \
-H "Content-Type: application/json" \
--data '{ "query": "{ hello(name: \"John Doe\") }" }' \
http://0.0.0.0:3000/graphql
This should return:
For easier development, we recommend using GraphiQL. A starter template combining Kemal, GraphiQL and a live subscription is found at examples/graphiql.
Where to go next
- Objects, queries and mutations for the types that make up a schema
- Context for passing request data to fields and limiting what a query may do
- Subscriptions for streaming responses over WebSockets
- Errors for how failures appear in a response
- Upgrading from 0.4 if you are coming from an earlier release