@@ -4,8 +4,13 @@ import { afterAll, beforeAll, expect, test } from '@jest/globals'
4
4
import * as openAPIToGraphQL from '../src/index'
5
5
import * as Oas3Tools from '../src/oas_3_tools'
6
6
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'
8
11
import { graphql } from 'graphql'
12
+ import { GraphQLOperation , processRequest } from 'graphql-upload'
13
+ import { startServer as startAPIServer , stopServer as stopAPIServer } from './file_upload_api_server'
9
14
10
15
/**
11
16
* Set up the schema first
@@ -21,14 +26,14 @@ let createdSchema
21
26
beforeAll ( async ( ) => {
22
27
const [ { schema } ] = await Promise . all ( [
23
28
openAPIToGraphQL . createGraphQLSchema ( oas ) ,
24
- startServer ( PORT )
29
+ startAPIServer ( PORT )
25
30
] )
26
31
27
32
createdSchema = schema
28
33
} )
29
34
30
35
afterAll ( async ( ) => {
31
- await stopServer ( )
36
+ await stopAPIServer ( )
32
37
} )
33
38
34
39
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
68
73
mutationType {
69
74
fields {
70
75
name
76
+ args {
77
+ name
78
+ type {
79
+ name
80
+ kind
81
+ }
82
+ }
71
83
type {
72
84
name
73
85
kind
@@ -78,17 +90,84 @@ test('introspection for mutations returns a mutation matching the custom field s
78
90
}`
79
91
80
92
const result = await graphql ( createdSchema , query )
93
+
81
94
expect ( result ) . toEqual ( {
82
95
data : {
83
96
__schema : {
84
97
mutationType : {
85
98
fields : expect . arrayContaining ( [
86
99
expect . objectContaining ( {
87
- name : 'fileUploadTest'
100
+ name : 'fileUploadTest' ,
101
+ args : expect . arrayContaining ( [
102
+ expect . objectContaining ( {
103
+ name : 'uploadInput'
104
+ } )
105
+ ] )
88
106
} )
89
107
] )
90
108
}
91
109
}
92
110
}
93
111
} )
94
112
} )
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
+ } )
0 commit comments