https://github.com/mathsgod/graphql-php-directive-resolvers
A schema directive resolvers for webonyx/graphql-php
https://github.com/mathsgod/graphql-php-directive-resolvers
graphql graphql-schema php
Last synced: 2 months ago
JSON representation
A schema directive resolvers for webonyx/graphql-php
- Host: GitHub
- URL: https://github.com/mathsgod/graphql-php-directive-resolvers
- Owner: mathsgod
- License: mit
- Created: 2019-01-11T02:52:08.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-05-20T09:42:45.000Z (about 6 years ago)
- Last Synced: 2025-10-08T19:58:25.727Z (8 months ago)
- Topics: graphql, graphql-schema, php
- Language: PHP
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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
```php
require_once(__DIR__ . "/../vendor/autoload.php");
use GraphQL\Utils\BuildSchema;
use GraphQL\GraphQL;
$schema_gql = <<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);
```