An open API service indexing awesome lists of open source software.

https://github.com/codex-team/graphql-directive-default

GraphQL directive for applying default values to nullable fields
https://github.com/codex-team/graphql-directive-default

directive graphql graphql-api

Last synced: 4 months ago
JSON representation

GraphQL directive for applying default values to nullable fields

Awesome Lists containing this project

README

          

# GraphQL @default directive
[![NPM version](https://img.shields.io/npm/v/@codexteam/graphql-directive-default?style=flat-square)](https://www.npmjs.com/package/@codexteam/graphql-directive-default)
[![License](https://img.shields.io/npm/l/@codexteam/graphql-directive-default?style=flat-square)](https://www.npmjs.com/package/@codexteam/graphql-directive-default)

GraphQL directive for applying default values to nullable fields
```graphql
type Query {
valueString: String! @default(value: "default value") # will return "default value"
valueBoolean: Boolean! @default(value: "true") # will return true
valueInt: Int! @default(value: "0") # will return 0
valueArray: [String]! @default(value: "[]") # will return empty array
valueObject: [ObjectType]! @default(value: "{}") # will return empty object
}
```

## Installation

```shell
npm i @codexteam/graphql-directive-default

# OR via yarn

yarn add @codexteam/graphql-directive-default
```

## How to use

1. Import lib

```ts
import createDirectiveDefault from '@codexteam/graphql-directive-default';
```

2. Add directive to `schemaTransforms`

```ts
const schema = makeExecutableSchema({
typeDefs,
resolvers,
schemaTransforms: [
createDirectiveDefault().schemaTransformer,
],
});
```

3. Add directive definitions to GraphQL types

```graphql
directive @default(value: String!) on FIELD_DEFINITION
```

4. Now you can add directive to your field definition

```graphql
type Query {
valueString: String! @default(value: "default value") # will return "default value"
valueBoolean: Boolean! @default(value: "true") # will return true
valueInt: Int! @default(value: "0") # will return 0
valueArray: [String]! @default(value: "[]") # will return empty array
valueObject: [ObjectType]! @default(value: "{}") # will return empty object
}
```

## Contributing Guide

Feel free to open new issues and submit Pull Requests

## About team

We are CodeX and we build products for developers and makers.

Follow us on Twitter: [twitter.com/codex_team](https://twitter.com/codex_team)

Feel free to contact: team@codex.so

[codex.so](https://codex.so)