Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated and removed deprecated packages related to graphql subscriptions #1482

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 52 additions & 80 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions packages/application-tester/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
"@apollo/client": "3.7.13",
"@boostercloud/framework-types": "workspace:^1.20.0",
"cross-fetch": "3.1.5",
"graphql": "^16.6.0",
"graphql": "^16.8.1",
"jsonwebtoken": "9.0.1",
"subscriptions-transport-ws": "0.11.0",
"tslib": "^2.4.0",
"ws": "8.12.0",
"@types/sinon": "10.0.0",
Expand Down
109 changes: 17 additions & 92 deletions packages/application-tester/src/graphql-helper.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import {
ApolloClient,
ApolloClientOptions,
ApolloLink,
HttpLink,
InMemoryCache,
NormalizedCacheObject,
split,
} from '@apollo/client'
import { ApolloClient, ApolloLink, HttpLink, InMemoryCache, NormalizedCacheObject, split } from '@apollo/client'
import { WebSocketLink } from '@apollo/client/link/ws'
import fetch from 'cross-fetch'
import * as WebSocket from 'ws'
import { SubscriptionClient } from 'subscriptions-transport-ws'
import { ProviderTestHelper } from './provider-test-helper'
import { getMainDefinition } from '@apollo/client/utilities'

Expand All @@ -31,23 +21,31 @@ export class GraphQLHelper {
})
}

/**
* IMPORTANT: After using this "DisconnectableApolloClient", you must call ".disconnect()" to close the socket. Otherwise
* it will keep waiting for messages forever
*/
public async clientWithSubscriptions(authToken?: AuthToken): Promise<DisconnectableApolloClient> {
const subscriptionClient: SubscriptionClient = await this.subscriptionsClient(authToken)
public async clientWithSubscriptions(authToken?: AuthToken): Promise<ApolloClient<NormalizedCacheObject>> {
const wsLink = new WebSocketLink({
uri: this.providerTestHelper.outputs.websocketURL,
options: {
reconnect: true,
connectionParams: () => {
if (authToken) {
const token = typeof authToken == 'function' ? authToken() : authToken
return { Authorization: 'Bearer ' + token }
}
return {}
},
},
})

const link = split(
({ query }) => {
const definition = getMainDefinition(query)
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription'
},
new WebSocketLink(subscriptionClient),
wsLink,
this.getAuthLink(authToken).concat(this.getApolloHTTPLink())
)

return new DisconnectableApolloClient(subscriptionClient, {
return new ApolloClient({
cache: new InMemoryCache(),
link: link,
defaultOptions: {
Expand All @@ -74,77 +72,4 @@ export class GraphQLHelper {
return forward(operation)
})
}

private async subscriptionsClient(authToken?: AuthToken): Promise<SubscriptionClient> {
return new Promise((resolve, reject) => {
const subscriptionClient = new SubscriptionClient(
this.providerTestHelper.outputs.websocketURL,
{
reconnect: true,
connectionParams: () => {
if (authToken) {
const token = typeof authToken == 'function' ? authToken() : authToken
return {
Authorization: 'Bearer ' + token,
}
}
return {}
},
connectionCallback: (err?: any) => {
if (err) {
reject(err)
return
}
resolve(subscriptionClient)
},
},
class MyWebSocket extends WebSocket {
public constructor(url: string, protocols?: string | string[]) {
super(url, protocols)

this.addListener('open', (): void => {
console.debug('[GraphQL socket] on open')
})
this.addListener('ping', (): void => {
console.debug('[GraphQL socket] on "ping"')
})
this.addListener('pong', (): void => {
console.debug('[GraphQL socket] on "pong"')
})
this.addListener('message', (data: WebSocket.Data): void => {
console.debug('[GraphQL socket] on message: ', data)
})
this.addListener('close', (code: number, message: string): void => {
console.debug('[GraphQL socket] on close: ', code, message)
})
this.addListener('error', (err: Error): void => {
console.debug('[GraphQL socket] on error: ', err.message)
})
}
}
)
})
}
}

export class DisconnectableApolloClient extends ApolloClient<NormalizedCacheObject> {
constructor(
private readonly subscriptionClient: SubscriptionClient,
options: ApolloClientOptions<NormalizedCacheObject>
) {
super(options)
}

public reconnect(): Promise<void> {
const reconnectPromise = new Promise<void>((resolve) => {
this.subscriptionClient.onReconnected(resolve)
})
this.subscriptionClient.close(false)
return reconnectPromise
}

public disconnect(): void {
this.subscriptionClient.close()
this.stop()
}
}
4 changes: 2 additions & 2 deletions packages/framework-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"node": ">=18.0.0 <19.0.0"
},
"peerDependencies": {
"graphql": "^16.6.0"
"graphql": "^16.8.1"
},
"dependencies": {
"@boostercloud/framework-common-helpers": "workspace:^1.20.0",
Expand Down Expand Up @@ -84,7 +84,7 @@
"ts-node": "^10.9.1",
"typescript": "4.7.4",
"eslint-plugin-unicorn": "~44.0.2",
"graphql": "^16.6.0"
"graphql": "^16.8.1"
},
"pnpm": {
"overrides": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('the `BoosterGraphQLDispatcher`', () => {
await dispatcher.dispatch({})

expect(config.provider.graphQL.handleResult).to.have.been.calledOnceWithExactly(null, {
'Sec-WebSocket-Protocol': 'graphql-ws',
'Sec-WebSocket-Protocol': 'graphql-transport-ws',
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@boostercloud/framework-provider-aws-infrastructure": "whatever",
"ts-patch": "whatever",
"@boostercloud/metadata-booster": "whatever",
"graphql": "^16.6.0"
"graphql": "^16.8.1"
},
"engines": {
"node": ">=18.0.0 <19.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { applicationName, checkAndGetCurrentEnv, getProviderTestHelper } from '.
import { internet, random, commerce, finance } from 'faker'
import { waitForIt } from '../../../helper/sleep'
import { ApplicationTester } from '@boostercloud/application-tester'
import { gql } from '@apollo/client'
import { gql, ApolloClient, NormalizedCacheObject } from '@apollo/client'

describe('After deployment', () => {
describe('the ARM template', () => {
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('After deployment', () => {
async () => {
try {
console.log('Performing mutation')
const client = applicationUnderTest.graphql.client(authToken)
const client: ApolloClient<NormalizedCacheObject> = applicationUnderTest.graphql.client(authToken)
return await client.mutate({
variables: {
sku: random.uuid(),
Expand Down
Loading
Loading