question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

upgrading apollo client to latest version for unsupported transport package with websocket subscriptions

See original GitHub issue

Describe the bug

apollographql/subscriptions-transport-ws is not supported, nor maintained. Please, use https://github.com/enisdenjo/graphql-ws.

To Reproduce using websocket based subscription queries

Expected behavior not working and suggests switching graphql-ws which latest version of apollo-client uses

Environment:

- @angular/cli@XX.YY.ZZ
- @angular/core@XX.YY.ZZ
- @apollo/client@XX.YY.ZZ
- apollo-angular@XX.YY.ZZ
- graphql@XX.YY.ZZ
- typescript@XX.YY.ZZ

Additional context

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
neapluscommented, Jul 21, 2022

Subscription queries by using graphql-ws with apollo-angular@3x and angular@13x

package.json

"dependencies": {
...
    "@angular/core": "~13.x",
    "@apollo/client": "3.6.8",
    "apollo-angular": "^3.0.1",
    "graphql": "^16",
    "graphql-ws": "5.9.0",
...
},

graphql.module.ts

import { NgModule } from '@angular/core';
import { ApolloModule, APOLLO_OPTIONS } from 'apollo-angular';
import { HttpLink } from 'apollo-angular/http';
import { split, ApolloClientOptions, InMemoryCache } from '@apollo/client/core';
import { getMainDefinition } from '@apollo/client/utilities';
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { createClient } from 'graphql-ws';

@NgModule({
    exports: [ApolloModule],
    providers: [
        {
            provide: APOLLO_OPTIONS,
            useFactory(httpLink: HttpLink): ApolloClientOptions<any> {
                // Create an http link:
                const http = httpLink.create({
                    uri: 'http://localhost:3000/graphql',
                });
                // Create a websocket link:
                const ws = new GraphQLWsLink(
                    createClient({
                        url: 'ws://localhost:8420/graphql',
                    }),
                );

                // using the ability to split links, you can send data to each link
                // depending on what kind of operation is being sent
                const link = split(
                    ({ query }) => {
                        const call = getMainDefinition(query);
                        console.log('GraphQL call > ', call);
                        return (call.kind === 'OperationDefinition' && call.operation === 'subscription');
                    },
                    ws,
                    http,
                );

                return { link, cache: new InMemoryCache(), };
            },
            deps: [HttpLink],
        },
    ],
})
export class GraphQLModule { }

mysubscriptiongql.ts

import { Injectable } from '@angular/core';
import { gql, Subscription } from 'apollo-angular';

@Injectable({
    providedIn: 'root',
})
export class MySubscriptionGQL extends Subscription {
    document = gql`
      subscription {
        msg
      }
    `;
}

sample.component.ts

import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { Apollo, gql } from 'apollo-angular';
import { MySubscriptionGQL } from './mysubscriptiongql';

@Component({})
export class SampleComponent implements OnInit {

    constructor(private apollo: Apollo, private mySubscriptionGQL: MySubscriptionGQL) { }

    ngOnInit(): void {
        // 1. using apollo client directly
        this.apollo.subscribe({
            query: gql`subscription { msg }`,
        }).subscribe((result) => {
            console.log('apollo-client', result);
        });

        // 2. using apollo-angular Subscription definition
        this.mySubscriptionGQL.subscribe().subscribe((result) => {
            console.log('subscription-gql', result);
        });
    }
}
1reaction
neapluscommented, Jul 20, 2022

indeed, it works 🤌 thanks dude. I’ll drop a simple sample here as well in case of someone needs, since It’s not documented.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Subscriptions in Apollo Server - Apollo GraphQL Docs
Enabling subscriptions ; 1. import { createServer } from 'http'; ; 2. import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';.
Read more >
Migrating to Apollo Client 3.0 - Apollo GraphQL Docs
This article walks you through migrating your application to Apollo Client 3.0 from previous versions of Apollo Client. To illustrate the migration process, ......
Read more >
Migrating to Apollo Server 4 - Apollo GraphQL Docs
This packaging structure means that new integration package releases are lockstep versioned to Apollo Server itself, making it challenging to support major ...
Read more >
Migrating to Apollo Server 3 - Apollo GraphQL Docs
This migration guide shows how to reenable subscriptions-transport-ws to make your transition from Apollo Server 2 to Apollo Server 3 as easy as...
Read more >
WebSocket Link - Apollo GraphQL Docs
⚠️ We no longer recommend using WebSocketLink or the subscriptions-transport-ws library, because the library is not actively maintained. To execute ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found