# 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](/docs/api-reference/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](https://pothos-graphql.dev/) 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](https://pothos-graphql.dev/docs/guide/printing-schemas#printing-schema).

## TypeGraphQL

[TypeGraphQL](https://typegraphql.com/) 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](https://typegraphql.com/docs/emit-schema.html).

## GraphQL Nexus

[GraphQL Nexus](https://nexusjs.org/) 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](https://nexusjs.org/docs/getting-started/tutorial/chapter-writing-your-first-schema#sdl).
Also refer to the
[Generated Artifacts guide for more information](https://nexusjs.org/docs/guides/generated-artifacts).

## gqtx

[`gqtx`](https://github.com/sikanhe/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:

```ts
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](https://graphql-ruby.org/api-doc/1.9.7/GraphQL/Schema/Printer.html):

```ruby
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](/docs/other-integrations/graphql-ruby).

## GraphQL-Crystal

[GraphQL-Crystal](https://github.com/graphql-crystal/graphql) is a GraphQL server library and schema
builder for Crystal.

You can follow the
[Getting Started guide](https://github.com/graphql-crystal/graphql#getting-started) to learn more
about the usage of `schema.document.to_s`.

## Juniper

[Juniper](https://github.com/graphql-rust/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:

```Rust
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`](https://github.com/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](https://github.com/async-graphql/async-graphql/blob/master/docs/en/src/sdl_export.md)):

```rust
# 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](https://graphene-python.org/) 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:

```python
from your_project.schema_definition import schema

with open('schema.graphql', 'w') as f:
    f.write(str(schema))
```

## Strawberry

[Strawberry](https://strawberry.rocks/) 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:

```python
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

```bash
strawberry export-schema schema > schema.graphql
```
