This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from ardatan/graphql-tools
-
Notifications
You must be signed in to change notification settings - Fork 5
/
testUpload.ts
138 lines (121 loc) · 3.58 KB
/
testUpload.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
import { Server } from 'http';
import { AddressInfo } from 'net';
import { Readable } from 'stream';
import { expect } from 'chai';
import express, { Express } from 'express';
import graphqlHTTP from 'express-graphql';
import { GraphQLUpload, graphqlUploadExpress } from 'graphql-upload';
import FormData from 'form-data';
import fetch from 'node-fetch';
import { buildSchema } from 'graphql';
import { mergeSchemas } from '../stitch/index';
import { makeExecutableSchema } from '../generate/index';
import { createServerHttpLink } from '../links/index';
import { GraphQLUpload as ServerGraphQLUpload } from '../scalars/index';
import { SubschemaConfig } from '../Interfaces';
function streamToString(stream: Readable) {
const chunks: Array<Buffer> = [];
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(chunk));
stream.on('error', reject);
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
});
}
function startServer(e: Express): Promise<Server> {
return new Promise((resolve, reject) => {
e.listen(undefined, 'localhost', function (error) {
if (error) {
reject(error);
} else {
resolve(this);
}
});
});
}
function testGraphqlMultipartRequest(query: string, port: number) {
const body = new FormData();
body.append(
'operations',
JSON.stringify({
query,
variables: {
file: null,
},
}),
);
body.append('map', '{ "1": ["variables.file"] }');
body.append('1', 'abc', { filename: __filename });
return fetch(`http://localhost:${port.toString()}`, { method: 'POST', body });
}
describe('graphql upload', () => {
it('should return a file after uploading one', async () => {
const remoteSchema = makeExecutableSchema({
typeDefs: `
scalar Upload
type Query {
version: String
}
type Mutation {
upload(file: Upload): String
}
`,
resolvers: {
Mutation: {
upload: async (_root, { file }) => {
const { createReadStream } = await file;
const stream = createReadStream();
const s = await streamToString(stream);
return s;
},
},
Upload: GraphQLUpload,
},
});
const remoteApp = express().use(
graphqlUploadExpress(),
graphqlHTTP({ schema: remoteSchema }),
);
const remoteServer = await startServer(remoteApp);
const remotePort = (remoteServer.address() as AddressInfo).port;
const nonExecutableSchema = buildSchema(`
scalar Upload
type Query {
version: String
}
type Mutation {
upload(file: Upload): String
}
`);
const subschema: SubschemaConfig = {
schema: nonExecutableSchema,
link: createServerHttpLink({
uri: `http://localhost:${remotePort.toString()}`,
}),
};
const gatewaySchema = mergeSchemas({
schemas: [subschema],
resolvers: {
Upload: ServerGraphQLUpload,
},
});
const gatewayApp = express().use(
graphqlUploadExpress(),
graphqlHTTP({ schema: gatewaySchema }),
);
const gatewayServer = await startServer(gatewayApp);
const gatewayPort = (gatewayServer.address() as AddressInfo).port;
const query = `
mutation upload($file: Upload!) {
upload(file: $file)
}
`;
const res = await testGraphqlMultipartRequest(query, gatewayPort);
expect(await res.json()).to.deep.equal({
data: {
upload: 'abc',
},
});
remoteServer.close();
gatewayServer.close();
});
});