Documentation
Integrations and Guides
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 (opens in a new tab) 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 (opens in a new tab).

TypeGraphQL

TypeGraphQL (opens in a new tab) 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 (opens in a new tab).

GraphQL Nexus

GraphQL Nexus (opens in a new tab) 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 (opens in a new tab). Also refer to the Generated Artifacts guide for more information (opens in a new tab).

gqtx

gqtx (opens in a new tab) 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 (opens in a new tab):

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 (opens in a new tab) is a GraphQL server library and schema builder for Crystal.

You can follow the Getting Started guide (opens in a new tab) to learn more about the usage of schema.document.to_s.

Juniper

Juniper (opens in a new tab) 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 (opens in a new tab) 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 (opens in a new tab)):

# 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 (opens in a new tab) 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 (opens in a new tab) is a Python library for building GraphQL APIs using a Python 3 and code-first approach.

The following snippet might help you to print the schema to a file:

import strawberry
import typing
from graphql import print_schema
 
schema = strawberry.Schema(query=Query)
with open('schema.graphql', 'w+') as f:
    f.write(print_schema(schema._schema))