Skip to content

Commit

Permalink
add $args for file upload to access additional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Sep 8, 2020
1 parent 2984285 commit 0c6c02e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
--------------------------------------------------
<a name="0.3.3"></a>
# 0.3.3 (2020-09-08)

## Changes
- add `ctx.meta.$args` to store additional arguments in case of file uploading.

--------------------------------------------------
<a name="0.3.2"></a>
# 0.3.2 (2020-08-30)
Expand Down
6 changes: 3 additions & 3 deletions examples/upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ broker.createService({
},
singleUpload: {
graphql: {
mutation: "singleUpload(file: Upload!): File!",
mutation: "singleUpload(file: Upload!, other: String): File!",
fileUploadArg: "file",
},
async handler(ctx) {
Expand All @@ -75,8 +75,8 @@ broker.createService({
fileChunks.push(chunk);
}
const fileContents = Buffer.concat(fileChunks);
ctx.broker.logger.info("Uploaded File Contents:");
ctx.broker.logger.info(fileContents.toString());
ctx.broker.logger.info("Uploaded File Contents:", fileContents.toString());
ctx.broker.logger.info("Additional arguments:", ctx.meta.$args);
return ctx.meta.$fileInfo;
},
},
Expand Down
8 changes: 5 additions & 3 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const GraphQL = require("graphql");
const { PubSub, withFilter } = require("graphql-subscriptions");
const hash = require("object-hash");

module.exports = function(mixinOptions) {
module.exports = function (mixinOptions) {
mixinOptions = _.defaultsDeep(mixinOptions, {
routeOptions: {
path: "/graphql",
Expand Down Expand Up @@ -212,6 +212,8 @@ module.exports = function(mixinOptions) {
? await dataLoader.loadMany(dataLoaderKey)
: await dataLoader.load(dataLoaderKey);
} else if (fileUploadArg != null && args[fileUploadArg] != null) {
const additionalArgs = _.omit(args, [fileUploadArg]);

if (Array.isArray(args[fileUploadArg])) {
return await Promise.all(
args[fileUploadArg].map(async uploadPromise => {
Expand All @@ -221,7 +223,7 @@ module.exports = function(mixinOptions) {
} = await uploadPromise;
const stream = createReadStream();
return context.ctx.call(actionName, stream, {
meta: { $fileInfo },
meta: { $fileInfo, $args: additionalArgs },
});
})
);
Expand All @@ -230,7 +232,7 @@ module.exports = function(mixinOptions) {
const { createReadStream, ...$fileInfo } = await args[fileUploadArg];
const stream = createReadStream();
return await context.ctx.call(actionName, stream, {
meta: { $fileInfo },
meta: { $fileInfo, $args: additionalArgs },
});
} else {
const params = {};
Expand Down
7 changes: 5 additions & 2 deletions test/unit/service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ describe("Test Service", () => {
createReadStream: () => "fake read stream",
};

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

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

Expand All @@ -575,6 +575,7 @@ describe("Test Service", () => {
encoding: "7bit",
mimetype: "text/plain",
},
$args: { other: "something" },
},
});
});
Expand Down Expand Up @@ -604,7 +605,7 @@ describe("Test Service", () => {
},
];

const res = await resolver(fakeRoot, { files }, { ctx });
const res = await resolver(fakeRoot, { files, other: "something" }, { ctx });

expect(res).toEqual([
"response for fake read stream 1",
Expand All @@ -619,6 +620,7 @@ describe("Test Service", () => {
encoding: "7bit",
mimetype: "text/plain",
},
$args: { other: "something" },
},
});
expect(ctx.call).toBeCalledWith("posts.uploadMulti", "fake read stream 2", {
Expand All @@ -628,6 +630,7 @@ describe("Test Service", () => {
encoding: "7bit",
mimetype: "text/plain",
},
$args: { other: "something" },
},
});
});
Expand Down

0 comments on commit 0c6c02e

Please sign in to comment.