GraphQL scalar types for validation of GraphQL objects.
The flexibility of GraphQL means validation for your schemas is possible. GraphQL Scalar Types allows you to build types easily using a fluent interface, similar to libraries like Joi.
npm i graphql graphql-scalar-types
import 'GraphQLScalarTypes' from 'graphql-scalar-types';
export const GraphQLName = GraphQLScalarTypes.string('Name').min(0).max(36).create();
export const GraphQLAge = GraphQLScalarTypes.number('Age').min(0).max(120).create();
Here is a minimal example using Apollo Server:
const { ApolloServer, gql } = require('apollo-server');
const { default: GraphQLScalarTypes } = require('graphql-scalar-types');
const schemaString = gql`
# Make sure to define the scalars in the schema string
scalar Name
scalar Email
type Person {
name: Name
email: Email
}
type Query {
successfulPerson: Person
unsuccessfulPerson: Person
}
`;
const resolverFunctions = {
// Now define the resolvers using GraphQLScalarTypes
Name: GraphQLScalarTypes.string('Name').min(1).create(),
Email: GraphQLScalarTypes.string('Email').regex(/[\w\d]+@[\w\d]+\.\w+/, { name: 'Email Regex' }).create(),
Query: {
successfulPerson: () => ({
name: 'Foo Bar',
email: 'test@email.com'
}),
unsuccessfulPerson: () => ({
name: '',
email: 'invalid-email'
})
}
};
const server = new ApolloServer({
typeDefs: schemaString,
resolvers: resolverFunctions
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Constructs a builder with a name for the GraphQLScalar.
Parameters
name
string
Examples
const GraphQLStringBuilder = GraphQLScalarTypes.string('Name');
const GraphQLNumberBuilder = GraphQLScalarTypes.number('Age');
const GraphQLBooleanBuilder = GraphQLScalarTypes.boolean('SpecialFalsy');
Gives a description to the GraphQLScalar.
Parameters
description
string
Makes the GraphQLScalar reject null
as a value.
Parameters
isNonNull
boolean Default value is true. (optional, defaulttrue
)
Clones the builder.
Generates the GraphQLScalar.
Returns GraphQLScalarType<TInternal, TExternal>
Extends Base
Boolean scalar type that represents boolean data.
By itself, it is essentially the GraphQLBoolean
type.
Converts additional values to true
during serialization.
Accepts a value or an array of values.
Parameters
params
...any
Converts additional values to false
during serialization.
Accepts a value or an array of values.
Parameters
params
...any
Extends Base
String scalar type that takes in string data.
By itself, it is essentially the GraphQLString
type.
Extends Base
Number scalar type that represents numerical data.
By itself, it is essentially the GraphQLFloat
type.
Specifies the minimum number allowed (inclusive).
Parameters
limit
number
Specifies the maximum number allowed (inclusive).
Parameters
limit
number
Specifies that the value must be greater than the limit.
Parameters
limit
number
Specifies that the value must be lesser than the limit.
Parameters
limit
number
Requires the number to be an integer from -(2^31) and 2^31 - 1. As per the GraphQL Spec, Integers are only treated as valid when a valid 32-bit signed integer, providing the broadest support across platforms.
Specifies the maximum number of decimal places allowed.
Parameters
limit
number
Specifies that the number must be a multiple of base.
Parameters
base
number
Specifies that the number must be positive (>0).
Specifies that the number must be negative (<0).
Extends Base
String scalar type that represents string data.
By itself, it is essentially the GraphQLString
type.
Specifies the minimum number of string characters allowed.
Parameters
limit
number
Specifies the maximum number of string characters allowed.
Parameters
limit
number
Specifies the exact number of string characters required.
Parameters
limit
number
Specifies the length of the string to be truncated to if it exceeds.
Parameters
limit
number
Requires the string value to only contain a-z, A-Z, and 0-9.
Requires the string value to be a credit card number.
Requires the string value to match the regex test.
Parameters
pattern
RegExpoptions
booleanname
for regexp pattern andinvert
to disallow pattern instead. (optional, default{name:'',invert:false}
)
Replaces the regex matches of the string with the replacement
. Equivalent to String.prototype.replace
.
Parameters
Trims the string.
Requires the string to be a valid base64 string.
Requires the string to be a valid hexadecimal string.
Given a value and a GraphQL type, determine if the value will be accepted for that type.
Parameters
value
anytype
GraphQLInputType