Laravel Lighthouse,订阅时如何获取所有数组数据? [英] Laravel Lighthouse, how can i get all array data when i do subscription?

查看:28
本文介绍了Laravel Lighthouse,订阅时如何获取所有数组数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I do subscription with laravel + Lighthouse + Laravel WebSockets + vue-apollo tech.

订阅时,我想获取所有数组数据,但我只获取了更改的数据.

When i subscription, i wanna get all array data, but i only got changed data.

我的 schema.graphql 在下面.

My schema.graphql is below.

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 代码

This is my vue-apollo code

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 像图片一样订阅.

vue-apollo get subscription like pic.

我认识到返回值只是改变的值而不是所有数据.所以我改变订阅模式如下.

I recognized return is only changed value instead of all data. So i change subscription schema like below.

type Subscription {
    testSub: [Test] @all
}

我也修改了 vue-apollo 代码.

I also changed vue-apollo code.

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 run dev 和 websocket start 之后做变异时,我得到了这个错误.

When i do mutation after npm run dev and websocket start, i got this error.

但我已经做了 testSub.

But i already made testSub.

php artisan lighthouse:subscription testSub

php artisan lighthouse:subscription testSub

这是我的 testSub 文件.

This is my testSub file.

<?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;
    }

}

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

How can i get all array data instead of changed data?

推荐答案

在你的 vue-apollo 代码中,你有:

In your vue-apollo code you have this:

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

所以,认为它就像一个查询.每次订阅被触发时,您都会查询 idname.如果您还想查询result,只需添加:

So, think that it is like a query. Everytime the subscription is fired, you are querying the id and name. If you want to also query the result, just add it:

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

没有办法告诉 apollo 必须获取 Test 类型的所有"字段.您必须明确这些字段.

There is no way to tell to apollo that has to fetch "all" fields of the Test type. You have to explicit those fields.

这篇关于Laravel Lighthouse,订阅时如何获取所有数组数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆