Skip to content

Commit 0f5ae61

Browse files
committed
test: useSubscription
1 parent 89a0240 commit 0f5ae61

File tree

7 files changed

+133
-14
lines changed

7 files changed

+133
-14
lines changed

packages/test-e2e-composable-vue3/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@vue/apollo-util": "workspace:*",
2121
"graphql": "^16.7.1",
2222
"graphql-tag": "^2.12.6",
23+
"graphql-ws": "^5.15.0",
2324
"pinia": "^2.1.6",
2425
"test-server": "workspace:*",
2526
"vue": "^3.3.4",
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client/core'
1+
import { ApolloClient, InMemoryCache, createHttpLink, split } from '@apollo/client/core'
22
import { onError } from '@apollo/client/link/error'
3+
import { getMainDefinition } from '@apollo/client/utilities'
4+
import { GraphQLWsLink } from '@apollo/client/link/subscriptions'
35
import { logErrorMessages } from '@vue/apollo-util'
6+
import { createClient } from 'graphql-ws'
47

58
const cache = new InMemoryCache()
69

@@ -10,12 +13,32 @@ const httpLink = createHttpLink({
1013
uri: 'http://localhost:4042/graphql',
1114
})
1215

16+
const wsLink = new GraphQLWsLink(createClient({
17+
url: 'ws://localhost:4042/graphql',
18+
}))
19+
20+
const splitLink = split(
21+
({ query }) => {
22+
const definition = getMainDefinition(query)
23+
if (definition.kind === 'OperationDefinition' &&
24+
definition.operation === 'subscription') {
25+
console.log(`Subscribing to ${definition.name?.value ?? 'anonymous'}`)
26+
}
27+
return (
28+
definition.kind === 'OperationDefinition' &&
29+
definition.operation === 'subscription'
30+
)
31+
},
32+
wsLink,
33+
httpLink,
34+
)
35+
1336
// Handle errors
1437
const errorLink = onError(error => {
1538
logErrorMessages(error)
1639
})
1740

1841
export const apolloClient = new ApolloClient({
1942
cache,
20-
link: errorLink.concat(httpLink),
43+
link: errorLink.concat(splitLink),
2144
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<script lang="ts" setup>
2+
import { useSubscription } from '@vue/apollo-composable'
3+
import gql from 'graphql-tag'
4+
import { ref } from 'vue'
5+
6+
const messages = ref<Array<{ id: string, text: string }>>([])
7+
8+
const { onResult } = useSubscription(gql`subscription OnMessageAdded {
9+
messageAdded(channelId: "general") {
10+
id
11+
text
12+
}
13+
}`)
14+
15+
onResult((result) => {
16+
console.log(result.data?.messageAdded)
17+
if (result.data?.messageAdded) {
18+
messages.value.push(result.data.messageAdded)
19+
}
20+
})
21+
</script>
22+
23+
<template>
24+
<div class="space-y-2 p-2 border border-gray-200 rounded-xl">
25+
<div
26+
v-for="message in messages"
27+
:key="message.id"
28+
class="message px-4 py-2 bg-white rounded-lg"
29+
>
30+
{{ message.text }}
31+
</div>
32+
33+
<div v-if="!messages.length">
34+
No messages yet
35+
</div>
36+
</div>
37+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script lang="ts" setup>
2+
import MessageForm from './MessageForm.vue'
3+
import Subscription from './Subscription.vue'
4+
</script>
5+
6+
<template>
7+
<div class="m-12 space-y-4">
8+
<h1 class="text-2xl">
9+
Subscription
10+
</h1>
11+
<MessageForm channel-id="general" />
12+
<div class="flex gap-4">
13+
<Subscription
14+
v-for="n in 3"
15+
:key="n"
16+
class="flex-1"
17+
/>
18+
</div>
19+
</div>
20+
</template>

packages/test-e2e-composable-vue3/src/router.ts

+7
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,12 @@ export const router = createRouter({
7979
layout: 'blank',
8080
},
8181
},
82+
{
83+
path: '/subscriptions',
84+
component: () => import('./components/Subscriptions.vue'),
85+
meta: {
86+
layout: 'blank',
87+
},
88+
},
8289
],
8390
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
describe('Subscription', () => {
2+
beforeEach(() => {
3+
cy.task('db:reset')
4+
cy.visit('/')
5+
})
6+
7+
it('receive messages in real time', () => {
8+
cy.visit('/subscriptions')
9+
cy.get('input').type('Meow{enter}')
10+
cy.get('.message').should('have.length', 3)
11+
cy.get('.message').should('contain', 'Meow')
12+
cy.get('input').type('Waf{enter}')
13+
cy.get('.message').should('have.length', 6)
14+
cy.get('.message').should('contain', 'Waf')
15+
})
16+
})

pnpm-lock.yaml

+27-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)