npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

validate-graphql

v1.0.9

Published

NPM module that provides an efficient and elegant way to validate your GraphQL queries and mutations

Downloads

7

Readme

validate-graphql

NPM version License: MIT

validate-graphql is a simple and elegant module that provides you an easy way to validate your queries and mutation with your own logic and YUP validation module. This allows an excellent way to configure your own validation logic by accepting your own validation functions that can be executed before your resolver gets executed.

Features

  • Can be used to validate type validation and value validation
  • Provides and easy way to write your own validation logic before your resolver is invoked
  • Enables you to effectively modularize your codebase by delegating your validation logic into separate module.
  • Written on Pure Javascript.

Install

npm i --save validate-graphql

Usage

Please add the below line to import from validate-graphql

import { ValidateGraphql, ValidatedQueries, ValidatedMutations } from 'validate-graphql';

Lets say if you have your queries like below:

getAllUsers: {
  type: new GraphQLList(userType),
  args: {
  	id: userIdField,
  },
  resolve: resolveGetAllUsers,
}


createUser: {
  type: userType,
  args: {
  	name: userNameField,
	manager: userManagerField,
	email: userEmailField
  },
  resolve: resolveCreateUser,
}


let queries = [...getAllUsers, your_other_queries]
let mutations = [...createUser, your_other_mutations]

Use the below code on the file where you build your schema. This will build a custom schema on the application start.

 ValidateGraphql({ "user_queries": queries, "product_queries": product_queries }, { "user_mutations": mutations }); 

if you have only one queries array on the whole you can just use

ValidateGraphql({ "my_queries": queries }, { "my_mutations": mutations });

"my_queries" and "my_mutations" are user defined names where you can provide any name of your choice. queries, mutations are your array of graphql queries and graphql mutations respectively.

Then you should pass ValidatedQueries["my_queries"] hash and ValidatedMutations["my_mutations"] to your schema instead of queries and mutations

Please refer example below

 let schema = new GraphQLSchema({
	query: new GraphQLObjectType({
	name: 'RootQuery',
	
	fields: () => ValidatedQueries['my_queries'], 
	
	}),
	mutation: new GraphQLObjectType({
	name: 'RootMutation',
	
	fields: () => ValidatedMutations['my_mutations'],
	
	}),
   

So instead of passing your queries and mutations directly you have to use ValidatedQueries["queries"] and ValidatedMutations["mutations"] where "queries" and "mutations" arguments are the user defined names that you have given while invoking ValidateGraphql method.

And Finally you have to add validate key to your query and mutation for which you want validation to be done:

validate key will accept only a function. You will be getting args and context in the function that you will pass to the validate key.

The function that you give in "validate" should return a JSON with status field value as true/false. If status is true your resolver gets executed, else you will an error JSON with two keys { status: "Validation Failed", message: "Error message"}.

If you want to return custom error message from your own validate function you should pass data key in the return statement. Please see example below:

getAllUsers: {
  type: new GraphQLList(userType),
  args: {
  id: userIdField,
  role: userRoleField
  },
  resolve: resolveGetAllUsers,
  
  validate: function(args, context){ //Please note you will get only two arguments args and context
    if(args.role == 'admin'){
      return { status: true}
    }else {
      return { status: false, data: { code: "103", message: "My Custome Error message"}}
      //In case your validation returns false response will be
      //{ code: "103", message: "My Custome Error message"}
    }
  }
},

Using YUP

You can give your YUP schema on the field validationSchema in your query and mutation. Please see this link on how to create YUP SCHEMA.

NOTE: your "validationSchema" will not get validated if you are not passing "valildate" field in your query and mutation. Also your validationSchema will get exectuted first before your "validate" function.

The field validationSchema accepts a JSON with two keys schema and error_field. You should give your YUP schema in schema key and "errror_field" should contain a string, which is the field name for your errors in the response.

getAllUsers: {
  type: new GraphQLList(userType),
  args: {
  id: userIdField,
  role: userRoleField
  email: userEmailField
  },
  resolve: resolveGetAllUsers,
  
  validate: function(args, context){ //Please note you will get only two arguments args and context
    if(args.role == 'admin'){
      return { status: true}
    }else {
      return { status: false, data: { code: "103", message: "My Custome Error message"}}
      //In case your validation returns false response will be
      //{ code: "103", message: "My Custome Error message"}
    }
  },
  
  
  validationSchema: { schema: yupSchema, error_field: "errors" } //This will get executed before validate function
  //This will give { errors: "email is not valid" } as response for bad emails
  
},

NOTE: If you are not passing "validate" key and "validationSchema" key your normal resolvers will get invoked asusual.

In case if you have doubt please post an issue and I will make sure this code base is updated frequently.

Methods

ValidateGraphql(queriesJSON :JSON, mutationsJSON :JSON)

Please feel free to Contribute.

Please give me a pull request after making changes to your forked repo.

License

validate-graphql is released under the MIT license. See LICENSE for details.