@@ -83,59 +83,3 @@ If your application truly requires file uploads through GraphQL, proceed with ca
8383- Ensure that streams are always terminated when the request ends, whether or not they were consumed.
8484- Apply strict request size limits and validate all fields.
8585- Treat file names, types, and contents as untrusted data.
86-
87- ## Example (not recommended for production)
88-
89- The example below demonstrates how uploads could be wired up using Express, ` graphql-http ` , and busboy.
90- It’s included only to illustrate the mechanics and is not production-ready.
91-
92- <Callout type = " warning" emoji = " ⚠️" >
93- We strongly discourage using this code in production.
94- </Callout >
95-
96- ``` js
97- import express from ' express' ;
98- import busboy from ' busboy' ;
99- import { createHandler } from ' graphql-http/lib/use/express' ;
100- import { schema } from ' ./schema.js' ;
101-
102- const app = express ();
103-
104- app .post (' /graphql' , (req , res , next ) => {
105- const contentType = req .headers [' content-type' ] || ' ' ;
106-
107- if (contentType .startsWith (' multipart/form-data' )) {
108- const bb = busboy ({ headers: req .headers });
109- let operations, map;
110- const files = {};
111-
112- bb .on (' field' , (name , val ) => {
113- if (name === ' operations' ) operations = JSON .parse (val);
114- else if (name === ' map' ) map = JSON .parse (val);
115- });
116-
117- bb .on (' file' , (fieldname , file , { filename, mimeType }) => {
118- files[fieldname] = { file, filename, mimeType };
119- });
120-
121- bb .on (' close' , () => {
122- for (const [key , paths ] of Object .entries (map)) {
123- for (const path of paths) {
124- const keys = path .split (' .' );
125- let target = operations;
126- while (keys .length > 1 ) target = target[keys .shift ()];
127- target[keys[0 ]] = files[key].file ;
128- }
129- }
130- req .body = operations;
131- next ();
132- });
133-
134- req .pipe (bb);
135- } else {
136- next ();
137- }
138- }, createHandler ({ schema }));
139-
140- app .listen (4000 );
141- ```
0 commit comments