@codexteam/graphql-directive-default
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

GraphQL @default directive

NPM version License

GraphQL directive for applying default values to nullable fields

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

npm i @codexteam/graphql-directive-default

# OR via yarn

yarn add @codexteam/graphql-directive-default

How to use

  1. Import lib
import createDirectiveDefault from '@codexteam/graphql-directive-default';
  1. Add directive to schemaTransforms
const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
    schemaTransforms: [
        createDirectiveDefault().schemaTransformer,
    ],
});
  1. Add directive definitions to GraphQL types
directive @default(value: String!) on FIELD_DEFINITION
  1. Now you can add directive to your field definition
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

Feel free to contact: team@codex.so

codex.so

Readme

Keywords

none

Package Sidebar

Install

npm i @codexteam/graphql-directive-default

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

28.1 kB

Total Files

17

Last publish

Collaborators

  • slaveeks
  • dsnostr
  • codex-team
  • khaydarovm