百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

moleculer-apollo-server

> 后端框架
开源

:rocket: 适用于 Moleculer 的 Apollo GraphQL 服务器

99 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

:rocket: 适用于 Moleculer 的 Apollo GraphQL 服务器

moleculer-apollo-server

Apollo GraphQL server 5 mixin for Moleculer API Gateway

Features

Install

bash
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.

…

Start your Moleculer project, open http://localhost:3000/graphql in your browser to run queries using Apollo Sandbox or send GraphQL requests directly to the same URL.

Define queries & mutations in service action definitions

javascript
module.exports = {
    name: "greeter", 

    actions: {
        hello: {
            graphql: {
                query: "hello: String"
            },
            handler(ctx) {
                return "Hello Moleculer!"
            }
        },
        welcome: {
            params: {
                name: "string"
            },
            graphql: {
                mutation: "welcome(name: String!): String"
            },
            handler(ctx) {
                return `Hello ${ctx.params.name}`;
            }
        }
    }
};

Generated schema

gql
type Mutation {
  welcome(name: String!): String
}

type Query {
  hello: String
}

Resolvers between services

posts.service.js

…

users.service.js

…

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:

…

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'.

javascript
resolve: {
    params: {
        id: [{ type: "number" }, { type: "array", items: "number" }],
        graphql: { dataLoaderOptions: { maxBatchSize: 100 } },
    },
    handler(ctx) {
        this.logger.debug("resolve action called.", { params: ctx.params });
        if (Array.isArray(ctx.params.id)) {
            return _.cloneDeep(ctx.params.id.map(id => this.findByID(id)));
        } else {
            return _.cloneDeep(this.findByID(ctx.params.id));
        }
    },
},

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
  • Full
    • npm run dev full
  • Full With Dataloader
    • set DATALOADER environment variable to "true"
    • npm run dev full
  • Typescript
    • npm run test:ts

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) 2026 MoleculerJS

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptapollo-servergraphql-serverhacktoberfestmoleculer

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架