Apollo GraphQL Server for Moleculer

0.4.1 · active · verified Sun Apr 19

moleculer-apollo-server provides an Apollo GraphQL server as a mixin for the Moleculer API Gateway, enabling easy integration of GraphQL endpoints within a Moleculer microservices architecture. It allows developers to define GraphQL queries and mutations directly within Moleculer service actions, automatically generating a unified GraphQL schema. The current stable version is 0.4.1, released recently with updates to Moleculer 0.15.0 and Apollo Server 5. This package differentiates itself by tightly coupling GraphQL schema definition with Moleculer actions, streamlining the process of exposing microservice capabilities via GraphQL. It also offers advanced features like inter-service resolvers and subscription support. The release cadence appears to be moderate, with significant breaking changes typically occurring with major Apollo Server version upgrades, as seen in the recent v0.4.0 release.

Common errors

Warnings

Install

Imports

Quickstart

This example sets up a Moleculer API Gateway with `moleculer-apollo-server` to expose GraphQL endpoints, demonstrating basic query and mutation definitions from a Moleculer service.

import ApiGateway from 'moleculer-web';
import { ApolloService } from 'moleculer-apollo-server';

// Basic Greeter service to expose actions via GraphQL
const GreeterService = {
    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}`;
            }
        }
    }
};

// API Gateway service with ApolloServer mixin
export default {
    name: 'api',
    mixins: [
        ApiGateway,
        ApolloService({
            typeDefs: `
                # Extend global types if needed, otherwise leave empty
            `,
            resolvers: {},
            routeOptions: {
                path: '/graphql',
                cors: true,
                mappingPolicy: 'restrict'
            },
            serverOptions: {
                // Apollo Server 5 options go here
                // E.g., include a basic plugin
                plugins: [
                    {
                        async serverWillStart() {
                            console.log('Apollo Server starting up...');
                        }
                    }
                ]
            }
        })
    ],
    // Define settings for the API Gateway itself if necessary
    settings: {
        port: process.env.PORT ?? 3000,
        host: process.env.HOST ?? '0.0.0.0'
    },
    // Add the greeter service to the broker in a real app or import it
    created() {
        this.broker.createService(GreeterService);
    }
};

view raw JSON →