Gathering detailed insights and metrics for @ltv/moleculer-apollo-server
Gathering detailed insights and metrics for @ltv/moleculer-apollo-server
npm install @ltv/moleculer-apollo-server
Typescript
Module System
Min. Node Version
Node Version
NPM Version
48.7
Supply Chain
82.8
Quality
69.1
Maintenance
50
Vulnerability
97
License
JavaScript (100%)
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
5,433
Last Day
1
Last Week
2
Last Month
13
Last Year
413
MIT License
100 Stars
181 Commits
55 Forks
7 Watchers
1 Branches
19 Contributors
Updated on May 21, 2024
Minified
Minified + Gzipped
Latest Version
0.1.25
Package Id
@ltv/moleculer-apollo-server@0.1.25
Unpacked Size
42.14 kB
Size
12.83 kB
File Count
16
NPM Version
6.9.0
Node Version
10.16.1
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
0%
2
Compared to previous week
Last Month
-85.2%
13
Compared to previous month
Last Year
-43.3%
413
Compared to previous year
10
Apollo GraphQL server mixin for Moleculer API Gateway
npm i moleculer-apollo-server moleculer-web@next 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 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 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) 2019 MoleculerJS
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
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
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
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
23 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-03-10
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