Homepage
Link to WPGraphQL on Github
Link to WPGraphQL on WordPress.org

register_graphql_scalar

Given a config array for a custom Scalar, this registers a Scalar for use in the Schema

Per the GraphQL Spec, WPGraphQL provides several built-in scalar types.

  • Boolean
  • Float
  • Integer
  • ID
  • String

This function is used to define new Scalars to the Schema. Some common cases are Url, Email, Date, and others.

Note: Future versions of WPGraphQL will likely support some custom Scalars out of the box. Stay tuned!

Parameters

  • $type_name (string): The name of the type to register
  • $config (array): Configuration for the field
    • $description (string): Description of the scalar. This will be used to self-document the schema and should describe how the scalar type should be used.
    • $serialize (function): A function used to serialize the value of the scalar. Serializes an internal value to include in a response.
    • $parseValue (function): A function used to parse the value of the scalar. Parses an externally provided value (query variable) to use as an input.
    • $parseLiteral (function): A function used to parse the value of the scalar. Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input.

Source

File: access-functions.php

Example

WPGraphQL ships with the out-of-the-box Scalars that are supported with GraphQL, however, it’s possible to define your own custom Scalars.

In order to register your own Scalar, you should understand how scalars are presented in GraphQL. GraphQL deals with scalars in following cases:

  1. When converting internal representation of value returned by your app (e.g. stored in a database or hardcoded in the source code) to serialized representation included in the response.
  2. When converting input value passed by a client in variables along with GraphQL query to internal representation of your app.
  3. When converting input literal value hardcoded in GraphQL query (e.g. field argument value) to the internal representation of your app.

In this example, we register a scalar called Email.

In order to view this new Scalar type, a new field can be registered.

This new field can now be queried:

Which will result in the following data to be returned:

This new field and Scalar type can be viewed in the GraphiQL documentation: