-
Notifications
You must be signed in to change notification settings - Fork 558
/
apollo-link.spec.ts
130 lines (127 loc) 路 3.48 KB
/
apollo-link.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
import { createServer, Server } from 'node:http'
import { AddressInfo } from 'node:net'
import { parse } from 'graphql'
import { ApolloClient, FetchResult, InMemoryCache } from '@apollo/client/core'
import { YogaLink } from '@graphql-yoga/apollo-link'
import { createSchema, createYoga } from 'graphql-yoga'
describe.skip('Yoga Apollo Link', () => {
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 Apollo 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: ApolloClient<unknown>
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 = new ApolloClient({
link: new YogaLink({
endpoint: url,
fetch: yoga.fetch as WindowOrWorkerGlobalScope['fetch'],
}),
cache: new InMemoryCache(),
})
})
afterAll((done) => {
server.close(() => done())
})
it('should handle queries correctly', async () => {
const result = await client.query({
query: parse(/* GraphQL */ `
query Greetings {
hello
}
`),
})
expect(result.error).toBeUndefined()
expect(result.errors?.length).toBeFalsy()
expect(result.data).toEqual({
hello: 'Hello Apollo Client!',
})
})
it('should handle subscriptions correctly', async () => {
expect.assertions(5)
const observable = client.subscribe({
query: parse(/* GraphQL */ `
subscription Time {
time
}
`),
})
const collectedValues: string[] = []
let i = 0
await new Promise<void>((resolve) => {
const subscription = observable.subscribe((result: FetchResult) => {
collectedValues.push(result.data?.time)
i++
if (i > 2) {
subscription.unsubscribe()
resolve()
}
})
})
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 result = await client.mutate({
mutation: parse(/* GraphQL */ `
mutation readFile($file: File!) {
readFile(file: $file)
}
`),
variables: {
file: new yoga.fetchAPI.File(['Hello World'], 'file.txt', {
type: 'text/plain',
}),
},
})
expect(result.errors?.length).toBeFalsy()
expect(result.data).toEqual({
readFile: 'Hello World',
})
})
})