Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Last active August 4, 2020 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonbahl/15d7a0f42dcf47e98371ab35b4e2c416 to your computer and use it in GitHub Desktop.
Save jasonbahl/15d7a0f42dcf47e98371ab35b4e2c416 to your computer and use it in GitHub Desktop.
This is an example showing how to return a list of keys and values where the keys and values are both strings.
add_action( 'graphql_register_types', function() {
register_graphql_object_type( 'keyValue', [
'description' => __( 'Keys and their values, both cast as strings', 'your-textdomain' ),
'fields' => [
'key' => [
'type' => 'String',
],
'value' => [
'type' => 'String',
],
]
] );
register_graphql_field( 'RootQuery', 'listOfKeyValues', [
'type' => [ 'list_of' => 'KeyValue' ],
'description' => __( 'Field that resolves as a list of keys and values', 'your-textdomain' ),
'resolve' => function() {
$mock_array = [
'key1' => 'Value1',
'key2' => 'Value2',
'key3' => 'Value3'
];
$list = [];
foreach ( $mock_array as $key => $value ) {
$list[] = [
'key' => $key,
'value' => $value,
];
}
return $list;
}
] );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment