PHP code example of diasfs / graphql-php-resolvers
1. Go to this page and download the library: Download diasfs/graphql-php-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/ */
diasfs / graphql-php-resolvers example snippets
use GraphQL\Utils\BuildSchema;
use GraphQL\GraphQL;
use GraphQL\Resolvers\FieldResolver;
$schema_gql = <<<gql
schema {
query Query
}
type Query {
hello:String
}
gql;
$data = array(
'Query' => array(
'hello' => "Hello World!"
)
);
$schema = BuildSchema::build($schema_gql, function($typeConfig) use ($data) {
return FieldResolver::TypeConfigDecorator($typeConfig, function($type_name, $field_name) use ($data) {
return $data[$type_name][$field_name];
});
});
$query = <<<gql
query {
hello
}
gql;
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues, $operationName);
$result = $result->toArray();
print_r($result);
Array
(
[data] => Array
(
[hello] => Hello World!
)
)
use GraphQL\Utils\BuildSchema;
use GraphQL\GraphQL;
use GraphQL\Resolvers\FieldResolver;
use GraphQL\Resolvers\DirectiveResolver;
$schema_gql = <<<gql
directive @upper on FIELD_DEFINITION | FIELD
directive @lower on FIELD_DEFINITION | FIELD
schema {
query Query
}
type Query {
hello:String @upper
world:String
}
gql;
$data = array(
'Query' => array(
'hello' => "Hello",
'world' => "World",
)
);
$schema = BuildSchema::build($schema_gql, function($typeConfig) use ($data) {
return FieldResolver::TypeConfigDecorator($typeConfig, function($type_name, $field_name) use ($data) {
return $data[$type_name][$field_name];
});
});
$directives = array(
'upper' => function($next, $directiveArgs, $value, $args, $context, $info) {
return $next($value, $args, $ctx, $info)->then(function($str) {
return strtolupper($str);
});
},
'lower' => function($next, $directiveArgs, $value, $args, $context, $info) {
return $next($value, $args, $ctx, $info)->then(function($str) {
return strtolower($str);
});
}
);
DirectivesResolver::bind($schema, function($name) use ($directives) {
return $directives[$name];
});
$query = <<<gql
query {
hello
world @lower
}
gql;
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues, $operationName);
$result = $result->toArray();
print_r($result);
Array
(
[data] => Array
(
[hello] => HELLO
[world] => world
)
)