Skip to content

Commit 0a04db3

Browse files
eokoneyoAlan-Cha
authored andcommitted
improve existing test, also add interagration test for upload flow
Signed-off-by: Eyo Okon Eyo <[email protected]>
1 parent e54e45e commit 0a04db3

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

packages/openapi-to-graphql/test/file_upload.test.ts

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import { afterAll, beforeAll, expect, test } from '@jest/globals'
44
import * as openAPIToGraphQL from '../src/index'
55
import * as Oas3Tools from '../src/oas_3_tools'
66

7-
import { startServer, stopServer } from './file_upload_server'
7+
import { Volume } from 'memfs'
8+
import FormData from 'form-data'
9+
import { createServer } from 'http'
10+
import fetch from 'cross-fetch'
811
import { graphql } from 'graphql'
12+
import { GraphQLOperation, processRequest } from 'graphql-upload'
13+
import { startServer as startAPIServer, stopServer as stopAPIServer } from './file_upload_api_server'
914

1015
/**
1116
* Set up the schema first
@@ -21,14 +26,14 @@ let createdSchema
2126
beforeAll(async () => {
2227
const [{ schema }] = await Promise.all([
2328
openAPIToGraphQL.createGraphQLSchema(oas),
24-
startServer(PORT)
29+
startAPIServer(PORT)
2530
])
2631

2732
createdSchema = schema
2833
})
2934

3035
afterAll(async () => {
31-
await stopServer()
36+
await stopAPIServer()
3237
})
3338

3439
test('All mutation endpoints are found to be present', () => {
@@ -68,6 +73,13 @@ test('introspection for mutations returns a mutation matching the custom field s
6873
mutationType {
6974
fields {
7075
name
76+
args {
77+
name
78+
type {
79+
name
80+
kind
81+
}
82+
}
7183
type {
7284
name
7385
kind
@@ -78,17 +90,84 @@ test('introspection for mutations returns a mutation matching the custom field s
7890
}`
7991

8092
const result = await graphql(createdSchema, query)
93+
8194
expect(result).toEqual({
8295
data: {
8396
__schema: {
8497
mutationType: {
8598
fields: expect.arrayContaining([
8699
expect.objectContaining({
87-
name: 'fileUploadTest'
100+
name: 'fileUploadTest',
101+
args: expect.arrayContaining([
102+
expect.objectContaining({
103+
name: 'uploadInput'
104+
})
105+
])
88106
})
89107
])
90108
}
91109
}
92110
}
93111
})
94112
})
113+
114+
test('upload completes without any error', async () => {
115+
// setup graphql for integration test
116+
const graphqlServer = createServer(async (req, res) => {
117+
try {
118+
const operation = await processRequest(req, res) as GraphQLOperation
119+
const result = await graphql(createdSchema, operation.query, null, null, operation.variables)
120+
res.end(JSON.stringify(result))
121+
} catch (e) {
122+
console.log(e)
123+
}
124+
})
125+
126+
const { port: graphqlServerPort, close: closeGraphQLServer } = await new Promise((resolve, reject) => {
127+
graphqlServer.listen(function (err) {
128+
if (err) {
129+
return reject(err)
130+
}
131+
132+
return resolve({
133+
port: this.address().port,
134+
close: () => this.close()
135+
})
136+
})
137+
})
138+
139+
const vol = new Volume()
140+
141+
// create mocked in memory file for upload
142+
vol.fromJSON({
143+
'./README.md': '1'
144+
}, '/app')
145+
146+
// prepare request to match graphql multipart request spec
147+
// https://github.com/jaydenseric/graphql-multipart-request-spec
148+
const form = new FormData()
149+
const query = `
150+
mutation FileUploadTest($file: Upload!) {
151+
fileUploadTest(uploadInput: { file: $file }) {
152+
id
153+
url
154+
}
155+
}
156+
`
157+
form.append('operations', JSON.stringify({ query, variables: { file: null } }))
158+
form.append('map', JSON.stringify({ 0: ['variables.file'] }))
159+
form.append('0', vol.createReadStream('/app/README.md'), {
160+
filename: 'readme.md',
161+
filepath: '/app'
162+
})
163+
164+
// @ts-ignore
165+
const uploadResult = await fetch(`http://localhost:${graphqlServerPort}`, { method: 'POST', body: form })
166+
.then(res => res.json())
167+
168+
expect(uploadResult.errors).not.toBeDefined()
169+
expect(uploadResult.data).toBeDefined()
170+
expect(uploadResult.data.fileUploadTest).toBeDefined()
171+
172+
closeGraphQLServer()
173+
})

packages/openapi-to-graphql/test/file_upload_server.js renamed to packages/openapi-to-graphql/test/file_upload_api_server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ function startServer (PORT) {
1313
const bodyParser = require('body-parser')
1414
app.use(bodyParser.json())
1515

16-
app.get('/api/upload', (req, res) => {
17-
res.send({
16+
app.post('/api/upload', (req, res) => {
17+
res.json({
1818
id: '1234567098',
1919
url: 'https://some-random-url.domain/assets/upload-file.ext'
2020
})

packages/openapi-to-graphql/test/fixtures/file_upload.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"paths": {
2222
"/upload": {
2323
"post": {
24+
"x-graphql-field-name": "fileUploadTest",
2425
"requestBody": {
2526
"content": {
2627
"multipart/form-data": {
@@ -70,4 +71,4 @@
7071
}
7172
}
7273
}
73-
}
74+
}

0 commit comments

Comments
 (0)