Laravel Lighthouse,进行订阅时如何获取所有数组数据?

问题描述 投票:0回答:1

我使用laravel + Lighthouse + Laravel WebSockets + vue-apollo技术进行订阅。

当我订阅时,我想获取所有数组数据,但是我只得到更改过的数据。

我的schema.graphql在下面。

type Mutation {
    updateTest(id: ID!, name: String, result: Int): Test @update
        @broadcast(subscription: "testSub")
}

type Subscription {
    testSub(id: ID): Test
}

type Test {
    id: ID!
    name: String
    result: Int
}

这是我的vue-apollo代码

const subQuery = gql`subscription testSub($id: ID) {
                testSub(id:$id) {
                    id
                    name
                }
            }`

            const observer = this.$apollo.subscribe({
                query: subQuery,
                variables () {
                    return {
                        id: 14,
                    }
                },
            })

            observer.subscribe({
                next (data) {
                    console.log('subscribe')
                    console.log(data)
                },
                error (error) {
                    console.log('err')
                    console.error(error)
                },
            })

当我做如下所示的突变时。

mutation {
    updateTest(id:14, name: "hahaha", result:1) {
        id
        name
    }
}

vue-apollo获得类似于图片的订阅。

enter image description here

我认识到回报只是更改后的值,而不是所有数据。所以我如下更改订阅模式。

type Subscription {
    testSub: [Test] @all
}

我也更改了vue-apollo代码。

const subQuery = gql`subscription testSub {  #delete argument
                testSub {                                #delete argument 
                    id
                    name
                }
            }`

            const observer = this.$apollo.subscribe({
                query: subQuery,
                variables () {
                    return {
                        id: 14,
                    }
                },
            })

            observer.subscribe({
                next (data) {
                    console.log('subscribe')
                    console.log(data)
                },
                error (error) {
                    console.log('err')
                    console.error(error)
                },
            })

当我在npm运行dev和websocket启动后做突变时,出现此错误。

enter image description here

但是我已经做了testSub。

php artisan灯塔:订阅测试Sub

这是我的testSub文件。

<?php

namespace App\GraphQL\Subscriptions;

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Subscriptions\Subscriber;
use Nuwave\Lighthouse\Schema\Types\GraphQLSubscription;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use App\Test as Test2;
use App\Events\test;

class TestSub extends GraphQLSubscription
{
    /**
     * Check if subscriber is allowed to listen to the subscription.
     *
     * @param  \Nuwave\Lighthouse\Subscriptions\Subscriber  $subscriber
     * @param  \Illuminate\Http\Request  $request
     * @return bool
     */
    public function authorize(Subscriber $subscriber, Request $request): bool
    {
        // TODO implement authorize
        return true;
    }

    /**
     * Filter which subscribers should receive the subscription.
     *
     * @param  \Nuwave\Lighthouse\Subscriptions\Subscriber  $subscriber
     * @param  mixed  $root
     * @return bool
     */
    public function filter(Subscriber $subscriber, $root): bool
    {
        return true;
        // TODO implement filter
    }

    public function encodeTopic(Subscriber $subscriber, string $fieldName): string
    {
        // Optionally create a unique topic name based on the
        // `author` argument.
        //$args = $subscriber->args;

        //return Str::snake($fieldName).':'.$args['author'];
        //return Str::snake($fieldName).':1';
        return 'testSub';
    }

    /**
     * Decode topic name.
     *
     * @param  string  $fieldName
     * @param  \App\Post  $root
     * @return string
     */
    public function decodeTopic(string $fieldName, $root): string
    {
        // Decode the topic name if the `encodeTopic` has been overwritten.
        $author_id = $root->author_id;

        //return Str::snake($fieldName).':'.$author_id;
        return 'testSub';
    }

    public function resolve($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Test2
    {
        event(new test());
        return $root;
    }

}

如何获取所有数组数据而不是已更改的数据?

subscription laravel-lighthouse
1个回答
0
投票

在vue-apollo代码中,您有以下代码:

 gql`subscription testSub($id: ID) {
                testSub(id:$id) {
                    id
                    name
                }
            }`

因此,认为它就像一个查询。每次触发订阅时,您都在查询idname。如果您还想查询result,只需添加它即可:

 gql`subscription testSub($id: ID) {
                testSub(id:$id) {
                    id
                    name
                    result # <--
                }
            }`

无法告诉阿波罗必须获取Test类型的“所有”字段。您必须明确显示这些字段。

© www.soinside.com 2019 - 2024. All rights reserved.