Documentation
Other Integrations
Code-First Frameworks

Code-First Approach and Hive CLI

If you’re using the Code-First approach to define your GraphQL schema, you may have noticed that you need to retrieve the schema SDL (in a .graphql file) before using it with the Hive CLI.

We’ve collected popular Code-First libraries and frameworks and created a quick guide for retrieving the GraphQL SDL before using it with the Hive CLI.

Pothos

Pothos is a plugin based GraphQL schema builder for TypeScript. It comes with a prebuild utility to get the schema object, which you can later print to a file.

To use Hive CLI with Pothos, you can follow the Printing Schema guide in Pothos website.

TypeGraphQL

TypeGraphQL is a GraphQL schema builder that uses TypeScript classes and decorators to define the schema.

To use Hive CLI with TypeGraphQL, you can follow the Emitting the schema SDL guide in TypeGraphQL website.

GraphQL Nexus

GraphQL Nexus is composable type definition for GraphQL in TypeScript/JavaScript.

Nexus emits .graphql file by default, as part of calling makeSchema function. You can find more details in the Nexus documentation. Also refer to the Generated Artifacts guide for more information.

gqtx

gqtx is a thin layer on top of graphql-js for writing a type-safe GraphQL server in TypeScript.

To use Hive CLI with gqtx, you’ll need to setup a script that calls buildGraphQLSchema and exports GraphQL schemas to a file:

import { writeFileSync } from 'fs'
import { buildGraphQLSchema, createTypesFactory } from 'gqtx'
import { lexicographicSortSchema, printSchema } from 'graphql'
 
const t = createTypesFactory<{}>()
const Query = t.queryType({
  fields: [
    t.field({
      name: 'foo',
      type: t.String,
      resolve: (_, args, ctx) => 'test'
    })
  ]
})
 
const schema = buildGraphQLSchema({ query: Query })
const schemaAsString = printSchema(lexicographicSortSchema(schema))
 
writeFileSync('schema.graphql', schemaAsString)

GraphQL-Ruby

GraphQL-Ruby is a Ruby library for building GraphQL servers and schemas. To export GraphQL SDL from your Ruby code, you can use the built-in Printer class:

class MySchema < GraphQL::Schema
  query Types::Query
  # ...
end
 
printer = GraphQL::Schema::Printer.new(MySchema)
printer.print_schema

You can also refer to the runtime integration with GraphQL-Ruby and Hive.

GraphQL-Crystal

GraphQL-Crystal is a GraphQL server library and schema builder for Crystal.

You can follow the Getting Started guide to learn more about the usage of schema.document.to_s.

Juniper

Juniper is a Rust library that makes it possible to write GraphQL servers in Rust that are type-safe.

The schema object of Juniper can be printted using the as_schema_language function:

struct Query;

#[graphql_object]
impl Query {
    fn hello(&self) -> FieldResult<&str> {
        Ok("hello world")
    }
}

let schema = RootNode::new(Query, EmptyMutation::<()>::new(), EmptySubscription::<()>::new());
let sdl = schema.as_schema_language();
let mut file = File::create("schema.graphql")?;

file.write_all(sdl)?

async-graphql

async-graphql is a Rust library from creating type-safe GraphQL APIs using Rust language.

The schema object of async-graphql can be printted using the sdl function (see SDL Export for more information):

# extern crate async_graphql;
use async_graphql::*;
 
struct Query;
 
#[Object]
impl Query {
    async fn add(&self, u: i32, v: i32) -> i32 {
        u + v
    }
}
 
let schema = Schema::build(Query, EmptyMutation, EmptySubscription).finish();
let sdl = schema.sdl();
let mut file = File::create("schema.graphql")?;
file.write_all(sdl)?

Graphene

Graphene is a library that provides tools to implement a GraphQL API in Python using a code-first approach.

The schema object of Graphene can be printted using the str function, and then saved to a .graphql file:

from your_project.schema_definition import schema
 
with open('schema.graphql', 'w') as f:
    f.write(str(schema))

Strawberry

Strawberry is a Python library for building GraphQL APIs using a Python 3 and code-first approach.

Strawberry provides a command-line interface to export the schema to a GraphQL file, for example, assuming we have the following schema.py file:

import strawberry
 
@strawberry.type
class Query:
    hello: str = strawberry.field(resolver=lambda: "hello world")
 
schema = strawberry.Schema(Query)

we can use the following command to export the schema to a .graphql file

strawberry export-schema schema > schema.graphql