Completed
Pull Request — master (#15)
by Christoffer
06:33 queued 01:55
created

graphql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 7
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL;
4
5
use Digia\GraphQL\Error\GraphQLError;
6
use Digia\GraphQL\Execution\Execution;
7
use Digia\GraphQL\Execution\ExecutionResult;
8
use Digia\GraphQL\Language\AST\Node\NodeInterface;
9
use Digia\GraphQL\Language\AST\Schema\SchemaBuilderInterface;
10
use Digia\GraphQL\Language\LexerInterface;
11
use Digia\GraphQL\Language\ParserInterface;
12
use Digia\GraphQL\Language\PrinterInterface;
13
use Digia\GraphQL\Language\Source;
14
use Digia\GraphQL\Type\SchemaInterface;
15
16
/**
17
 * @param string|Source $source
18
 * @param array $options
19
 * @return NodeInterface
20
 * @throws GraphQLError
21
 * @throws \Exception
22
 */
23
function parse($source, array $options = []): NodeInterface
24
{
25
    return GraphQL::get(ParserInterface::class)->parse(
26
        GraphQL::get(LexerInterface::class)
27
            ->setSource($source instanceof Source ? $source : new Source($source))
28
            ->setOptions($options)
29
    );
30
}
31
32
/**
33
 * @param string|Source $source
34
 * @param array $options
35
 * @return NodeInterface
36
 * @throws GraphQLError
37
 * @throws \Exception
38
 */
39
function parseValue($source, array $options = []): NodeInterface
40
{
41
    return GraphQL::get(ParserInterface::class)->parseValue(
42
        GraphQL::get(LexerInterface::class)
43
            ->setSource($source instanceof Source ? $source : new Source($source))
44
            ->setOptions($options)
45
    );
46
}
47
48
/**
49
 * @param string|Source $source
50
 * @param array $options
51
 * @return NodeInterface
52
 * @throws GraphQLError
53
 * @throws \Exception
54
 */
55
function parseType($source, array $options = []): NodeInterface
56
{
57
    return GraphQL::get(ParserInterface::class)->parseType(
58
        GraphQL::get(LexerInterface::class)
59
            ->setSource($source instanceof Source ? $source : new Source($source))
60
            ->setOptions($options)
61
    );
62
}
63
64
/**
65
 * @param string $source
66
 * @param array $options
67
 * @return SchemaInterface
68
 * @throws \Digia\GraphQL\Error\GraphQLError
69
 * @throws \Exception
70
 */
71
function buildSchema(string $source, array $options = []): SchemaInterface
72
{
73
    return GraphQL::get(SchemaBuilderInterface::class)->build(parse($source, $options));
74
}
75
76
/**
77
 * @param NodeInterface $node
78
 * @return string
79
 */
80
function printNode(NodeInterface $node): string
81
{
82
    return GraphQL::getInstance()->get(PrinterInterface::class)->print($node);
83
}
84
85
/**
86
 * @param Schema        $schema
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
87
 * @param string        $source
88
 * @param null          $rootValue
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $variableValues is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $contextValue is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $rootValue is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $operationName is correct as it would always require null to be passed?
Loading history...
89
 * @param null          $contextValue
90
 * @param null          $variableValues
91
 * @param null          $operationName
92
 * @param callable|null $fieldResolver
93
 * @return ExecutionResult
94
 * @throws Error\GraphQLError
95
 * @throws \Exception
96
 */
97
function graphql(
98
    SchemaInterface $schema,
99
    string $source,
100
    $rootValue = null,
101
    $contextValue = null,
102
    $variableValues = null,
103
    $operationName = null,
104
    callable $fieldResolver = null
105
): ExecutionResult {
106
    /** @noinspection PhpParamsInspection */
107
    return Execution::execute(
108
        $schema,
109
        parse($source),
110
        $rootValue,
111
        $contextValue,
112
        $variableValues,
113
        $operationName,
114
        $fieldResolver
115
    );
116
}
117