1. Go to this page and download the library: Download ralali/graphql-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespaceApp\GraphQL\Types;
useApp\User;
useGraphQL\Type\Definition\Type;
useRebing\GraphQL\Support\TypeasGraphQLType;
classUserTypeextendsGraphQLType{
protected $attributes = [
'name' => 'User',
'description' => 'A user',
'model' => User::class,
];
publicfunctionfields(): array{
return [
'id' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The id of the user',
// Use 'alias', if the database column is different from the type name.// This is supported for discrete values as well as relations.// - you can also use `DB::raw()` to solve more complex issues// - or a callback returning the value (string or `DB::raw()` result)'alias' => 'user_id',
],
'email' => [
'type' => Type::string(),
'description' => 'The email of user',
],
// Uses the 'getIsMeAttribute' function on our custom User model'isMe' => [
'type' => Type::boolean(),
'description' => 'True, if the queried user is the current user',
'selectable' => false, // Does not try to query this from the database
]
];
}
// If you want to resolve the field yourself, you can declare a method// with the following format resolve[FIELD_NAME]Field()protectedfunctionresolveEmailField($root, $args){
return strtolower($root->email);
}
}
publicfunctionvalidationErrorMessages(array $args = []): array{
return [
'name.ase enter your email address',
'email.email' => 'Please enter a valid email address',
'email.exists' => 'Sorry, this email address is already in use',
];
}
namespaceApp\GraphQL\Types;
useApp\User;
useGraphQL\Type\Definition\Type;
useRebing\GraphQL\Support\Facades\GraphQL;
useRebing\GraphQL\Support\TypeasGraphQLType;
classUserTypeextendsGraphQLType{
/**
* @var array
*/protected $attributes = [
'name' => 'User',
'description' => 'A user',
'model' => User::class,
];
/**
* @return array
*/publicfunctionfields(): array{
return [
'uuid' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The uuid of the user'
],
'email' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The email of user'
],
'profile' => [
'type' => GraphQL::type('Profile'),
'description' => 'The user profile',
],
'posts' => [
'type' => Type::listOf(GraphQL::type('Post')),
'description' => 'The user posts',
// Can also be defined as a string'always' => ['title', 'body'],
]
];
}
}
classProfileTypeextendsGraphQLType{
protected $attributes = [
'name' => 'Profile',
'description' => 'A user profile',
'model' => UserProfileModel::class,
];
publicfunctionfields(): array{
return [
'name' => [
'type' => Type::string(),
'description' => 'The name of user'
]
];
}
}
classPostTypeextendsGraphQLType{
protected $attributes = [
'name' => 'Post',
'description' => 'A post',
'model' => PostModel::class,
];
publicfunctionfields(): array{
return [
'title' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The title of the post'
],
'body' => [
'type' => Type::string(),
'description' => 'The body the post'
]
];
}
}
classUserTypeextendsGraphQLType{
// ...publicfunctionfields(): array{
return [
// ...// Relation'posts' => [
'type' => Type::listOf(GraphQL::type('post')),
'description' => 'A list of posts written by the user',
'args' => [
'date_from' => [
'type' => Type::string(),
],
],
// $args are the local arguments passed to the relation// $query is the relation builder object// $ctx is the GraphQL context (can be customized by overriding `\Rebing\GraphQL\GraphQLController::queryContext`'query' => function(array $args, $query, $ctx){
return $query->where('posts.created_at', '>', $args['date_from']);
}
]
];
}
}
namespaceApp\GraphQL\Interfaces;
useGraphQL;
useGraphQL\Type\Definition\Type;
useRebing\GraphQL\Support\InterfaceType;
classCharacterInterfaceextendsInterfaceType{
protected $attributes = [
'name' => 'Character',
'description' => 'Character interface.',
];
publicfunctionfields(): array{
return [
'id' => [
'type' => Type::nonNull(Type::int()),
'description' => 'The id of the character.'
],
'name' => Type::string(),
'appearsIn' => [
'type' => Type::nonNull(Type::listOf(GraphQL::type('Episode'))),
'description' => 'A list of episodes in which the character has an appearance.'
],
];
}
publicfunctionresolveType($root){
// Use the resolveType to resolve the Type which is implemented trough this interface
$type = $root['type'];
if ($type === 'human') {
return GraphQL::type('Human');
} elseif ($type === 'droid') {
return GraphQL::type('Droid');
}
}
}
namespaceApp\GraphQL\Types;
useGraphQL;
useRebing\GraphQL\Support\TypeasGraphQLType;
useGraphQL\Type\Definition\Type;
classHumanTypeextendsGraphQLType{
protected $attributes = [
'name' => 'Human',
'description' => 'A human.'
];
publicfunctionfields(): array{
return [
'id' => [
'type' => Type::nonNull(Type::int()),
'description' => 'The id of the human.',
],
'name' => Type::string(),
'appearsIn' => [
'type' => Type::nonNull(Type::listOf(GraphQL::type('Episode'))),
'description' => 'A list of episodes in which the human has an appearance.'
],
'totalCredits' => [
'type' => Type::nonNull(Type::int()),
'description' => 'The total amount of credits this human owns.'
]
];
}
publicfunctioninterfaces(): array{
return [
GraphQL::type('Character')
];
}
}
publicfunctionfields(): array{
$interface = GraphQL::type('Character');
return [
$interface->getField('id'),
$interface->getField('name'),
$interface->getField('appearsIn'),
'totalCredits' => [
'type' => Type::nonNull(Type::int()),
'description' => 'The total amount of credits this human owns.'
]
];
}
publicfunctionfields(): array{
$interface = GraphQL::type('Character');
return array_merge($interface->getFields(), [
'totalCredits' => [
'type' => Type::nonNull(Type::int()),
'description' => 'The total amount of credits this human owns.'
]
]);
}
namespaceApp\GraphQL\InputObject;
useGraphQL\Type\Definition\Type;
useRebing\GraphQL\Support\InputType;
classReviewInputextendsInputType{
protected $attributes = [
'name' => 'ReviewInput',
'description' => 'A review with a comment and a score (0 to 5)'
];
publicfunctionfields(): array{
return [
'comment' => [
'name' => 'comment',
'description' => 'A comment (250 max chars)',
'type' => Type::string(),
// You can define Laravel Validation here'rules' => ['max:250']
],
'score' => [
'name' => 'score',
'description' => 'A score (0 to 5)',
'type' => Type::int(),
'rules' => ['min:0', 'max:5']
]
];
}
}
classUserTypeextendsGraphQLType{
// ...publicfunctionfields(): array{
return [
// ...// JSON column containing all posts made by this user'posts' => [
'type' => Type::listOf(GraphQL::type('post')),
'description' => 'A list of posts written by the user',
// Now this will simply request the "posts" column, and it won't// query for all the underlying columns in the "post" object// The value defaults to true'is_relation' => false
]
];
}
// ...
}
namespaceApp\GraphQL\Types;
useApp\User;
useGraphQL\Type\Definition\Type;
useRebing\GraphQL\Support\TypeasGraphQLType;
classUserTypeextendsGraphQLType{
protected $attributes = [
'name' => 'User',
'description' => 'A user',
'model' => User::class,
];
publicfunctionfields(): array{
return [
'id' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The id of the user',
],
'email' => [
'type' => Type::string(),
'description' => 'The email of user',
],
'address' => [
'type' => Type::string(),
'description' => 'The address of user',
'deprecationReason' => 'Deprecated due to address field split'
],
'address_line_1' => [
'type' => Type::string(),
'description' => 'The address line 1 of user',
],
'address_line_2' => [
'type' => Type::string(),
'description' => 'The address line 2 of user',
],
];
}
}