# gqlizer

> The power of GQL in its simplest form

Latest version **0.0.14** (published 2023-08-31) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.14 |
| Published | 2023-08-31 |
| First published | 2022-04-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 60 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Maxi Zipitria |
| Maintainers | maxizipitria |
| Keywords | gql, graphql, gql resolver, graphql resolver |

## Links

- npm: https://www.npmjs.com/package/gqlizer
- npm.io page: https://npm.io/package/gqlizer

## Dependencies (2)

- [graphql](https://npm.io/package/graphql.md) ^16.3.0
- [graphql-tag](https://npm.io/package/graphql-tag.md) ^2.12.6

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 0.0.14 (latest) — 2023-08-31
- 0.0.13 — 2023-06-10
- 0.0.12 — 2023-05-26
- 0.0.11 — 2022-10-18
- 0.0.10 — 2022-05-23
- 0.0.9 — 2022-04-23
- 0.0.8 — 2022-04-20
- 0.0.7 — 2022-04-19
- 0.0.6 — 2022-04-18
- 0.0.5 — 2022-04-18
- 0.0.4 — 2022-04-17

## README

# The power of GQL in its simplest form

GQLizer allows you to use GraphQL anywhere for anything.

**WARNING: This package is a work in progress and does not follow semantic versioning**.

## Usage

There's two ways to use GQLizer:

1. You can use the resolver directly

```typescript
import { gql, resolveQuery, ResolverFn } from "gqlizer";

const myAwesomeResolver: ResolverFn = (operations) => {
  // ...
};

const result = await resolveQuery({
  resolver: myAwesomeResolver,
  query: gql`
    {
      User(id: 1) {
        id
        firstName
        lastName
      }
    }
  `,
});

console.log(result);
```

2. Or you can create a "client" that you can export and use in your application.

```typescript
// resolveQuery.ts
import createQueryResolver, { ResolverFn } from "gqlizer";

const myAwesomeResolver: ResolverFn = (operations) => {
  // ...
};

export const resolveQuery = createQueryResolver(myAwesomeResolver);

// somewhere_else.ts
import { gql } from "gqlizer";
import { resolveQuery } from "./resolveQuery";

const result = await resolveQuery(gql`
  {
    User(id: 1) {
      id
      firstName
      lastName
    }
  }
`);

console.log(result);
```

## Resolver

The resolver API was thought so it can be called as little as possible.

The resolver function has a single argument, an array of operations. This is because GQLizer calls the resolver once per depth level, and not for each field. This gives you the chance to make the resolver as fast as possible.

For example: if you are trying to use GQLizer for your own REST API, you could potentially implement an endpoint to resolve the whole array of operations in a single call.

The resolver has to return an array of `DocumentLike` objects.

```typescript
type ResolverFn = (operations: Operation[]) => Promise<DocumentLike[]>;

type Operation = {
  handle: string; // The name of the field to resolve
  params?: QueryParams; //  Argument passed to the query of this field
  parent?: DocumentLike; // The result of the parent operation
};

type DocumentLike = Record<string, unknown>;
```

## Notes

1. This package re-exports `gql` and `GqlDocumentNode` from `graphql-tag` for convenience.

2. We **DO NOT** validate the query. You should do that yourself.

3. We export some utilities that we use under the hood, because why not?
   - `assert`: a simple assertion function
   - `combineArrays`: a function that merges two arrays
   - `groupBy`: a function that groups items by a key
   - `isStringArray`: a function that checks if an array is made of strings
   - `parseGql`: a function that transforms a query into a simpler object

## Full Example using Star Wars API (SWAPI)

https://stackblitz.com/edit/gqlizer-example-swapi?file=index.ts

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