Installations
npm install moleculer-apollo-server
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>= 10.x.x
Node Version
18.16.0
NPM Version
9.5.1
Score
42.8
Supply Chain
56.1
Quality
71.1
Maintenance
25
Vulnerability
97
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
validate.email 🚀
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Developer
moleculerjs
Download Statistics
Total Downloads
495,634
Last Day
400
Last Week
1,030
Last Month
5,314
Last Year
90,295
GitHub Statistics
MIT License
100 Stars
181 Commits
55 Forks
7 Watchers
1 Branches
19 Contributors
Updated on May 21, 2024
Bundle Size
3.88 MB
Minified
944.24 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.3.8
Package Id
moleculer-apollo-server@0.3.8
Unpacked Size
50.08 kB
Size
14.49 kB
File Count
14
NPM Version
9.5.1
Node Version
18.16.0
Published on
Apr 23, 2023
Total Downloads
Cumulative downloads
Total Downloads
495,634
Last Day
30.7%
400
Compared to previous day
Last Week
-16.6%
1,030
Compared to previous week
Last Month
16.4%
5,314
Compared to previous month
Last Year
-45.2%
90,295
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
10
moleculer-apollo-server 
Apollo GraphQL server mixin for Moleculer API Gateway
Features
Install
npm i moleculer-apollo-server moleculer-web graphql
Usage
This example demonstrates how to setup a Moleculer API Gateway with GraphQL mixin in order to handle incoming GraphQL requests via the default /graphql
endpoint.
1"use strict"; 2 3const ApiGateway = require("moleculer-web"); 4const { ApolloService } = require("moleculer-apollo-server"); 5 6module.exports = { 7 name: "api", 8 9 mixins: [ 10 // Gateway 11 ApiGateway, 12 13 // GraphQL Apollo Server 14 ApolloService({ 15 16 // Global GraphQL typeDefs 17 typeDefs: ``, 18 19 // Global resolvers 20 resolvers: {}, 21 22 // API Gateway route options 23 routeOptions: { 24 path: "/graphql", 25 cors: true, 26 mappingPolicy: "restrict" 27 }, 28 29 // https://www.apollographql.com/docs/apollo-server/v2/api/apollo-server.html 30 serverOptions: { 31 tracing: true, 32 33 engine: { 34 apiKey: process.env.APOLLO_ENGINE_KEY 35 } 36 } 37 }) 38 ] 39}; 40
Start your Moleculer project, open http://localhost:3000/graphql in your browser to run queries using graphql-playground, or send GraphQL requests directly to the same URL.
Define queries & mutations in service action definitions
1module.exports = { 2 name: "greeter", 3 4 actions: { 5 hello: { 6 graphql: { 7 query: "hello: String" 8 }, 9 handler(ctx) { 10 return "Hello Moleculer!" 11 } 12 }, 13 welcome: { 14 params: { 15 name: "string" 16 }, 17 graphql: { 18 mutation: "welcome(name: String!): String" 19 }, 20 handler(ctx) { 21 return `Hello ${ctx.params.name}`; 22 } 23 } 24 } 25};
Generated schema
1type Mutation { 2 welcome(name: String!): String 3} 4 5type Query { 6 hello: String 7}
Resolvers between services
posts.service.js
1module.exports = { 2 name: "posts", 3 settings: { 4 graphql: { 5 type: ` 6 """ 7 This type describes a post entity. 8 """ 9 type Post { 10 id: Int! 11 title: String! 12 author: User! 13 votes: Int! 14 voters: [User] 15 createdAt: Timestamp 16 } 17 `, 18 resolvers: { 19 Post: { 20 author: { 21 // Call the `users.resolve` action with `id` params 22 action: "users.resolve", 23 rootParams: { 24 "author": "id" 25 } 26 }, 27 voters: { 28 // Call the `users.resolve` action with `id` params 29 action: "users.resolve", 30 rootParams: { 31 "voters": "id" 32 } 33 } 34 } 35 } 36 } 37 }, 38 actions: { 39 find: { 40 //cache: true, 41 params: { 42 limit: { type: "number", optional: true } 43 }, 44 graphql: { 45 query: `posts(limit: Int): [Post]` 46 }, 47 handler(ctx) { 48 let result = _.cloneDeep(posts); 49 if (ctx.params.limit) 50 result = posts.slice(0, ctx.params.limit); 51 else 52 result = posts; 53 54 return _.cloneDeep(result); 55 } 56 }, 57 58 findByUser: { 59 params: { 60 userID: "number" 61 }, 62 handler(ctx) { 63 return _.cloneDeep(posts.filter(post => post.author == ctx.params.userID)); 64 } 65 }, 66 } 67};
users.service.js
1module.exports = { 2 name: "users", 3 settings: { 4 graphql: { 5 type: ` 6 """ 7 This type describes a user entity. 8 """ 9 type User { 10 id: Int! 11 name: String! 12 birthday: Date 13 posts(limit: Int): [Post] 14 postCount: Int 15 } 16 `, 17 resolvers: { 18 User: { 19 posts: { 20 // Call the `posts.findByUser` action with `userID` param. 21 action: "posts.findByUser", 22 rootParams: { 23 "id": "userID" 24 } 25 }, 26 postCount: { 27 // Call the "posts.count" action 28 action: "posts.count", 29 // Get `id` value from `root` and put it into `ctx.params.query.author` 30 rootParams: { 31 "id": "query.author" 32 } 33 } 34 } 35 } 36 } 37 }, 38 actions: { 39 find: { 40 //cache: true, 41 params: { 42 limit: { type: "number", optional: true } 43 }, 44 graphql: { 45 query: "users(limit: Int): [User]" 46 }, 47 handler(ctx) { 48 let result = _.cloneDeep(users); 49 if (ctx.params.limit) 50 result = users.slice(0, ctx.params.limit); 51 else 52 result = users; 53 54 return _.cloneDeep(result); 55 } 56 }, 57 58 resolve: { 59 params: { 60 id: [ 61 { type: "number" }, 62 { type: "array", items: "number" } 63 ] 64 }, 65 handler(ctx) { 66 if (Array.isArray(ctx.params.id)) { 67 return _.cloneDeep(ctx.params.id.map(id => this.findByID(id))); 68 } else { 69 return _.cloneDeep(this.findByID(ctx.params.id)); 70 } 71 } 72 } 73 } 74};
File Uploads
moleculer-apollo-server supports file uploads through the GraphQL multipart request specification.
To enable uploads, the Upload scalar must be added to the Gateway:
1"use strict"; 2 3const ApiGateway = require("moleculer-web"); 4const { ApolloService, GraphQLUpload } = require("moleculer-apollo-server"); 5 6module.exports = { 7 name: "api", 8 9 mixins: [ 10 // Gateway 11 ApiGateway, 12 13 // GraphQL Apollo Server 14 ApolloService({ 15 16 // Global GraphQL typeDefs 17 typeDefs: ["scalar Upload"], 18 19 // Global resolvers 20 resolvers: { 21 Upload: GraphQLUpload 22 }, 23 24 // API Gateway route options 25 routeOptions: { 26 path: "/graphql", 27 cors: true, 28 mappingPolicy: "restrict" 29 }, 30 31 // https://www.apollographql.com/docs/apollo-server/v2/api/apollo-server.html 32 serverOptions: { 33 tracing: true, 34 35 engine: { 36 apiKey: process.env.APOLLO_ENGINE_KEY 37 } 38 } 39 }) 40 ] 41}; 42
Then a mutation can be created which accepts an Upload argument. The fileUploadArg
property must be set to the mutation's argument name so that moleculer-apollo-server knows where to expect a file upload. When the mutation's action handler is called, ctx.params
will be a Readable Stream which can be used to read the contents of the uploaded file (or pipe the contents into a Writable Stream). File metadata will be made available in ctx.meta.$fileInfo
.
files.service.js
1module.exports = { 2 name: "files", 3 settings: { 4 graphql: { 5 type: ` 6 """ 7 This type describes a File entity. 8 """ 9 type File { 10 filename: String! 11 encoding: String! 12 mimetype: String! 13 } 14 ` 15 } 16 }, 17 actions: { 18 uploadFile: { 19 graphql: { 20 mutation: "uploadFile(file: Upload!): File!", 21 fileUploadArg: "file", 22 }, 23 async handler(ctx) { 24 const fileChunks = []; 25 for await (const chunk of ctx.params) { 26 fileChunks.push(chunk); 27 } 28 const fileContents = Buffer.concat(fileChunks); 29 // Do something with file contents 30 31 // Additional arguments: 32 this.logger.info("Additional arguments:", ctx.meta.$args); 33 34 return ctx.meta.$fileInfo; 35 } 36 } 37 } 38};
To accept multiple uploaded files in a single request, the mutation can be changed to accept an array of Upload
s and return an array of results. The action handler will then be called once for each uploaded file, and the results will be combined into an array automatically with results in the same order as the provided files.
1... 2graphql: { 3 mutation: "upload(file: [Upload!]!): [File!]!", 4 fileUploadArg: "file" 5} 6...
Dataloader
moleculer-apollo-server supports DataLoader via configuration in the resolver definition. The called action must be compatible with DataLoader semantics -- that is, it must accept params with an array property and return an array of the same size, with the results in the same order as they were provided.
To activate DataLoader for a resolver, simply add dataLoader: true
to the resolver's property object in the resolvers
property of the service's graphql
property:
1settings: { 2 graphql: { 3 resolvers: { 4 Post: { 5 author: { 6 action: "users.resolve", 7 dataLoader: true, 8 rootParams: { 9 author: "id", 10 }, 11 }, 12 voters: { 13 action: "users.resolve", 14 dataLoader: true, 15 rootParams: { 16 voters: "id", 17 }, 18 }, 19 ...
Since DataLoader only expects a single value to be loaded at a time, only one rootParams
key/value pairing will be utilized, but params
and GraphQL child arguments work properly.
You can also specify options for construction of the DataLoader in the called action definition's graphql
property. This is useful for setting things like `maxBatchSize'.
1resolve: {
2 params: {
3 id: [{ type: "number" }, { type: "array", items: "number" }],
4 graphql: { dataLoaderOptions: { maxBatchSize: 100 } },
5 },
6 handler(ctx) {
7 this.logger.debug("resolve action called.", { params: ctx.params });
8 if (Array.isArray(ctx.params.id)) {
9 return _.cloneDeep(ctx.params.id.map(id => this.findByID(id)));
10 } else {
11 return _.cloneDeep(this.findByID(ctx.params.id));
12 }
13 },
14},
It is unlikely that setting any of the options which accept a function will work properly unless you are running moleculer in a single-node environment. This is because the functions will not serialize and be run by the moleculer-web Api Gateway.
Examples
- Simple
npm run dev
- File Upload
npm run dev upload
- See here for information about how to create a file upload request
- Full
npm run dev full
- Full With Dataloader
- set
DATALOADER
environment variable to"true"
npm run dev full
- set
Test
$ npm test
In development with watching
$ npm run ci
Contribution
Please send pull requests improving the usage and fixing bugs, improving documentation and providing better examples, or providing some testing, because these things are important.
License
The project is available under the MIT license.
Contact
Copyright (c) 2020 MoleculerJS

No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 5/22 approved changesets -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/moleculerjs/moleculer-apollo-server/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/moleculerjs/moleculer-apollo-server/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/moleculerjs/moleculer-apollo-server/ci.yml/master?enable=pin
- Info: 0 out of 3 GitHub-owned GitHubAction dependencies pinned
- Info: 1 out of 1 npmCommand dependencies pinned
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 16 are checked with a SAST tool
Reason
22 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-j5g3-5c8r-7qfx
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-7gc6-qh9x-w6h8
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-wm7h-9275-46v2
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.8
/10
Last Scanned on 2025-03-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to moleculer-apollo-server
@ltv/moleculer-apollo-server-mixin
An Apollo Server v4 mixin for moleculerjs. Schema first & compatible with GraphQL codegen
@latech/moleculer-apollo-server
Apollo GraphQL server for Moleculer API Gateway
@ltv/moleculer-apollo-server
Apollo GraphQL server for Moleculer API Gateway
moleculer-apollo-server-v4
