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

register_graphql_enum_type

Given a Type Name and a $config array, this adds an EnumType to the TypeRegistry

Parameters

  • $type_name (string): The unique name of the EnumType
  • $config (array): Configuration for the EnumType
    • $description (string): Description of the enum type. This will be used to self-document the schema and should describe to clients how the enum type should be used.
    • $values (array): The possible values of the enum.

Source

File: access-functions.php

Examples

In GraphQL, Enum Types are used to provide a predefined set of values.

An example of an Enum in WPGraphQL is the AvatarRatingEnum. Avatars in GraphQL can have a rating that is one of a predefined list of values: GPGR, and X. Since we know all the options before hand, we can expose fields to resolve to the AvatarRatingEnum type and ensure that it’s always one of those predefined values.

Let’s say we wanted to be able to query for the current weather, and the responses should always be one of the following: SunnyCloudyRainy

This would add an Enum to our Type registry, but it wouldn’t be in use yet. We could add a field that resolves to this Type:

Now we could query:

And get a response like: