Skip to content

Commit c22d38b

Browse files
committed
fix: graphql upload for all versions
1 parent 019faf4 commit c22d38b

File tree

7 files changed

+47
-20
lines changed

7 files changed

+47
-20
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/ParseGraphQLServer.spec.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6835,7 +6835,7 @@ describe('ParseGraphQLServer', () => {
68356835

68366836
describe('Files Mutations', () => {
68376837
describe('Create', () => {
6838-
it_only_node_version('<17')('should return File object', async () => {
6838+
it('should return File object', async () => {
68396839
const clientMutationId = uuidv4();
68406840

68416841
parseServer = await global.reconfigureServer({
@@ -9301,7 +9301,7 @@ describe('ParseGraphQLServer', () => {
93019301
expect(result6[0].node.name).toEqual('imACountry3');
93029302
});
93039303

9304-
it_only_node_version('<17')('should support files', async () => {
9304+
it('should support files', async () => {
93059305
try {
93069306
parseServer = await global.reconfigureServer({
93079307
publicServerURL: 'http://localhost:13377/parse',
@@ -9452,7 +9452,9 @@ describe('ParseGraphQLServer', () => {
94529452
body: body2,
94539453
});
94549454
expect(res.status).toEqual(200);
9455-
const result2 = JSON.parse(await res.text());
9455+
const resText = await res.text();
9456+
const result2 = JSON.parse(resText);
9457+
console.log('result2', resText);
94569458
expect(result2.data.createSomeClass1.someClass.someField.name).toEqual(
94579459
jasmine.stringMatching(/_myFileName.txt$/)
94589460
);
@@ -9510,7 +9512,6 @@ describe('ParseGraphQLServer', () => {
95109512
id: result2.data.createSomeClass1.someClass.id,
95119513
},
95129514
});
9513-
95149515
expect(typeof getResult.data.someClass.someField).toEqual('object');
95159516
expect(getResult.data.someClass.someField.name).toEqual(
95169517
result.data.createFile.fileInfo.name
@@ -9549,9 +9550,14 @@ describe('ParseGraphQLServer', () => {
95499550
}
95509551
});
95519552

9552-
it_only_node_version('<17')('should not upload if file is too large', async () => {
9553+
it('should not upload if file is too large', async () => {
95539554
parseGraphQLServer.parseServer.config.maxUploadSize = '1kb';
9554-
9555+
const options = await parseGraphQLServer._getGraphQLOptions();
9556+
expect(new options.fetchApi.Body().options).toEqual({
9557+
formDataLimits: {
9558+
fileSize: 1024,
9559+
},
9560+
});
95559561
const body = new FormData();
95569562
body.append(
95579563
'operations',
@@ -9588,9 +9594,9 @@ describe('ParseGraphQLServer', () => {
95889594
headers,
95899595
body,
95909596
});
9591-
95929597
const result = JSON.parse(await res.text());
9593-
expect(res.status).toEqual(500);
9598+
console.log(result);
9599+
expect(res.status).toEqual(413);
95949600
expect(result.errors[0].message).toEqual('File size limit exceeded: 1024 bytes');
95959601
});
95969602

src/GraphQL/ParseGraphQLServer.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class ParseGraphQLServer {
3232

3333
async _getGraphQLOptions() {
3434
try {
35+
const formDataLimits = {
36+
fileSize: this._transformMaxUploadSizeToBytes(
37+
this.parseServer.config.maxUploadSize || '20mb'
38+
),
39+
};
3540
return {
3641
schema: await this.parseGraphQLSchema.load(),
3742
context: ({ req: { info, config, auth } }) => ({
@@ -40,13 +45,19 @@ class ParseGraphQLServer {
4045
auth,
4146
}),
4247
maskedErrors: false,
48+
// Needed to ensure formDataLimits since it seems to not working
49+
// this is a temporary fix until the issue is resolved
50+
// we need to ask graphql-yoga team
51+
plugins: [
52+
{
53+
onRequestParse: ({ request }) => {
54+
request.options.formDataLimits = formDataLimits;
55+
},
56+
},
57+
],
4358
fetchApi: createFetch({
4459
useNodeFetch: true,
45-
formDataLimits: {
46-
fileSize: this._transformMaxUploadSizeToBytes(
47-
this.parseServer.config.maxUploadSize || '20mb'
48-
),
49-
},
60+
formDataLimits,
5061
}),
5162
};
5263
} catch (e) {
@@ -89,6 +100,7 @@ class ParseGraphQLServer {
89100
app.use(this.config.graphQLPath, handleParseSession);
90101
app.use(this.config.graphQLPath, handleParseErrors);
91102
app.use(this.config.graphQLPath, async (req, res) => {
103+
// console.log("here", req)
92104
const server = await this._getServer();
93105
return server(req, res);
94106
});

src/GraphQL/loaders/filesMutations.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import * as defaultGraphQLTypes from './defaultGraphQLTypes';
55
import logger from '../../logger';
66

77
const handleUpload = async (upload, config) => {
8-
const data = Buffer.from(upload.arrayBuffer ? await upload.arrayBuffer() : upload.blobParts);
8+
const data = await upload.buffer();
99
const fileName = upload.name;
1010
const type = upload.type;
11-
11+
console.log('data.length', data.length);
1212
if (!data || !data.length) {
1313
throw new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Invalid file upload.');
1414
}

src/GraphQL/loaders/parseClassMutations.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const load = function (parseGraphQLSchema, parseClass, parseClassConfig: ?ParseG
8282
const parseFields = await transformTypes('create', fields, {
8383
className,
8484
parseGraphQLSchema,
85+
originalFields: args.fields || {},
8586
req: { config, auth, info },
8687
});
8788

@@ -190,6 +191,7 @@ const load = function (parseGraphQLSchema, parseClass, parseClassConfig: ?ParseG
190191
const parseFields = await transformTypes('update', fields, {
191192
className,
192193
parseGraphQLSchema,
194+
originalFields: args.fields || {},
193195
req: { config, auth, info },
194196
});
195197

src/GraphQL/loaders/usersMutations.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const load = parseGraphQLSchema => {
3838
const parseFields = await transformTypes('create', fields, {
3939
className: '_User',
4040
parseGraphQLSchema,
41+
originalFields: args.fields || {},
4142
req: { config, auth, info },
4243
});
4344

@@ -114,6 +115,7 @@ const load = parseGraphQLSchema => {
114115
const parseFields = await transformTypes('create', fields, {
115116
className: '_User',
116117
parseGraphQLSchema,
118+
originalFields: args.fields || {},
117119
req: { config, auth, info },
118120
});
119121

src/GraphQL/transformers/mutation.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { fromGlobalId } from 'graphql-relay';
33
import { handleUpload } from '../loaders/filesMutations';
44
import * as defaultGraphQLTypes from '../loaders/defaultGraphQLTypes';
55
import * as objectsMutations from '../helpers/objectsMutations';
6+
import deepcopy from 'deepcopy';
67

78
const transformTypes = async (
89
inputType: 'create' | 'update',
910
fields,
10-
{ className, parseGraphQLSchema, req }
11+
{ className, parseGraphQLSchema, req, originalFields }
1112
) => {
1213
const {
1314
classGraphQLCreateType,
@@ -44,7 +45,9 @@ const transformTypes = async (
4445
fields[field] = transformers.polygon(fields[field]);
4546
break;
4647
case inputTypeField.type === defaultGraphQLTypes.FILE_INPUT:
47-
fields[field] = await transformers.file(fields[field], req);
48+
// fields are a deepcopy, but we can't deepcopy a stream so
49+
// we use the original fields from the graphql request
50+
fields[field] = await transformers.file(originalFields[field], req);
4851
break;
4952
case parseClass.fields[field].type === 'Relation':
5053
fields[field] = await transformers.relation(
@@ -152,9 +155,10 @@ const transformers = {
152155
nestedObjectsToAdd = (
153156
await Promise.all(
154157
value.createAndAdd.map(async input => {
155-
const parseFields = await transformTypes('create', input, {
158+
const parseFields = await transformTypes('create', deepcopy(input), {
156159
className: targetClass,
157160
parseGraphQLSchema,
161+
originalFields: input || {},
158162
req: { config, auth, info },
159163
});
160164
return objectsMutations.createObject(targetClass, parseFields, config, auth, info);
@@ -213,9 +217,10 @@ const transformers = {
213217

214218
let nestedObjectToAdd;
215219
if (value.createAndLink) {
216-
const parseFields = await transformTypes('create', value.createAndLink, {
220+
const parseFields = await transformTypes('create', deepcopy(value.createAndLink), {
217221
className: targetClass,
218222
parseGraphQLSchema,
223+
originalFields: value.createAndLink || {},
219224
req: { config, auth, info },
220225
});
221226
nestedObjectToAdd = await objectsMutations.createObject(

0 commit comments

Comments
 (0)