Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Created May 13, 2022 18:57
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/3db3c60236c80df106ebd19561967ec5 to your computer and use it in GitHub Desktop.
Save jasonbahl/3db3c60236c80df106ebd19561967ec5 to your computer and use it in GitHub Desktop.
Using the auth callbacks when registering fields to the Schema.
add_action( 'graphql_register_types', function() {
register_graphql_field( 'RootQuery', 'privateField', [
'type' => 'String',
'resolve' => function() {
return 'some private data';
},
'auth' => [
'callback' => function() {
return current_user_can( 'edit_posts' );
}
]
] );
register_graphql_mutation( 'doSomethingPrivate', [
'inputFields' => [],
'outputFields' => [
'somethingPrivate' => [
'type' => 'String',
]
],
'auth' => [
'callback' => function() {
return current_user_can( 'edit_posts' );
},
'errorMessage' => __( 'You do not have permission to "doSomethingPrivate"' ),
],
'mutateAndGetPayload' => function() {
return [
'somethingPrivate' => 'something private'
];
}
] );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment