Gathering detailed insights and metrics for moleculer-apollo-server
Gathering detailed insights and metrics for moleculer-apollo-server
Gathering detailed insights and metrics for moleculer-apollo-server
Gathering detailed insights and metrics for moleculer-apollo-server
@steedos/moleculer-apollo-server
Apollo GraphQL server for Moleculer API Gateway
@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
npm install moleculer-apollo-server
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
100 Stars
181 Commits
55 Forks
7 Watchers
2 Branches
19 Contributors
Updated on May 21, 2024
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
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
10
Apollo GraphQL server mixin for Moleculer API Gateway
npm i moleculer-apollo-server moleculer-web graphql
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}
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};
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...
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.
npm run dev
npm run dev upload
npm run dev full
DATALOADER
environment variable to "true"
npm run dev full
$ npm test
In development with watching
$ npm run ci
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.
The project is available under the MIT license.
Copyright (c) 2020 MoleculerJS
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 5/22 approved changesets -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
24 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
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 More