graphql-default-fields

1.0.1 • Public • Published

GraphQL Default Fields

It normalizes graphql fields to return "zero valued" values when inexistent.

Usage

        const graphql = require('graphql');
        const withDefaults = require('graphql-default-fields')(graphql.GraphQLObjectType);
        
        const Obj = new GraphQLObjectType({
            name: 'Obj',
            fields: {
                boolean: {
                    type: GraphQLBoolean
                },
                float: {
                    type: GraphQLFloat
                },
                int: {
                    type: GraphQLInt
                },
                list: {
                    type: new GraphQLList(GraphQLString)
                },
                string: {
                    type: GraphQLString
                }
            }
        });

        // without any values, "Obj" is going return:
        {
            boolean: null,
            float: null,
            int: null,
            list: null,
            string: null
        }
        
        const ObjWithDefaults = new withDefaults.GraphQLObjectType({
            name: 'ObjWithDefaults',
            fields: {
                boolean: {
                    type: GraphQLBoolean,
                    defaultValue: true
                },
                float: {
                    type: GraphQLFloat,
                    defaultValue: 10.5
                },
                int: {
                    type: GraphQLInt,
                    defaultValue: 10
                },
                list: {
                    type: new GraphQLList(GraphQLString),
                    defaultValue: ['string']
                },
                string: {
                    type: GraphQLString,
                    defaultValue: 'string'
                }
            }
        });

        // without any values, "ObjWithDefaults" is going return:
        {
            boolean: false,
            float: 0,
            int: 0,
            list: [],
            string: ''
        }

Readme

Keywords

none

Package Sidebar

Install

npm i graphql-default-fields

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

43.8 kB

Total Files

5

Last publish

Collaborators

  • feliperohde