GraphQL Client for Crystal

A GraphQL client shard for the Crystal language.

Usage

Installing

Just add this to your shards.yml file:

dependencies:

  crystal-gql:
    github: itsezc/crystal-gql

Then run:

shards install

Initializing

require "crystal-gql"

# Define the client
api = GraphQLClient.new "https://countries.trevorblades.com"

Querying

# useQuery
data, error, loading = api.useQuery(GQL {
	"continents" => [
		"code",
		"name",
		{
			"countries" => [
				"name",
				"capital",
				{
					"languages" => [
						"name"
					]
				}
			]
		}
	]
})

# or traditional queries
data, error, loading = api.query("{
    continents {
        code
        name
    	countries {
      		name
      		capital
      		languages {
        			name
      		}
    	}
    }
}")

# Print data
print data

Querying

With authentication:

api.add_header("Authorization", "Bearer: TOKEN")
# useQuery
data, error, loading = api.useQuery(GQL {
	"continents" => [
		"code",
		"name",
		{
			"countries" => [
				"name",
				"capital",
				{
					"languages" => [
						"name"
					]
				}
			]
		}
	]
})