1
<?php
2

3
namespace Nuwave\Lighthouse\Subscriptions;
4

5
use Closure;
6
use GraphQL\Type\Definition\ResolveInfo;
7
use Illuminate\Support\Str;
8
use Nuwave\Lighthouse\Exceptions\DefinitionException;
9
use Nuwave\Lighthouse\Schema\AST\ASTHelper;
10
use Nuwave\Lighthouse\Schema\Directives\SubscriptionDirective;
11
use Nuwave\Lighthouse\Schema\Types\GraphQLSubscription;
12
use Nuwave\Lighthouse\Schema\Values\FieldValue;
13
use Nuwave\Lighthouse\Subscriptions\Exceptions\UnauthorizedSubscriber;
14
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
15
use Nuwave\Lighthouse\Support\Contracts\ProvidesSubscriptionResolver;
16
use Nuwave\Lighthouse\Support\Utils;
17

18
class SubscriptionResolverProvider implements ProvidesSubscriptionResolver
19
{
20
    /**
21
     * @var \Nuwave\Lighthouse\Subscriptions\SubscriptionRegistry
22
     */
23
    protected $subscriptionRegistry;
24

25 1
    public function __construct(SubscriptionRegistry $subscriptionRegistry)
26
    {
27 1
        $this->subscriptionRegistry = $subscriptionRegistry;
28
    }
29

30
    /**
31
     * Provide a resolver for a subscription field in case no resolver directive is defined.
32
     *
33
     * @throws \Nuwave\Lighthouse\Exceptions\DefinitionException
34
     */
35 1
    public function provideSubscriptionResolver(FieldValue $fieldValue): Closure
36
    {
37 1
        $fieldName = $fieldValue->getFieldName();
38

39 1
        $directive = ASTHelper::directiveDefinition($fieldValue->getField(), SubscriptionDirective::NAME);
40 1
        if ($directive !== null) {
41 0
            $className = ASTHelper::directiveArgValue($directive, 'class');
42
        } else {
43 1
            $className = Str::studly($fieldName);
44
        }
45

46 1
        $className = Utils::namespaceClassname(
47 1
            $className,
48 1
            $fieldValue->defaultNamespacesForParent(),
49
            function (string $class): bool {
50 1
                return is_subclass_of($class, GraphQLSubscription::class);
51
            }
52
        );
53

54 1
        if (! $className) {
55 0
            throw new DefinitionException(
56 0
                "No class found for the subscription field {$fieldName}"
57
            );
58
        }
59

60
        /** @var \Nuwave\Lighthouse\Schema\Types\GraphQLSubscription $subscription */
61 1
        $subscription = app($className);
62

63
        // Subscriptions can only be placed on a single field on the root
64
        // query, so there is no need to consider the field path
65 1
        $this->subscriptionRegistry->register(
66 1
            $subscription,
67
            $fieldName
68
        );
69

70
        return function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($subscription, $fieldName) {
71 1
            if ($root instanceof Subscriber) {
72 1
                return $subscription->resolve($root->root, $args, $context, $resolveInfo);
73
            }
74

75 1
            $subscriber = new Subscriber(
76 1
                $args,
77
                $context,
78
                $resolveInfo
79
            );
80

81 1
            if (! $subscription->can($subscriber)) {
82 0
                throw new UnauthorizedSubscriber(
83 0
                    'Unauthorized subscription request'
84
                );
85
            }
86

87 1
            $this->subscriptionRegistry->subscriber(
88
                $subscriber,
89 1
                $subscription->encodeTopic($subscriber, $fieldName)
90
            );
91

92 1
            return null;
93 1
        };
94
    }
95
}

Read our documentation on viewing source code .

Loading