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

Laravel Lighthouse, how can i get all array data when i do subscription?

我使用 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 像图片一样获取订阅。

我认识到 return 只是更改的值而不是所有数据。 所以我像下面这样更改订阅架构。

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 启动后进行突变时,我得到了这个错误。

但是我已经做了testSub.

php artisan lighthouse:subscription testSub

这是我的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;
    }

}

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

在你的 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 # <--
                }
            }`

无法告诉 apollo 必须获取 "all" 测试类型的字段。您必须明确这些字段。