1

i am trying to create a graphql subcription with hasura, follow this example: Boilerplate to test GraphQL subscriptions using nodejs, just added types for trypescript, if i run it directly with nodemon works fine, and gets me the data i want but if try to make a tsc i get the following errors:

enter image description here

and my code for the subscription is:

/*****  Setup a GraphQL subscription observable  ******************************/

import { DocumentNode, execute, GraphQLRequest } from 'apollo-link';
import { WebSocketLink } from 'apollo-link-ws';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import ws from 'ws';

const getWsClient = (wsURL: string) => {
    const client = new SubscriptionClient(
        wsURL, {
            reconnect: true, connectionParams: () => {
                return {
                    headers: {
                        'x-hasura-admin-secret': process.env.HASURA_GRAPHQL_ADMIN_SECRET
                    }
                }
            }
    }, ws
    );
    return client;
};

// wsURL: GraphQL endpoint
// query: GraphQL query (use gql`` from the 'graphql-tag' library)
// variables: Query variables object
const createSubscription = (wsURL: string, query: DocumentNode, variables: Record<string, any>) => {
    const link = new WebSocketLink(getWsClient(wsURL));
    return execute(link, { query, variables } as GraphQLRequest);
};

export default createSubscription

I already updated typescript for "nodemon": "^2.0.15", "ts-node": "^10.2.1", "typescript": "^4.4.2"

does any one have any ideas?

Thanks in advance.

Solus
  • 31
  • 2
  • I just tried to add this to a empty nodejs/TS project, and it compiles for me. I had to use graphql ^15.0.0, not latest as it gave dependency errors. I also had to add a ws.d.ts file with "declare module 'ws';" for TS to stop complaining. After that I copied the code for the client and ran and got a response (using 'tsc -w' for compiling) and run from vs code. – Jesper Kristiansen Mar 05 '22 at 03:27

0 Answers0