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
- Host: GitHub
- URL: https://github.com/codex-team/graphql-directive-default
- Owner: codex-team
- License: mit
- Created: 2021-02-12T10:07:33.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-11-25T07:54:15.000Z (over 4 years ago)
- Last Synced: 2025-09-22T03:37:29.499Z (9 months ago)
- Topics: directive, graphql, graphql-api
- Language: TypeScript
- Homepage:
- Size: 306 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GraphQL @default directive
[](https://www.npmjs.com/package/@codexteam/graphql-directive-default)
[](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)