0

谁能帮我理解为什么我会收到这个错误:

我的设置:

新的 sveltekit 项目。graphql 的 ASP.net 核心后端。在我修改它以包含订阅之前,查询是有效的。

错误信息:

Cannot read property 'executeQuery' of null
TypeError: Cannot read property 'executeQuery' of null
at C:\Repos\AGScada\node_modules\@urql\svelte\dist\urql-svelte.js:234:11
at C:\Repos\AGScada\node_modules\wonka\dist\wonka.js:254:18
at Object.next (C:\Repos\AGScada\node_modules\wonka\dist\wonka.js:1161:14)
at C:\Repos\AGScada\node_modules\@urql\svelte\dist\urql-svelte.js:173:11
at Object.subscribe (C:\Repos\AGScada\node_modules\svelte\store\index.js:53:9)
at Object.subscribe (C:\Repos\AGScada\node_modules\@urql\svelte\dist\urql-svelte.js:96:14)
at C:\Repos\AGScada\node_modules\@urql\svelte\dist\urql-svelte.js:168:14
at C:\Repos\AGScada\node_modules\wonka\dist\wonka.js:1159:9
at C:\Repos\AGScada\node_modules\wonka\dist\wonka.js:247:7
at C:\Repos\AGScada\node_modules\wonka\dist\wonka.js:590:18

源代码 index.svelte:

    import { setClient, query, operationStore, subscription } from '@urql/svelte';
    import { createClient, defaultExchanges, subscriptionExchange } from '@urql/svelte';
    import { createClient as createWSClient } from 'graphql-ws';
    const wsClient = process.browser ? createWSClient({url: 'ws://localhost:5000/graphql',}):null;  
    const client = process.browser ? createClient({
    url: 'http://localhost:5000/graphql',
    exchanges: [
      ...defaultExchanges,
      subscriptionExchange({
        forwardSubscription: (operation) => ({
          subscribe: (sink) => ({
            unsubscribe: wsClient.subscribe(operation, sink),
          }),
        }),
      }),
    ],
  }): null;

    let tags = operationStore(`query {tags{tagName value}}`);

    let onTagUpdated = operationStore(`subscription {onTagUpdated{tagName value}}`);

    query(tags);
    subscription(onTagUpdated);

    setClient(client);

    if($tags.fetching)
    {
        console.log("loading");
    }    
    else
    {
        console.log($tags.data);
    }

感谢您的任何帮助。

4

1 回答 1

0

我让 sveltekit 和 URQL 与订阅支持一起工作。

来源可以在这里找到: https ://github.com/ruban258/sveltekit_urql_test

import { Client, setClient, operationStore, subscription, defaultExchanges, subscriptionExchange, query, createClient } from '@urql/svelte';
import {SubscriptionClient} from 'subscriptions-transport-ws';
import { browser } from '$app/env';
import { createClient as createWSClient } from 'graphql-ws'; 


const subscriptionClient = browser ? new SubscriptionClient('ws://localhost:5000/graphql', { reconnect: true }): null;

const wsClient = browser? createWSClient({
  url: 'ws://localhost:5000/graphql',
}):null;

const client = browser ? new Client({
  url: 'http://localhost:5000/graphql',
  exchanges: [
    ...defaultExchanges,
    subscriptionExchange({
      forwardSubscription: (operation) => subscriptionClient.request(operation)
    }),
  ],
}): null;



const messages = operationStore(`
subscription onTagUpdated{
  onTagUpdated{
    tagName
    value
  }
}
  `);

const tags = operationStore(`
query{
  tags{
    tagName
    value
  }
}
  `);  

const handleSubscription = (messages = [], data) => {
return [data.newMessages, ...messages];
};


setClient(client);
于 2021-09-26T11:48:08.900 回答