*/ public $args; /** * The variables passed to the subscription query. * * @var array */ public $variables; /** * The context passed to the query. * * @var \Nuwave\Lighthouse\Support\Contracts\GraphQLContext */ public $context; /** * @param array $args */ public function __construct( array $args, GraphQLContext $context, ResolveInfo $resolveInfo ) { $this->fieldName = $resolveInfo->fieldName; $this->channel = self::uniqueChannelName(); $this->args = $args; $this->variables = $resolveInfo->variableValues; $this->context = $context; /** * Must be here, since webonyx/graphql-php validated the subscription. * * @var \GraphQL\Language\AST\OperationDefinitionNode $operation */ $operation = $resolveInfo->operation; $this->query = new DocumentNode([ 'definitions' => new NodeList(array_merge( $resolveInfo->fragments, [$operation] )), ]); } /** * Unserialize subscription from a JSON string. * * @param string $subscription */ public function unserialize($subscription): void { $data = \Safe\json_decode($subscription, true); $this->channel = $data['channel']; $this->topic = $data['topic']; /** * We know the type since it is set during construction and serialized. * * @var \GraphQL\Language\AST\DocumentNode $documentNode */ $documentNode = AST::fromArray( unserialize($data['query']) ); $this->query = $documentNode; $this->fieldName = $data['field_name']; $this->args = $data['args']; $this->variables = $data['variables']; $this->context = $this->contextSerializer()->unserialize( $data['context'] ); } /** * Convert this into a JSON string. */ public function serialize(): string { return \Safe\json_encode([ 'channel' => $this->channel, 'topic' => $this->topic, 'query' => serialize( AST::toArray($this->query) ), 'field_name' => $this->fieldName, 'args' => $this->args, 'variables' => $this->variables, 'context' => $this->contextSerializer()->serialize($this->context), ]); } /** * Set root data. * * @return $this * @deprecated set the attribute directly */ public function setRoot($root): self { $this->root = $root; return $this; } /** * Generate a unique private channel name. */ public static function uniqueChannelName(): string { return 'private-lighthouse-'.Str::random(32).'-'.time(); } protected function contextSerializer(): ContextSerializer { return app(ContextSerializer::class); } }