-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform Uploads to Streams before calling action (#71)
Update the upload feature to transform object resolved from GraphQL upload to a stream before calling the resolution action. This allows uploads to work properly across the transporter.
- Loading branch information
Showing
7 changed files
with
332 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
"use strict"; | ||
|
||
const { ServiceBroker } = require("moleculer"); | ||
|
||
const ApiGateway = require("moleculer-web"); | ||
const { ApolloService, GraphQLUpload } = require("../../index"); | ||
|
||
const broker = new ServiceBroker({ logLevel: "info", hotReload: true }); | ||
|
||
broker.createService({ | ||
name: "api", | ||
|
||
mixins: [ | ||
// Gateway | ||
ApiGateway, | ||
|
||
// GraphQL Apollo Server | ||
ApolloService({ | ||
typeDefs: ["scalar Upload"], | ||
resolvers: { | ||
Upload: GraphQLUpload, | ||
}, | ||
// API Gateway route options | ||
routeOptions: { | ||
path: "/graphql", | ||
cors: true, | ||
mappingPolicy: "restrict", | ||
}, | ||
|
||
// https://www.apollographql.com/docs/apollo-server/v2/api/apollo-server.html | ||
serverOptions: {}, | ||
}), | ||
], | ||
|
||
events: { | ||
"graphql.schema.updated"({ schema }) { | ||
this.logger.info("Generated GraphQL schema:\n\n" + schema); | ||
}, | ||
}, | ||
}); | ||
|
||
broker.createService({ | ||
name: "files", | ||
settings: { | ||
graphql: { | ||
type: ` | ||
""" | ||
This type describes a File entity. | ||
""" | ||
type File { | ||
filename: String! | ||
encoding: String! | ||
mimetype: String! | ||
} | ||
`, | ||
}, | ||
}, | ||
actions: { | ||
hello: { | ||
graphql: { | ||
query: "hello: String!", | ||
}, | ||
handler() { | ||
return "Hello Moleculer!"; | ||
}, | ||
}, | ||
singleUpload: { | ||
graphql: { | ||
mutation: "singleUpload(file: Upload!): File!", | ||
fileUploadArg: "file", | ||
}, | ||
async handler(ctx) { | ||
const fileChunks = []; | ||
for await (const chunk of ctx.params) { | ||
fileChunks.push(chunk); | ||
} | ||
const fileContents = Buffer.concat(fileChunks); | ||
ctx.broker.logger.info("Uploaded File Contents:"); | ||
ctx.broker.logger.info(fileContents.toString()); | ||
return ctx.meta.$fileInfo; | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
broker.start().then(async () => { | ||
broker.repl(); | ||
|
||
broker.logger.info("----------------------------------------------------------"); | ||
broker.logger.info("For information about creating a file upload request,"); | ||
broker.logger.info( | ||
"see https://github.com/jaydenseric/graphql-multipart-request-spec#curl-request" | ||
); | ||
broker.logger.info("----------------------------------------------------------"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.