1 | <?php |
||||
2 | /******************************************************************************* |
||||
3 | * This file is part of the GraphQL Bundle package. |
||||
4 | * |
||||
5 | * (c) YnloUltratech <[email protected]> |
||||
6 | * |
||||
7 | * For the full copyright and license information, please view the LICENSE |
||||
8 | * file that was distributed with this source code. |
||||
9 | ******************************************************************************/ |
||||
10 | |||||
11 | namespace Ynlo\GraphQLBundle\Util; |
||||
12 | |||||
13 | use GraphQL\Type\Definition\Type; |
||||
14 | use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
||||
15 | use Ynlo\GraphQLBundle\Annotation\Argument; |
||||
16 | use Ynlo\GraphQLBundle\Definition\ArgumentAwareInterface; |
||||
17 | use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface; |
||||
18 | use Ynlo\GraphQLBundle\Type\Registry\TypeRegistry; |
||||
19 | |||||
20 | class GraphQLBuilder |
||||
21 | { |
||||
22 | public static function resolveFields(FieldsAwareDefinitionInterface $definition): array |
||||
23 | { |
||||
24 | $fields = []; |
||||
25 | foreach ($definition->getFields() as $fieldDefinition) { |
||||
26 | try { |
||||
27 | $type = TypeRegistry::get($fieldDefinition->getType()); |
||||
28 | } catch (\UnexpectedValueException $exception) { |
||||
29 | $msg = sprintf( |
||||
30 | 'The property "%s" of object "%s" does not have valid type. %s', |
||||
31 | $fieldDefinition->getName(), |
||||
32 | $definition->getName(), |
||||
33 | $exception->getMessage() |
||||
34 | ); |
||||
35 | throw new \RuntimeException($msg); |
||||
36 | } |
||||
37 | |||||
38 | if ($fieldDefinition->isList()) { |
||||
39 | if ($fieldDefinition->isNonNullList()) { |
||||
40 | $type = Type::nonNull($type); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
41 | } |
||||
42 | $type = Type::listOf($type); |
||||
43 | } |
||||
44 | |||||
45 | if ($fieldDefinition->isNonNull()) { |
||||
46 | $type = Type::nonNull($type); |
||||
47 | } |
||||
48 | |||||
49 | $fields[$fieldDefinition->getName()] = [ |
||||
50 | 'type' => $type, |
||||
51 | 'description' => $fieldDefinition->getDescription(), |
||||
52 | 'deprecationReason' => $fieldDefinition->getDeprecationReason(), |
||||
53 | 'args' => GraphQLBuilder::buildArguments($fieldDefinition), |
||||
54 | 'complexity' => GraphQLBuilder::buildComplexityFn($fieldDefinition->getComplexity()), |
||||
55 | ]; |
||||
56 | } |
||||
57 | |||||
58 | return $fields; |
||||
59 | } |
||||
60 | |||||
61 | public static function buildArguments(ArgumentAwareInterface $argumentAware): array |
||||
62 | { |
||||
63 | $args = []; |
||||
64 | foreach ($argumentAware->getArguments() as $argDefinition) { |
||||
65 | $arg = []; |
||||
66 | $arg['description'] = $argDefinition->getDescription(); |
||||
67 | $argType = TypeRegistry::get($argDefinition->getType()); |
||||
68 | |||||
69 | if ($argDefinition->isList()) { |
||||
70 | if ($argDefinition->isNonNullList()) { |
||||
71 | $argType = Type::nonNull($argType); |
||||
0 ignored issues
–
show
$argType of type GraphQL\Type\Definition\Type is incompatible with the type GraphQL\Type\Definition\NullableType expected by parameter $wrappedType of GraphQL\Type\Definition\Type::nonNull() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
72 | } |
||||
73 | $argType = Type::listOf($argType); |
||||
74 | } |
||||
75 | |||||
76 | if ($argDefinition->isNonNull()) { |
||||
77 | $argType = Type::nonNull($argType); |
||||
78 | } |
||||
79 | |||||
80 | $arg['type'] = $argType; |
||||
81 | if ($argDefinition->getDefaultValue() !== Argument::UNDEFINED_ARGUMENT) { |
||||
82 | $arg['defaultValue'] = $argDefinition->getDefaultValue(); |
||||
83 | } |
||||
84 | $args[$argDefinition->getName()] = $arg; |
||||
85 | } |
||||
86 | |||||
87 | return $args; |
||||
88 | } |
||||
89 | |||||
90 | public static function buildComplexityFn(?string $complexity): ?callable |
||||
91 | { |
||||
92 | if (null === $complexity) { |
||||
93 | return null; |
||||
94 | } |
||||
95 | |||||
96 | if (is_numeric($complexity)) { |
||||
97 | return function ($childrenComplexity) use ($complexity) { |
||||
98 | return $childrenComplexity + $complexity; |
||||
99 | }; |
||||
100 | } |
||||
101 | |||||
102 | // support only static string callable func |
||||
103 | if (\is_string($complexity) && \is_callable($complexity)) { |
||||
104 | return $complexity; |
||||
0 ignored issues
–
show
|
|||||
105 | } |
||||
106 | |||||
107 | $el = new ExpressionLanguage(); |
||||
108 | |||||
109 | return function ($childrenComplexity, $args) use ($el, $complexity) { |
||||
110 | return $el->evaluate($complexity, array_merge($args, ['children_complexity' => $childrenComplexity])); |
||||
111 | }; |
||||
112 | } |
||||
113 | } |
||||
114 |