Skip to content

Commit 0c6c02e

Browse files
committed
add $args for file upload to access additional arguments
1 parent 2984285 commit 0c6c02e

4 files changed

Lines changed: 20 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
--------------------------------------------------
2+
<a name="0.3.3"></a>
3+
# 0.3.3 (2020-09-08)
4+
5+
## Changes
6+
- add `ctx.meta.$args` to store additional arguments in case of file uploading.
7+
18
--------------------------------------------------
29
<a name="0.3.2"></a>
310
# 0.3.2 (2020-08-30)

examples/upload/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ broker.createService({
6666
},
6767
singleUpload: {
6868
graphql: {
69-
mutation: "singleUpload(file: Upload!): File!",
69+
mutation: "singleUpload(file: Upload!, other: String): File!",
7070
fileUploadArg: "file",
7171
},
7272
async handler(ctx) {
@@ -75,8 +75,8 @@ broker.createService({
7575
fileChunks.push(chunk);
7676
}
7777
const fileContents = Buffer.concat(fileChunks);
78-
ctx.broker.logger.info("Uploaded File Contents:");
79-
ctx.broker.logger.info(fileContents.toString());
78+
ctx.broker.logger.info("Uploaded File Contents:", fileContents.toString());
79+
ctx.broker.logger.info("Additional arguments:", ctx.meta.$args);
8080
return ctx.meta.$fileInfo;
8181
},
8282
},

src/service.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const GraphQL = require("graphql");
1515
const { PubSub, withFilter } = require("graphql-subscriptions");
1616
const hash = require("object-hash");
1717

18-
module.exports = function(mixinOptions) {
18+
module.exports = function (mixinOptions) {
1919
mixinOptions = _.defaultsDeep(mixinOptions, {
2020
routeOptions: {
2121
path: "/graphql",
@@ -212,6 +212,8 @@ module.exports = function(mixinOptions) {
212212
? await dataLoader.loadMany(dataLoaderKey)
213213
: await dataLoader.load(dataLoaderKey);
214214
} else if (fileUploadArg != null && args[fileUploadArg] != null) {
215+
const additionalArgs = _.omit(args, [fileUploadArg]);
216+
215217
if (Array.isArray(args[fileUploadArg])) {
216218
return await Promise.all(
217219
args[fileUploadArg].map(async uploadPromise => {
@@ -221,7 +223,7 @@ module.exports = function(mixinOptions) {
221223
} = await uploadPromise;
222224
const stream = createReadStream();
223225
return context.ctx.call(actionName, stream, {
224-
meta: { $fileInfo },
226+
meta: { $fileInfo, $args: additionalArgs },
225227
});
226228
})
227229
);
@@ -230,7 +232,7 @@ module.exports = function(mixinOptions) {
230232
const { createReadStream, ...$fileInfo } = await args[fileUploadArg];
231233
const stream = createReadStream();
232234
return await context.ctx.call(actionName, stream, {
233-
meta: { $fileInfo },
235+
meta: { $fileInfo, $args: additionalArgs },
234236
});
235237
} else {
236238
const params = {};

test/unit/service.spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ describe("Test Service", () => {
563563
createReadStream: () => "fake read stream",
564564
};
565565

566-
const res = await resolver(fakeRoot, { file }, { ctx });
566+
const res = await resolver(fakeRoot, { file, other: "something" }, { ctx });
567567

568568
expect(res).toBe("response from action");
569569

@@ -575,6 +575,7 @@ describe("Test Service", () => {
575575
encoding: "7bit",
576576
mimetype: "text/plain",
577577
},
578+
$args: { other: "something" },
578579
},
579580
});
580581
});
@@ -604,7 +605,7 @@ describe("Test Service", () => {
604605
},
605606
];
606607

607-
const res = await resolver(fakeRoot, { files }, { ctx });
608+
const res = await resolver(fakeRoot, { files, other: "something" }, { ctx });
608609

609610
expect(res).toEqual([
610611
"response for fake read stream 1",
@@ -619,6 +620,7 @@ describe("Test Service", () => {
619620
encoding: "7bit",
620621
mimetype: "text/plain",
621622
},
623+
$args: { other: "something" },
622624
},
623625
});
624626
expect(ctx.call).toBeCalledWith("posts.uploadMulti", "fake read stream 2", {
@@ -628,6 +630,7 @@ describe("Test Service", () => {
628630
encoding: "7bit",
629631
mimetype: "text/plain",
630632
},
633+
$args: { other: "something" },
631634
},
632635
});
633636
});

0 commit comments

Comments
 (0)