Skip to content

Commit 2c0adde

Browse files
committed
Implement user field.
1 parent 2530626 commit 2c0adde

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/Field/User/UserField.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Field\User;
4+
5+
use Youshido\GraphQL\Field\AbstractField;
6+
use Youshido\GraphQL\Execution\ResolveInfo;
7+
use Youshido\GraphQL\Type\NonNullType;
8+
use ProcessWire\GraphQL\Type\Object\UserType;
9+
10+
class UserField extends AbstractField {
11+
12+
public function getType()
13+
{
14+
return new NonNullType(new UserType());
15+
}
16+
17+
public function getName()
18+
{
19+
return 'user';
20+
}
21+
22+
public function getDescription()
23+
{
24+
return 'The current user of the app.';
25+
}
26+
27+
public function resolve($value, array $args, ResolveInfo $info)
28+
{
29+
return \ProcessWire\wire('user');
30+
}
31+
32+
}

src/Schema.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use ProcessWire\GraphQL\Field\TemplatedPageArray\TemplatedPageArrayField;
88
use ProcessWire\GraphQL\Field\Debug\DbQueryCountField;
99
use ProcessWire\GraphQL\Field\Auth\LoginField;
10+
use ProcessWire\GraphQL\Field\Auth\LogoutField;
11+
use ProcessWire\GraphQL\Field\User\UserField;
1012

1113

1214

@@ -33,6 +35,10 @@ public function build(SchemaConfig $config)
3335

3436
// Auth
3537
$query->addfield(new LoginField());
38+
$query->addfield(new LogoutField());
39+
40+
// User
41+
$query->addField(new UserField());
3642
}
3743

3844
public function getName()

src/Type/Object/UserType.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Type\Object;
4+
5+
use Youshido\GraphQL\Type\Object\AbstractObjectType;
6+
use Youshido\GraphQL\Type\Scalar\StringType;
7+
use Youshido\GraphQL\Type\Scalar\IdType;
8+
use Youshido\GraphQL\Type\NonNullType;
9+
10+
class UserType extends AbstractObjectType {
11+
12+
public function getName()
13+
{
14+
return 'User';
15+
}
16+
17+
public function getDescription()
18+
{
19+
return 'Represents ProcessWire User.';
20+
}
21+
22+
public function build($config)
23+
{
24+
$config->addField('name', [
25+
'type' => new NonNullType(new StringType()),
26+
'description' => "The user's login name.",
27+
'resolve' => function ($value) {
28+
return (string) $value->name;
29+
}
30+
]);
31+
32+
$config->addField('email', [
33+
'type' => new NonNullType(new StringType()),
34+
'description' => "The user's email address.",
35+
'resolve' => function ($value) {
36+
return (string) $value->email;
37+
}
38+
]);
39+
40+
$config->addField('id', [
41+
'type' => new NonNullType(new IdType()),
42+
'description' => "The user's id.",
43+
'resolve' => function ($value) {
44+
return (string) $value->id;
45+
}
46+
]);
47+
}
48+
49+
}

0 commit comments

Comments
 (0)