mathsgod / graphql-php-directive-resolvers
GraphQL directive resolver
1.0.1
2020-05-20 09:42 UTC
Requires
- guzzlehttp/promises: ^1.3
- webonyx/graphql-php: ^0.12.6
Requires (Dev)
- phpunit/phpunit: ^6.5
This package is auto-updated.
Last update: 2024-12-20 19:37:13 UTC
README
Example
Schema:
directive @upper on FIELD_DEFINITION
schema {
query: Query
}
type Query {
me: User
}
type User {
first_name:String @upper
last_name:String
}
Input:
query{
me{
first_name
}
}
Result:
Array
(
[data] => Array
(
[me] => Array
(
[first_name] => MY FIRST NAME
)
)
)
Code
require_once(__DIR__ . "/../vendor/autoload.php"); 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);