Apollo GraphQL Server for Moleculer
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
-
Error: ApolloServer requires options to be passed to its constructor
cause Attempting to instantiate ApolloService without `serverOptions` or with an incorrect structure after Apollo Server 5 migration.fixEnsure `serverOptions` is correctly defined within the `ApolloService` mixin configuration, adhering to the Apollo Server 5 constructor options. For example, `serverOptions: { plugins: [...] }`. -
TypeError: Cannot read properties of undefined (reading 'schema')
cause Likely a mismatch between `graphql` peer dependency versions or an issue with schema generation/loading within `moleculer-apollo-server`.fixVerify that `graphql` is installed and meets the `^16.0.0` peer dependency requirement. Rebuild the project if using TypeScript, and ensure `typeDefs` and `resolvers` are valid GraphQL SDL and resolver maps. -
Error: You must install 'moleculer-web' package! npm i moleculer-web
cause `moleculer-web` is a common companion for `moleculer-apollo-server` but is not a hard dependency of the `moleculer-apollo-server` package itself, thus must be installed separately.fixInstall the `moleculer-web` package: `npm install moleculer-web` or `yarn add moleculer-web`. Ensure it is added to your project's dependencies. -
SyntaxError: Unexpected token 'export' (at ...)
cause Attempting to run an ES module-style project (with `import/export`) in a Node.js environment configured for CommonJS, or vice versa.fixEnsure your `package.json` specifies `"type": "module"` for ES Modules, or use CommonJS `require()` syntax if not. Also, verify your `tsconfig.json` `module` option is appropriate (e.g., `"module": "Node16"` or `"ESNext"`).
Warnings
- breaking Version 0.4.0 significantly upgraded Apollo Server from v2 to v5, necessitating corresponding changes in `typeDefs`, `resolvers`, and `serverOptions`. Users migrating from older versions must review Apollo Server's migration guides for v3, v4, and v5.
- breaking Beginning with v0.4.0, Node.js version >= 20.x.x is required. Older Node.js runtimes are no longer supported due to dependencies on features available in Node.js 20.
- breaking GraphQL file upload support was removed in v0.4.0, as Apollo Server v3+ no longer provides built-in support for it. If your application relies on file uploads, you will need to implement a separate solution.
- breaking The built-in healthcheck endpoint was removed in v0.4.0, mirroring changes in Apollo Server v4+. Applications depending on this endpoint for liveness/readiness probes will need an alternative.
- gotcha The peer dependency for `moleculer` was updated to `^0.14.0 || ^0.15.0` in v0.4.1. Ensure your `moleculer` version aligns with this, especially if you are using an older `moleculer` version, which might lead to unexpected behavior or runtime errors.
Install
-
npm install moleculer-apollo-server -
yarn add moleculer-apollo-server -
pnpm add moleculer-apollo-server
Imports
- ApolloService
const { ApolloService } = require('moleculer-apollo-server');import { ApolloService } from 'moleculer-apollo-server'; - GraphQLContext
import { GraphQLContext } from 'moleculer-apollo-server'; - ApolloServiceOptions
import { ApolloServiceOptions } from 'moleculer-apollo-server';
Quickstart
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);
}
};