@codexteam/graphql-directive-default
v1.1.0
Published
GraphQL directive for applying default values to nullable fields
Downloads
4
Keywords
Readme
GraphQL @default directive
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
- Import lib
import createDirectiveDefault from '@codexteam/graphql-directive-default';
- Add directive to
schemaTransforms
const schema = makeExecutableSchema({
typeDefs,
resolvers,
schemaTransforms: [
createDirectiveDefault().schemaTransformer,
],
});
- Add directive definitions to GraphQL types
directive @default(value: String!) on FIELD_DEFINITION
- 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: [email protected]