PHP code example of shmax / graphql-php-validation-toolkit
1. Go to this page and download the library: Download shmax/graphql-php-validation-toolkit library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shmax / graphql-php-validation-toolkit example snippets
//...
'updateBook' => new ValidatedFieldDefinition([
'name' => 'updateBook',
'type' => Types::book(),
'args' => [
'bookId' => [
'type' => Type::id(),
'validate' => function ($bookId) {
global $books;
if (!Book::find($bookId) {
return 0;
}
return [1, 'Unknown book!'];
},
],
],
'resolve' => static function ($value, $args) : bool {
return Book::find($args['bookId']);
},
],
//...
'updateAuthor' => new ValidatedFieldDefinition([
'type' => Types::author(),
'args' => [
'authorId' => [
'validate' => function(string $authorId) {
if(Author::find($authorId)) {
return 0;
}
return 1;
}
]
]
])
//...
'updateThing' => new ValidatedFieldDefinition([
'type' => Types::thing(),
'args' => [
'foo' => [
'n 1;
}
],
'bar' => [
'
//...
'updateAuthor' => new ValidatedFieldDefinition([
'type' => Types::author(),
'args' => [
'authorId' => [
'validate' => function(string $authorId) {
if(Author::find($authorId)) {
return 0;
}
return [1, "We can't find that author"];
}
]
]
])
//...
'setPhoneNumbers' => new ValidatedFieldDefinition([
'type' => Types::bool(),
'args' => [
'phoneNumbers' => [
'type' => Type::listOf(Type::string()),
'validate' => function(string $phoneNumber) {
$res = preg_match('/^[0-9\-]+$/', $phoneNumber) === 1;
if (!$res) {
return [1, 'That does not seem to be a valid phone number'];
}
return 0;
}
]
]
])
enum AuthorErrors {
case AuthorNotFound;
}
'updateAuthor' => [
'type' => Types::author(),
'errorCodes' => AuthorErrors::class,
'validate' => function(string $authorId) {
if(Author::find($authorId)) {
return 0;
}
return [AuthorErrors::AuthorNotFound, "We can't find that author"];
}
]
echo $errorType->name; // Author_Attributes_FirstName_PriceErrorCode
echo $errorType->name; // PriceErrorCode
new ValidatedFieldDefinition([
'typeSetter' => static function ($type) {
return Types::set($type);
},
]);
composer