Skip to content

Commit ba19937

Browse files
committed
Added new library for PHP: graphql-attribute-schema [server][php]
Easily build your GraphQL schema for **webonyx/graphql-php** using PHP attributes instead of large configuration arrays.
1 parent 5cab7e4 commit ba19937

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
name: graphql-attribute-schema
3+
description: Easily build your GraphQL schema for webonyx/graphql-php using PHP attributes instead of large configuration arrays.
4+
url: https://jerowork.github.io/graphql-attribute-schema
5+
github: jerowork/graphql-attribute-schema
6+
---
7+
8+
Easily build your GraphQL schema for **webonyx/graphql-php** using PHP attributes instead of large configuration arrays.
9+
10+
A simple example:
11+
12+
```php
13+
use Jerowork\GraphqlAttributeSchema\Attribute\Enum;
14+
use Jerowork\GraphqlAttributeSchema\Attribute\Field;
15+
use Jerowork\GraphqlAttributeSchema\Attribute\InputType;
16+
use Jerowork\GraphqlAttributeSchema\Attribute\Mutation;
17+
use Jerowork\GraphqlAttributeSchema\Attribute\Query;
18+
use Jerowork\GraphqlAttributeSchema\Attribute\Type;
19+
20+
final readonly class CreateUserMutation
21+
{
22+
#[Mutation]
23+
public function createUser(CreateUserInputType $input): User
24+
{
25+
// Business logic to create a user
26+
}
27+
}
28+
29+
final readonly class UserQuery
30+
{
31+
#[Query(description: 'Get a user')]
32+
public function user(int $userid): User
33+
{
34+
// Fetch and return user data
35+
}
36+
}
37+
38+
#[InputType]
39+
final readonly class CreateUserInputType
40+
{
41+
public function __construct(
42+
#[Field]
43+
public int $userId,
44+
#[Field]
45+
public string $name,
46+
#[Field(name: 'phoneNumber')]
47+
public ?string $phone,
48+
) {}
49+
}
50+
51+
#[Type]
52+
final readonly class User
53+
{
54+
// Define fields as class properties
55+
public function __construct(
56+
#[Field]
57+
public int $userId,
58+
#[Field]
59+
public string $name,
60+
public ?string $phone,
61+
#[Field(description: 'The status of the user')]
62+
public UserStatusType $status,
63+
) {}
64+
65+
// Define fields with methods for additional logic
66+
#[Field]
67+
public function getPhoneNumber(): string
68+
{
69+
return sprintf('+31%s', $this->phone);
70+
}
71+
}
72+
73+
#[Enum(description: 'The status of the user')]
74+
enum UserStatusType: string
75+
{
76+
case Created = 'CREATED';
77+
case Removed = 'REMOVED';
78+
}
79+
```
80+
81+
This will result in the following GraphQL schema:
82+
```graphql
83+
type Mutation {
84+
createUser(input: CreateUserInput!): User!
85+
}
86+
87+
type Query {
88+
user(userId: Int!): User!
89+
}
90+
91+
input CreateUserInput {
92+
userId: Int!
93+
name: String!
94+
phoneNumber: String
95+
}
96+
97+
type User {
98+
userId: Int!
99+
name: String!
100+
status: UserStatus!
101+
phoneNumber: String
102+
}
103+
104+
enum UserStatus {
105+
CREATED
106+
REMOVED
107+
}
108+
```
109+
110+
Available attributes: `Mutation`, `Query`, `Type`, `InterfaceType`, `InputType`, `Enum`, `EnumValue`, `Field`, `Arg`, `Autowire`, `Scalar`, `Cursor`

0 commit comments

Comments
 (0)