# graphql-schema-query

> [![npm version](https://badge.fury.io/js/graphql-schema-query.svg)](https://badge.fury.io/js/graphql-schema-query) [![bundlephobia](https://badgen.net/bundlephobia/minzip/graphql-schema-query)](https://bundlephobia.com/result?p=graphql-schema-query) [![li

Latest version **0.1.7** (published 2020-04-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install graphql-schema-query
pnpm add graphql-schema-query
yarn add graphql-schema-query
bun add graphql-schema-query
```

## 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.1.7 |
| Published | 2020-04-22 |
| First published | 2020-04-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 1 |
| Unpacked size | 20.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | PabloSzx |
| Maintainers | pablosz |
| Keywords | graphql, schema, query, mutation, next.js, typegraphql, nexus |

## Links

- npm: https://www.npmjs.com/package/graphql-schema-query
- Repository: https://github.com/PabloSzx/graphql-schema-query
- npm.io page: https://npm.io/package/graphql-schema-query

## Dependencies (1)

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

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 0.1.7 (latest) — 2020-04-22
- 0.1.6 — 2020-04-22
- 0.1.5 — 2020-04-22
- 0.1.4 — 2020-04-21
- 0.1.3 — 2020-04-21
- 0.1.2 — 2020-04-21
- 0.1.1 — 2020-04-21
- 0.1.0 — 2020-04-21

## README

# graphql-schema-query

[![npm version](https://badge.fury.io/js/graphql-schema-query.svg)](https://badge.fury.io/js/graphql-schema-query)
[![bundlephobia](https://badgen.net/bundlephobia/minzip/graphql-schema-query)](https://bundlephobia.com/result?p=graphql-schema-query)
[![license](https://badgen.net/github/license/pabloszx/graphql-schema-query)](https://github.com/pabloszx/graphql-schema-query)

```sh
yarn add graphql-schema-query
# or
npm install graphql-schema-query
```

This small library is made to execute a GraphQL **query** or **mutation** without the need of any http request, and just using the GraphQL schema generated from [TypeGraphQL](https://typegraphql.com/), [Nexus Schema](https://www.nexusjs.org/#/components/schema/about) or any other.

Useful for **testing**, referencing your own resolvers without any hassle or **repeating code**.

And **specially** the reason I made this library, for [**getServerSideProps**](https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering) and [**getStaticProps**](https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation) usage in [Next.js](https://nextjs.org/)

## Usage

```ts
import schemaQuery from 'graphql-schema-query';

// ...
import { makeSchema } from '@nexus/schema';

export const schema = makeSchema({
  // ...
});

// or

import { buildSchemaSync } from 'type-graphql';

export const schema = buildSchemaSync({
  // ...
});

// ...

export const executeFromSchema = schemaQuery(schema);
```

Then, anywhere you need to use it:

```ts
import { executeFromSchema } from './[anywhere]';
import { gql } from 'graphql-squema-query';
// import gql from "graphql-tag"; also works

const helloWorld = async () => {
  // You can use a plain string
  executeFromSchema(`query {
        helloWorld
    }`);

  // Or you can use a graphql-tag document,
  // which gives you some nice editor formatting
  executeFromSchema(gql`
    query {
      helloWorld
    }
  `);
};
```

### Protip

This library is using [**graphql-tag-ts**](https://www.npmjs.com/package/graphql-tag-ts), which can give you some nice type-safety from the gql tag itself, writing less code without any performance penalty.

For example:

```ts
import { executeFromSchema } from './[anywhere]';
import { gql, DocumentNode } from 'graphql-schema-query';
// import gql, { DocumentNode } from "graphql-tag-ts"; also works

const HelloWorldQuery: DocumentNode<
  {
    helloWorld: string;
  },
  {
    anyVariable: string;
  }
> = gql`
  query($anyVariable: String!) {
    helloWorld(anyVariable: $anyVariable)
  }
`;

const helloWorld = async () => {
  const data = await executeFromSchema(HelloWorldQuery, {
    variables: {
      anyVariable: 'asd',
    },
  });

  // data === { helloWorld: string } | undefined | null
};
```

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