PHP code example of mathsgod / graphql-php-directive-resolvers
1. Go to this page and download the library: Download mathsgod/graphql-php-directive-resolvers 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/ */
mathsgod / graphql-php-directive-resolvers example snippets
use GraphQL\Utils\BuildSchema;
use GraphQL\GraphQL;
$schema_gql = <<<gql
directive @upper on FIELD_DEFINITION
schema {
query: Query
}
type Query {
me: User
}
type User {
first_name:String @upper
last_name:String
}
gql;
$schema = BuildSchema::build($schema_gql);
$schema->getType("Query")->getField("me")->resolveFn = function ($root, $args, $context, $info) {
return ["first_name" => "my first name"];
};
$directiveResolvers = [
"upper" => function ($next, $source, $args, $context) {
return $next()->then(function ($str) {
return strtoupper($str);
});
}
];
attachDirectiveResolvers($schema, $directiveResolvers);
//----- query data
$query = "query{
me{
first_name
}
}
";
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues, $operationName);
$result = $result->toArray();
print_r($result);