-
Notifications
You must be signed in to change notification settings - Fork 558
/
urql-exchange.spec.ts
145 lines (141 loc) 路 3.57 KB
/
urql-exchange.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { createServer, Server } from 'node:http'
import { AddressInfo } from 'node:net'
import { yogaExchange } from '@graphql-yoga/urql-exchange'
import { Client, createClient } from '@urql/core'
import { createSchema, createYoga } from 'graphql-yoga'
import { pipe, toObservable } from 'wonka'
describe.skip('URQL Yoga Exchange', () => {
const endpoint = '/graphql'
const hostname = '127.0.0.1'
const yoga = createYoga({
graphqlEndpoint: endpoint,
logging: false,
maskedErrors: false,
schema: createSchema({
typeDefs: /* GraphQL */ `
scalar File
type Query {
hello: String
}
type Mutation {
readFile(file: File!): String!
}
type Subscription {
time: String
}
`,
resolvers: {
Query: {
hello: () => 'Hello Urql Client!',
},
Mutation: {
readFile: (_, args: { file: File }) => args.file.text(),
},
Subscription: {
time: {
async *subscribe() {
while (true) {
await new Promise((resolve) => setTimeout(resolve, 1000))
yield new Date().toISOString()
}
},
resolve: (str) => str,
},
},
},
}),
})
let server: Server
let url: string
let client: Client
beforeAll(async () => {
server = createServer(yoga)
await new Promise<void>((resolve) => server.listen(0, hostname, resolve))
const port = (server.address() as AddressInfo).port
url = `http://${hostname}:${port}${endpoint}`
client = createClient({
url,
exchanges: [
yogaExchange({
fetch: yoga.fetch as WindowOrWorkerGlobalScope['fetch'],
}),
],
})
})
afterAll((done) => {
server.close(done)
})
it('should handle queries correctly', async () => {
const result = await client
.query(
/* GraphQL */ `
query Greetings {
hello
}
`,
{},
)
.toPromise()
expect(result.error).toBeUndefined()
expect(result.data).toEqual({
hello: 'Hello Urql Client!',
})
})
it('should handle subscriptions correctly', async () => {
const observable = pipe(
client.subscription(
/* GraphQL */ `
subscription Time {
time
}
`,
{},
),
toObservable,
)
const collectedValues: string[] = []
let i = 0
await new Promise<void>((resolve, reject) => {
const subscription = observable.subscribe({
next: (result) => {
collectedValues.push(result.data?.time)
i++
if (i > 2) {
subscription.unsubscribe()
resolve()
}
},
complete: () => {
resolve()
},
error: (error) => {
reject(error)
},
})
})
expect(collectedValues.length).toBe(3)
expect(i).toBe(3)
const now = new Date()
for (const value of collectedValues) {
expect(new Date(value).getFullYear()).toBe(now.getFullYear())
}
})
it('should handle file uploads correctly', async () => {
const query = /* GraphQL */ `
mutation readFile($file: File!) {
readFile(file: $file)
}
`
const result = await client
.mutation(query, {
file: new yoga.fetchAPI.File(['Hello World'], 'file.txt', {
type: 'text/plain',
}),
})
.toPromise()
expect(result.error).toBeFalsy()
expect(result.data).toEqual({
readFile: 'Hello World',
})
})
})