# core-types-graphql

> core-types ⬌ GraphQL conversion

Latest version **3.0.0** (published 2023-03-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install core-types-graphql
pnpm add core-types-graphql
yarn add core-types-graphql
bun add core-types-graphql
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2023-03-19 |
| First published | 2020-12-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14.13.1 \|\| >=16.0.0 |
| Dependencies | 2 |
| Unpacked size | 32.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Gustaf Räntilä |
| Maintainers | grantila |
| Keywords | core-types, type, types, graphql, gql |

## Links

- npm: https://www.npmjs.com/package/core-types-graphql
- Repository: https://github.com/grantila/core-types-graphql
- Homepage: https://github.com/grantila/core-types-graphql#readme
- Issues: https://github.com/grantila/core-types-graphql/issues
- npm.io page: https://npm.io/package/core-types-graphql

## Dependencies (2)

- [graphql](https://npm.io/package/graphql.md) ^16.6.0
- [core-types](https://npm.io/package/core-types.md) ^3.0.0

## 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

- 3.0.0 (latest) — 2023-03-19
- 2.0.0 — 2023-02-02
- 1.5.0 — 2022-06-19
- 1.4.0 — 2021-10-20
- 1.3.1 — 2021-10-17
- 1.3.0 — 2021-09-11
- 1.2.0 — 2021-02-16
- 1.1.0 — 2021-02-14
- 1.0.0 — 2020-12-05

## README

[![npm version][npm-image]][npm-url]
[![downloads][downloads-image]][npm-url]
[![build status][build-image]][build-url]
[![coverage status][coverage-image]][coverage-url]
[![Node.JS version][node-version]][node-url]


# core-types-graphql

This package provides conversion functions between [`core-types`][core-types-github-url] and GraphQL.

*You probably don't want to use this package directly, but rather [`typeconv`][typeconv-github-url] which uses this package to convert between TypeScript, JSON Schema and GraphQL.*

Other conversion packages:
 * [`core-types-ts`][core-types-ts-github-url]
 * [`core-types-json-schema`][core-types-json-schema-github-url]


# Versions

Since 3.0, this package is pure ESM and requires Node 14.13.1 or later.


# Contents

- [core-types-graphql](#core-types-graphql)
- [Versions](#versions)
- [Contents](#contents)
- [Usage](#usage)
	- [core-types to GraphQL](#core-types-to-graphql)
	- [GraphQL to core-types](#graphql-to-core-types)
- [Utilities](#utilities)


# Usage

There are two conversion functions, `convertCoreTypesToGraphql` and `convertGraphqlToCoreTypes`, both returning a wrapped value, of the type [`ConversionResult`](https://github.com/grantila/core-types#conversion).


## core-types to GraphQL

Conversion can be done to GraphQL *code* using `convertCoreTypesToGraphql`, but also to GraphQL *AST* using `convertCoreTypesToGraphqlAst`. The arguments to these are the same.

```ts
import { convertCoreTypesToGraphql } from 'core-types-graphql'

let doc; // This core-types document comes from somewhere

const { data: graphQL } = convertCoreTypesToGraphql( doc );
```

You can provide options as a second argument fn the type:

```ts
interface CoreTypesToGraphqlOptions
{
	warn?: WarnFunction;
	filename?: string;
	sourceFilename?: string;
	userPackage?: string;
	userPackageUrl?: string;
	nullTypeName?: string | null;
	nameGenerator?: NameGeneratorFunction;
	unsupported?: 'ignore' | 'warn' | 'error';
	includeComment?: boolean;
}
```

These options are all optional.

 * `warn`: A function callback to be used for warnings, defaults to `console.warn`.
 * `filename` The filename to be written to.<br />This is a hint, no file will be written by the conversion function.
 * `sourceFilename`: The name of the source file from which the core-types comes.
 * `userPackage`: The name of the package using this package.
 * `userPackageUrl`: The url to the package using this package.
 * `nullTypeName`: Optional custom type used for null.
 * `nameGenerator`: A function for generating names.<br />GraphQL doesn't support inline objects or union types,<br />so these must be constructed as separate types.
 * `unsupported`: What to do when detecting an unsupported type
   * `ignore`: Ignore (skip) type
   * `warn`: Ignore type, but warn (default)
   * `error`: Throw an error
 * `includeComment`: Includes a header comment about the auto-generated file.<br />Defaults to `true`.

The `warn` function is of type `WarnFunction` from [`core-types`][core-types-github-url], meaning it takes a message as string, and an optional second argument of type `CoreTypesErrorMeta`, also from [`core-types`][core-types-github-url].

The `nameGenerator` function is of type `NameGeneratorFunction` defined as:

```ts
( baseName: string, nameHint: string, test: NameGeneratorTestFunction ) => string;
```

where `NameGeneratorTestFunction` is a test function to check if the generated name is available, on the form:

```ts
( name: string ) => boolean;
```

If this is specified (instead of letting a default name generator be used), an implementation is supposed to generate a name, potentially based on the `baseName` and a `nameHint` (e.g. an interface name and a property name within that interface), and test this generated name against `test`, altering it if necessary until `test` returns `true`, and then return that string.


## GraphQL to core-types

```ts
import { convertGraphqlToCoreTypes } from 'core-types-graphql'

let graphQL; // This GraphQL string comes from somewhere

const { data: doc } = convertGraphqlToCoreTypes( graphQL );
```

An optional second argument can be provided on the form

```ts
interface GraphqlToCoreTypesOptions
{
	warn?: WarnFunction;
	unsupported?: 'ignore' | 'warn' | 'error';
}
```

 * `warn`: The same warn function as in [CoreTypesToGraphqlOptions](#core-types-to-graphql)
 * `unsupported`: What to do when detecting an unsupported type
   * `ignore`: Ignore (skip) type (default)
   * `warn`: Ignore type, but warn
   * `error`: Throw an error


# Utilities

This package exports two utility functions; `getBreakingChanges` and `getDangerousChanges` which both take two GraphQL source code texts (as strings) semantically meaning an "old" and a "new" version of a schema. The functions return a list of breaking/dangerous changes on the type `BreakingChange`/`DangerousChange` from the [`graphql`][graphql-npm-url] package.


[npm-image]: https://img.shields.io/npm/v/core-types-graphql.svg
[npm-url]: https://npmjs.org/package/core-types-graphql
[downloads-image]: https://img.shields.io/npm/dm/core-types-graphql.svg
[build-image]: https://img.shields.io/github/actions/workflow/status/grantila/core-types-graphql/master.yml?branch=master
[build-url]: https://github.com/grantila/core-types-graphql/actions?query=workflow%3AMaster
[coverage-image]: https://coveralls.io/repos/github/grantila/core-types-graphql/badge.svg?branch=master
[coverage-url]: https://coveralls.io/github/grantila/core-types-graphql?branch=master
[node-version]: https://img.shields.io/node/v/core-types-graphql
[node-url]: https://nodejs.org/en/

[typeconv-github-url]: https://github.com/grantila/typeconv
[core-types-github-url]: https://github.com/grantila/core-types
[core-types-ts-github-url]: https://github.com/grantila/core-types-ts
[core-types-json-schema-github-url]: https://github.com/grantila/core-types-json-schema
[graphql-npm-url]: https://npmjs.org/package/graphql

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