# graphql-tag-ts

> [![npm version](https://badge.fury.io/js/graphql-tag-ts.svg)](https://badge.fury.io/js/graphql-tag-ts)

Latest version **0.1.16** (published 2020-06-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install graphql-tag-ts
pnpm add graphql-tag-ts
yarn add graphql-tag-ts
bun add graphql-tag-ts
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.16 |
| Published | 2020-06-29 |
| First published | 2019-11-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | PabloSzx |
| Maintainers | pablosz |
| Keywords | apollo, gql, graphql, graphql-tag, react-apollo, typescript |

## Links

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

## Dependencies (1)

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

## 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.1.16 (latest) — 2020-06-29
- 0.1.15 — 2020-02-16
- 0.1.14 — 2020-01-28
- 0.1.13 — 2020-01-28
- 0.1.12 — 2019-12-06
- 0.1.11 — 2019-12-06
- 0.1.10 — 2019-12-06
- 0.1.9 — 2019-11-16
- 0.1.8 — 2019-11-16
- 0.1.7 — 2019-11-16
- 0.1.6 — 2019-11-16
- 0.1.5 — 2019-11-16
- 0.1.4 — 2019-11-16
- 0.1.3 — 2019-11-16
- 0.1.2 — 2019-11-16
- … 1 more at https://npm.io/package/graphql-tag-ts/versions

## README

# graphql-tag-ts

[![npm version](https://badge.fury.io/js/graphql-tag-ts.svg)](https://badge.fury.io/js/graphql-tag-ts)

```shell
yarn add graphql-tag-ts
```

or

```shell
npm install graphql-tag-ts
```

This project is basically **strongly typed** [graphql-tag](https://github.com/apollographql/graphql-tag) using [Module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html).

Just installing this package you will be able to inject the types in your query declarations, no need to declaring them elsewhere or having to declare them when calling your queries.

> queries file

```typescript
import gql, { DocumentNode } from "graphql-tag-ts";
import gql_two from "graphql-tag";
/**
 * You can import either version, this library
 * is only doing module augmentation
 * but using graphql-tag-ts is a safe bet
 */

export const query_one = gql<
  { hello: { world: string } },
  { variable: string }
>`
  query($variable: String!) {
    hello {
      world
    }
  }
`;

/**
 * You also can cast the DocumentNode type to the object.
 * This method is better if you don't want to lose
 * GraphQL query highlighting in your editor
 */

export const query_two: DocumentNode<
  { hello: { world: string } },
  { variable: string }
> = gql`
  query($variable: String!) {
    hello {
      world
    }
  }
`;
```

> application

```typescript
import { useQuery } from "@apollo/react-hooks";
import { query_one } from "./queries";

export default () => {
  // "data" is strongly typed!
  // data = { hello: { world: string } } | undefined
  const { data, loading } = useQuery(query_one, {
    // "variables" is strongly typed too!
    // variables = { variable: string } | undefined
    variables: {
      variable: "HelloWorld!"
    }
  });

  return <div>{loading ? "Loading..." : data?.hello.world ?? "Not found"}</div>;
};
```

## Functions available

---

The module augmentation needs to be specified for every function that is required, so as is right now we are augmenting:

From [**@apollo/react-hooks**](https://www.npmjs.com/package/@apollo/react-hooks)

- useQuery
- useMutation
- useLazyQuery
- useSubscription

From [**apollo-server-integration-testing**](https://github.com/zapier/apollo-server-integration-testing)

- query
- mutate

### If you want another library / function to be added please feel free to request it in [the issues](https://github.com/PabloSzx/graphql-tag-ts/issues) or send a pull request.

## Usage with [babel-plugin-graphql-tag](https://github.com/gajus/babel-plugin-graphql-tag)

---

To use [**babel-plugin-graphql-tag**](https://github.com/gajus/babel-plugin-graphql-tag) (_highly recommended_) all you need to specify is the **importName** configuration option in your babel configuration.

> .babelrc

```javascript
{
  ...
  "plugins": [
    ...
    ["babel-plugin-graphql-tag", { "importName": "graphql-tag-ts" }]
  ]
}

```

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