# gqg

> Schema-first GraphQL code generator

Latest version **0.1.1** (published 2021-06-25) · ISC license · 0 weekly downloads

## Install

```sh
npm install gqg
pnpm add gqg
yarn add gqg
bun add gqg
```

Provides the command `gqg`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2021-06-25 |
| First published | 2021-06-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 64.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | hanpama |

## Links

- npm: https://www.npmjs.com/package/gqg
- Repository: https://github.com/hanpama/gqg
- Homepage: https://github.com/hanpama/gqg#readme
- Issues: https://github.com/hanpama/gqg/issues
- npm.io page: https://npm.io/package/gqg

## Dependencies (1)

- [graphql](https://npm.io/package/graphql.md) ^15.5.1

## Recent versions

- 0.1.1 (latest) — 2021-06-25
- 0.1.0 — 2021-06-25

## README

# GQG

Schema-first GraphQL code generator


## Usage

```sh
npm install --global gqg
```

```sh
gqg (SCHEMA_DIR) (OUTPUT_FILE)
# example: gqg schema/ src/__test__/testSchema.ts
```


## `@resolve` and `@source`

A GraphQL implementation is a composition of resolvers and sources.

```graphql
type Post implements Node {
  id: ID!

  title: String!

  publishedAt: Date!

  content: String!

  author: User!
}
```

When you define a type like `Post`, you would have a blueprint of
how its resolver and source type should be.

GQG helps you to explicitly annotate the resolver and source fields.

```graphql
type Post implements Node {
  # "Our post entity already has `id` field (which is a string),
  # but it should be resolved to a Relay global ID."
  id: ID!
    @resolve @source(name: "id", type: "String!")

  # "title, publishedAt, and content are simple scalars
  # and our Post entity have the exact same fields."
  title: String!

  publishedAt: Date!

  content: String!

  author: User!
    @resolve @source(name: "authorId", type: "String!")
    # "We store post-author relations as `authorId` field in `Post` entity.
    # This author resolver will load the actual User entity from the database."
}
```

Based on the schema, GQG generates its source and resolver types like below:

```ts
export interface PostSource {
  __typename?: "Post"
  readonly id: string
  readonly title: string
  readonly publishedAt: any
  readonly content: string
  readonly authorId: string
}
```

```ts
export interface PostResolver<TContext> {
  __type?: (source: any, context: TContext, info: graphql.GraphQLResolveInfo) => boolean
  id!: (src:  PostSource, args: {}, context: TContext, info: graphql.GraphQLResolveInfo) => string | null | undefined | Promise<string | null | undefined>
  author!: (src:  PostSource, args: {}, context: TContext, info: graphql.GraphQLResolveInfo) => UserSource | null | undefined | Promise<UserSource | null | undefined>
}
```

Now, you can implement `PostResolver` and pass the resolver instance to a function `createGraphQLSchema`
which is also created by GQG.

```ts
const schema: graphql.Schema = createGraphQLSchema({
  Post: {
    id: source => {
      return encodeGlobalId(PostTypename, source.id)
    },
    author: async source => {
      return users.find(item => item.id === source.authorId)
    },
  },
})
```

## Schema directory structure

When you execute `gqg (SCHEMA_DIR) (OUTPUT_FILE)`, GQG collects all files in SCHEMA directory.

Let's run it on the [test schema](schema/).

```
schema/
├── Post.graphql
├── User.graphql
├── relay.graphql
└── root.graphql
```

Execution of `gqg schema/ src/__test__/testSchema.ts` creates a single file to the
location of `src/__test__/testSchema.ts`. You can check [the generated code](src/__test__/testSchema.ts).

It should be much more complex in a real application though,
you can also find [the test implementation](src/__test__/schemaExecution.test.ts).

---
_Source: https://npm.io/package/gqg · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
