1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DeInternetJongens\LighthouseUtils\Generators\Queries; |
4
|
|
|
|
5
|
|
|
use DeInternetJongens\LighthouseUtils\Models\GraphQLSchema; |
6
|
|
|
use DeInternetJongens\LighthouseUtils\Schema\Scalars\Date; |
7
|
|
|
use DeInternetJongens\LighthouseUtils\Schema\Scalars\DateTimeTz; |
8
|
|
|
use DeInternetJongens\LighthouseUtils\Schema\Scalars\Email; |
9
|
|
|
use DeInternetJongens\LighthouseUtils\Schema\Scalars\FullTextSearch; |
10
|
|
|
use GraphQL\Type\Definition\EnumType; |
11
|
|
|
use GraphQL\Type\Definition\FloatType; |
12
|
|
|
use GraphQL\Type\Definition\IDType; |
13
|
|
|
use GraphQL\Type\Definition\IntType; |
14
|
|
|
use GraphQL\Type\Definition\StringType; |
15
|
|
|
use GraphQL\Type\Definition\Type; |
16
|
|
|
use Nuwave\Lighthouse\Schema\Types\Scalars\DateTime; |
17
|
|
|
|
18
|
|
|
class PaginateAllQueryGenerator |
19
|
|
|
{ |
20
|
|
|
/** @var array */ |
21
|
|
|
private static $supportedGraphQLTypes = [ |
22
|
|
|
IDType::class, |
23
|
|
|
StringType::class, |
24
|
|
|
IntType::class, |
25
|
|
|
FloatType::class, |
26
|
|
|
Date::class, |
27
|
|
|
DateTime::class, |
28
|
|
|
DateTimeTz::class, |
29
|
|
|
EnumType::class, |
30
|
|
|
Email::class, |
31
|
|
|
FullTextSearch::class, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Generates GraphQL queries with arguments for each field |
36
|
|
|
* Returns a query for 'all' and 'paginated', depending on what kind of result you want |
37
|
|
|
* |
38
|
|
|
* @param string $typeName |
39
|
|
|
* @param Type[] $typeFields |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public static function generate(string $typeName, array $typeFields): string |
43
|
|
|
{ |
44
|
|
|
$arguments = []; |
45
|
|
|
|
46
|
|
|
foreach ($typeFields as $fieldName => $field) { |
47
|
|
|
$className = get_class($field); |
48
|
|
|
// We can generate queries for all but Object types, as Object types are relations |
49
|
|
|
if (! in_array($className, self::$supportedGraphQLTypes)) { |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($field instanceof FullTextSearch) { |
54
|
|
|
$arguments[] = sprintf('%s: %s @fulltext', $fieldName, $field->name); |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Add all our custom directives |
59
|
|
|
$arguments[] = sprintf('%s: %s @eq', $fieldName, $field->name); |
60
|
|
|
$arguments[] = sprintf('%s_not: %s @not', $fieldName, $field->name); |
61
|
|
|
$arguments[] = sprintf('%s_in: [%s] @in', $fieldName, $field->name); |
62
|
|
|
$arguments[] = sprintf('%s_not_in: [%s] @not_in', $fieldName, $field->name); |
63
|
|
|
|
64
|
|
|
if ($field instanceof EnumType) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($field instanceof StringType) { |
69
|
|
|
$arguments[] = sprintf('%s_contains: %s @contains', $fieldName, $field->name); |
70
|
|
|
$arguments[] = sprintf('%s_not_contains: %s @not_contains', $fieldName, $field->name); |
71
|
|
|
$arguments[] = sprintf('%s_starts_with: %s @starts_with', $fieldName, $field->name); |
72
|
|
|
$arguments[] = sprintf('%s_not_starts_with: %s @not_starts_with', $fieldName, $field->name); |
73
|
|
|
$arguments[] = sprintf('%s_ends_with: %s @not_ends_with', $fieldName, $field->name); |
74
|
|
|
|
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
$arguments[] = sprintf('%s_lt: %s @lt', $fieldName, $field->name); |
80
|
|
|
$arguments[] = sprintf('%s_lte: %s @lte', $fieldName, $field->name); |
81
|
|
|
$arguments[] = sprintf('%s_gt: %s @gt', $fieldName, $field->name); |
82
|
|
|
$arguments[] = sprintf('%s_gte: %s @gte', $fieldName, $field->name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (count($arguments) < 1) { |
86
|
|
|
return ''; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$allQueryName = str_plural(lcfirst($typeName)); |
90
|
|
|
$queryArguments = sprintf('(%s)', implode(', ', $arguments)); |
91
|
|
|
$allQuery = sprintf(' %1$s%2$s: [%3$s]! @all(model: "%3$s")', $allQueryName, $queryArguments, $typeName); |
92
|
|
|
|
93
|
|
|
$paginatedQueryName = str_plural(lcfirst($typeName)) . 'Paginated'; |
94
|
|
|
$paginatedQuery = sprintf(' %1$s%2$s: [%3$s]! @paginate(model: "%3$s")', $paginatedQueryName, $queryArguments, $typeName); |
95
|
|
|
|
96
|
|
|
if (config('lighthouse-utils.authorization')) { |
97
|
|
|
$allPermission = sprintf('All%1$s', str_plural($typeName)); |
98
|
|
|
$allQuery .= sprintf(' @can(if: "%1$s", model: "User")', $allPermission); |
99
|
|
|
|
100
|
|
|
$paginatePermission = sprintf('paginate%1$s', str_plural($typeName)); |
101
|
|
|
$paginatedQuery .= sprintf(' @can(if: "%1$s", model: "User")', $paginatePermission); |
102
|
|
|
|
103
|
|
|
GraphQLSchema::register($allQueryName, $typeName, 'query', $allPermission ?? null); |
104
|
|
|
GraphQLSchema::register($paginatedQueryName, $typeName, 'query', $paginatePermission ?? null); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
return $allQuery ."\r\n". $paginatedQuery; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|