1. Go to this page and download the library: Download moxuandi/yii2-graphql 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/ */
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use yii\graphql\base\GraphQLQuery;
use yii\graphql\GraphQL;
class UserQuery extends GraphQLQuery
{
public function type()
{
return GraphQL::type(UserType::class);
}
public function args()
{
return [
'id'=>[
'type' => Type::nonNull(Type::id())
],
];
}
public function resolve($value, $args, $context, ResolveInfo $info)
{
return DataSource::findUser($args['id']);
}
}
use GraphQL\Type\Definition\Type;
use yii\graphql\base\GraphQLType;
use yii\graphql\GraphQL;
class UserType extends GraphQLType
{
protected $attributes = [
'name'=>'user',
'description'=>'user is user'
];
public function fields()
{
$result = [
'id' => ['type'=>Type::id()],
'email' => Types::email(),
'email2' => Types::email(),
'photo' => [
'type' => GraphQL::type(ImageType::class),
'description' => 'User photo URL',
'args' => [
'size' => Type::nonNull(GraphQL::type(ImageSizeEnumType::class)),
]
],
'firstName' => [
'type' => Type::string(),
],
'lastName' => [
'type' => Type::string(),
],
'lastStoryPosted' => GraphQL::type(StoryType::class),
'fieldWithError' => [
'type' => Type::string(),
'resolve' => function() {
throw new \Exception("This is error field");
}
]
];
return $result;
}
public function resolvePhotoField(User $user,$args){
return DataSource::getUserPhoto($user->id, $args['size']);
}
public function resolveIdField(User $user, $args)
{
return $user->id.'test';
}
public function resolveEmail2Field(User $user, $args)
{
return $user->email2.'test';
}
}
'hello' => "
query hello{hello}
",
'singleObject' => "
query user {
user(id:\"2\") {
id
email
email2
photo(size:ICON){
id
url
}
firstName
lastName
}
}
",
'multiObject' => "
query multiObject {
user(id: \"2\") {
id
email
photo(size:ICON){
id
url
}
}
stories(after: \"1\") {
id
author{
id
}
body
}
}
",
'updateObject' => "
mutation updateUserPwd{
updateUserPwd(id: \"1001\", password: \"123456\") {
id,
username
}
}
"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.