Skip to content

andwaredev-zz/graphql-safe-object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphql-safe-object

GraphQLSafeObjectType for GraphQL.js is an extension of GraphQLObjectType with the added ability to return its children when null.

Allows return of empty children elements when parent is not found, rather than returning null with no children elements.

Basic Usage

import { graphql, GraphQLSchema, GraphQLString } from 'graphql';
import GraphQLSafeObjectType from 'graphql-safe-object';

const data = {
  username: 'jsmith'
};

const safeObject = new GraphQLSafeObjectType({
  name: 'SafeObject',
  fields: {
    username: {
      type: GraphQLString
    },
    location: {
      type: new GraphQLSafeObjectType({
        name: 'Location',
        fields: {
          city: { type: GraphQLString },
          state: { type: GraphQLString },
          contact: {
            type: new GraphQLSafeObjectType({
              name: 'Contact',
              fields: {
                name: { type: GraphQLString }
              }
            })
          }
        }
      })
    }
  }
});

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      user: {
        type: safeObject,
        resolve: () => data
      }
    }
  })
});

graphql(schema, `
  query {
    user {
      username,
      location {
        city,
        state,
        contact {
          name
        }
      }
    }
  }
`)
  .then(({ data: { user } }) => {
    /**
     * user = {
     *   username: 'jsmith',
     *   location: {
     *     city: null,
     *     state: null,
     *     contact: {
     *       name: null
     *     }
     *   }
     * }
     */
     
    doSomething(user.location.contact.name); // won't throw an exception
  });

Additional Options

Providing a notFoundValue will override the default internal notFoundValue of that type.

...

const safeObject = new GraphQLSafeObjectType({
  name: 'SafeObject',
  fields: {
    username: {
      type: GraphQLString,
      notFoundValue: 'unknown'
    },
    location: {
      type: new GraphQLSafeObjectType({
        name: 'Location',
        fields: {
          city: { type: GraphQLString, notFoundValue: 'Boston' },
          state: { type: GraphQLString, notFoundValue: 'MA' },
          contact: {
            type: new GraphQLSafeObjectType({
              name: 'Contact',
              fields: {
                name: { type: GraphQLString }
              }
            })
          }
        }
      })
    }
  }
});

...

graphql(schema, `
  query {
    user {
      username,
      location {
        city,
        state,
        contact {
          name
        }
      }
    }
  }
`)
  .then(({ data: { user } }) => {
    /**
     * user = {
     *   username: 'jsmith',
     *   location: {
     *     city: 'Boston',
     *     state: 'MA',
     *     contact: {
     *       name: null
     *     }
     *   }
     * }
     */
     
    doSomething(user.location.city); // Boston
  });

About

GraphQLSafeObjectType for GraphQL.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published