smallorange-graphql-default-fieldsdeprecated

Normalizes graphql fields to return "zero valued" values when inexistent.

Usage no npm install needed!

<script type="module">
  import smallorangeGraphqlDefaultFields from 'https://cdn.skypack.dev/smallorange-graphql-default-fields';
</script>

README

CircleCI

Small Orange GraphQL Default Fields

Normalizes graphql fields to return "zero valued" values when inexistent.

Usage

        const graphql = require('graphql');
        const withDefaults = require('smallorange-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: ''
        }