-
Notifications
You must be signed in to change notification settings - Fork 1
Graphql - wprowadzenie #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
696fe90
f82a070
80f6fc3
ff2aaf0
3b9081e
f9cc92c
71644ea
b6920de
9adc01a
e7c5699
cf74e36
8451f44
dcc3ac8
2779f8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
|
|
||
| namespace Controller; | ||
|
|
||
| use Slim\Http\Response; | ||
| use \Psr\Http\Message\ServerRequestInterface as Request; | ||
| use GraphQL\Type\Definition\ObjectType; | ||
| use GraphQL\GraphQL; | ||
| use GraphQL\Type\Schema; | ||
| use GraphQL\Type\Definition\Type; | ||
| use GraphQLElo\Resolvers\UsersResolver; | ||
| use GraphQLElo\Types\UserType; | ||
|
|
||
| class GraphqlCtrl { | ||
| /** @var UsersResolver */ | ||
| private $usersResolver; | ||
|
|
||
| /** @var UserType */ | ||
| private $userType; | ||
|
|
||
| public function __construct(UsersResolver $usersResolver, UserType $userType) { | ||
| $this->usersResolver = $usersResolver; | ||
| $this->userType = $userType; | ||
| } | ||
|
|
||
| public function api(Request $request, Response $response): Response { | ||
| $schema = new Schema([ | ||
| 'query' => new ObjectType([ | ||
| 'name' => 'Query', | ||
| 'fields' => $this->getQueryFields(), | ||
| ]), | ||
| ]); | ||
|
|
||
| $rawInput = $request->getBody(); | ||
| $input = json_decode($rawInput, true); | ||
| $query = $input['query']; | ||
| $variableValues = $input['variables'] ?? null; | ||
|
|
||
| try { | ||
| $result = GraphQL::executeQuery($schema, $query, null, null, $variableValues); | ||
| $output = $result->toArray(); | ||
| } catch (\Exception $e) { | ||
| $output = [ | ||
| 'errors' => [ | ||
| [ | ||
| 'message' => $e->getMessage() | ||
| ] | ||
| ] | ||
| ]; | ||
| } | ||
|
|
||
| return $response->withJson($output); | ||
| } | ||
|
|
||
| private function getQueryFields(): array { | ||
| return [ | ||
| 'users' => [ | ||
| 'type' => Type::nonNull(Type::listOf(Type::nonNull($this->userType))), | ||
| 'resolve' => $this->usersResolver, | ||
| ], | ||
| ]; | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| namespace GraphQLElo\Resolvers; | ||
|
|
||
| use Model\Entity\User; | ||
| use Model\Repository\UserRepository; | ||
|
|
||
| class UsersResolver { | ||
| /** @var UserRepository */ | ||
| private $userRepository; | ||
|
|
||
| public function __construct(UserRepository $userRepository) | ||
| { | ||
| $this->userRepository = $userRepository; | ||
| } | ||
|
|
||
| /** | ||
| * @return User[]; | ||
| */ | ||
| public function __invoke(): array | ||
| { | ||
| return $this->userRepository->findBy( | ||
| ['deleted' => 0], | ||
| ['rating' => 'DESC', 'code' => 'ASC'] | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| namespace GraphQLElo\Types; | ||
|
|
||
| use Model\Entity\User; | ||
| use Service\UsersSvc; | ||
| use GraphQL\Type\Definition\ObjectType; | ||
| use GraphQL\Type\Definition\Type; | ||
|
|
||
| class UserType extends ObjectType { | ||
| public function __construct(UsersSvc $usersSvc) { | ||
| $config = [ | ||
| 'fields' => [ | ||
| 'userNid' => [ | ||
| 'type' => Type::nonNull(Type::int()), | ||
| 'resolve' => static function (User $user) { | ||
| return $user->getUserNid(); | ||
| }, | ||
| ], | ||
| 'code' => [ | ||
| 'type' => Type::nonNull(Type::string()), | ||
| 'resolve' => static function (User $user) { | ||
| return $user->getCode(); | ||
| }, | ||
| ], | ||
| 'name' => [ | ||
| 'type' => Type::nonNull(Type::string()), | ||
| 'resolve' => static function (User $user) { | ||
| return $user->getName(); | ||
| }, | ||
| ], | ||
| 'rating' => [ | ||
| 'type' => Type::nonNull(Type::int()), | ||
| 'resolve' => static function (User $user) { | ||
| return $user->getRating(); | ||
| }, | ||
| ], | ||
| 'team' => [ | ||
| 'type' => Type::nonNull(Type::string()), | ||
| 'resolve' => static function (User $user) { | ||
| return $user->getTeam(); | ||
| }, | ||
| ], | ||
| 'trendRatingDiff' => [ | ||
| 'type' => Type::nonNull(Type::int()), | ||
| 'resolve' => static function (User $user) use ($usersSvc) { | ||
| return $usersSvc->calculateTrendRatingDiffForGql($user); | ||
| }, | ||
| ], | ||
| ], | ||
| ]; | ||
| parent::__construct($config); | ||
| } | ||
|
|
||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,11 @@ | |
| "@glennsl/bs-json", | ||
| "@ahrefs/bs-reactstrap", | ||
| "@ahrefs/bs-recharts", | ||
| "reason-react-document-title" | ||
| "reason-react-document-title", | ||
| "reason-apollo" | ||
| ], | ||
| "ppx-flags": [ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pytanie] Cóż to?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sam tego dobrze nie obczajam ale ppx pozwala na wprowadzenie swojej składni która się kompiluje do czegoś (jakiegoś modułu czy coś takiego). Żeby można było używać jakieś zewnętrzene ppxy to trzeba to tutaj zadeklarować. To co tutaj używam to graphql_ppx - czyli daje on możliwość używania składni graphqla - wymaga go reason-apollo aby działał. |
||
| "graphql_ppx/ppx" | ||
| ], | ||
| "refmt": 3 | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Zakładam, że resolvery służą do przetworzenia parametrów Requesta i na ich podstawie zwrócenia odpowiednich danych? Czy tutaj w jakiś sposób czasem te parametry nie powinny być przekazywane? Chodzi mi o to, że np. czy kod
['rating' => 'DESC', 'code' => 'ASC']nie powinien czasem być jakoś przekazywany jako parametr mówiący o tym według czego ma być sortowanie?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tak, możnaby zrobić jakiś input typ który by tym sterował i wtedy możnaby tym sterować z FE, ale narazie to jest wprowadzenie graphqlea - narazie bym zostawł w przyszłości jak będzie potrzeba będzie można to oczywiście zmienić