项目作者: codex-team

项目描述 :
GraphQL directive for applying default values to nullable fields
高级语言: TypeScript
项目地址: git://github.com/codex-team/graphql-directive-default.git
创建时间: 2021-02-12T10:07:33Z
项目社区:https://github.com/codex-team/graphql-directive-default

开源协议:MIT License

下载


GraphQL @default directive

@codexteam/graphql-directive-default"">NPM version
@codexteam/graphql-directive-default"">License

GraphQL directive for applying default values to nullable fields

  1. type Query {
  2. valueString: String! @default(value: "default value") # will return "default value"
  3. valueBoolean: Boolean! @default(value: "true") # will return true
  4. valueInt: Int! @default(value: "0") # will return 0
  5. valueArray: [String]! @default(value: "[]") # will return empty array
  6. valueObject: [ObjectType]! @default(value: "{}") # will return empty object
  7. }

Installation

  1. npm i @codexteam/graphql-directive-default
  2. # OR via yarn
  3. yarn add @codexteam/graphql-directive-default

How to use

  1. Import lib
  1. import createDirectiveDefault from '@codexteam/graphql-directive-default';
  1. Add directive to schemaTransforms
  1. const schema = makeExecutableSchema({
  2. typeDefs,
  3. resolvers,
  4. schemaTransforms: [
  5. createDirectiveDefault().schemaTransformer,
  6. ],
  7. });
  1. Add directive definitions to GraphQL types
  1. directive @default(value: String!) on FIELD_DEFINITION
  1. Now you can add directive to your field definition
  1. type Query {
  2. valueString: String! @default(value: "default value") # will return "default value"
  3. valueBoolean: Boolean! @default(value: "true") # will return true
  4. valueInt: Int! @default(value: "0") # will return 0
  5. valueArray: [String]! @default(value: "[]") # will return empty array
  6. valueObject: [ObjectType]! @default(value: "{}") # will return empty object
  7. }

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