GraphQL Fields API Reference

Register Fields to the Schema

@todo: Document the registration API

  • register_graphql_field()
  • deregister_graphql_field()

Register field for custom post

        register_graphql_field(
            'Document',
            'documentDateFrom',
            [
                'type'        => 'String',
                'description' => __( 'Document validity from', 'wp-graphql' ),
                'resolve'     => function( WP_Post $post ) {
                    $document_date = get_post_meta( $post->ID, 'date_from', true );
                    return $document_date;
                },
            ]
        );

Register field for user

        $hobbies = [
            'type'        => WPGraphQLTypes::list_of( WPGraphQLTypes::string() ),
            'description' => __( 'Custom field for user mutations', 'your-textdomain' ),
            'resolve'     => function( WP_User $user ) {
                $hobbies = get_user_meta( $user->ID, 'hobbies', true );
                return ! empty( $hobbies ) ? $hobbies : [];
            },
        ];

        register_graphql_field( 'User', 'hobbies', $hobbies );
        register_graphql_field( 'CreateUserInput', 'hobbies', $hobbies );
        register_graphql_field( 'UpdateUserInput', 'hobbies', $hobbies );

Filtering Field Definitions

@todo: Document how to filter existing field definitions

Root Fields

@todo: Document that fields on any Type in the Schema are the same, including Root Fields, they're
just fields on the RootQuery Type, so they serve as a root entry into the Schema.