Last active
August 4, 2020 15:21
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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