Created
May 13, 2022 18:57
-
-
Save jasonbahl/3db3c60236c80df106ebd19561967ec5 to your computer and use it in GitHub Desktop.
Using the auth callbacks when registering fields to the Schema.
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_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