kevinsegal / moleculer-apollo-server

:rocket: [WIP] Apollo GraphQL server for Moleculer API Gateway

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Moleculer logo

Build Status Coverage Status Codacy Badge Code Climate David Known Vulnerabilities Join the chat at https://gitter.im/moleculerjs/moleculer

moleculer-apollo-server NPM version

Apollo GraphQL server mixin for Moleculer API Gateway

Features

Install

npm install moleculer-apollo-server moleculer-web --save

Usage

This example demonstrates how to setup a Moleculer API Gateway with GraphQL mixin, that handles incoming GraphQL requests via the default /graphql endpoint.

"use strict";

const ApiGateway 	= require("moleculer-web");
const { ApolloService } = require("moleculer-apollo-server");

module.exports = {
    name: "api",

    mixins: [
        // Gateway
        ApiGateway,

        // GraphQL Apollo Server
        ApolloService({

            // Global GraphQL typeDefs
            typeDefs: ``,

            // Global resolvers
            resolvers: {},

            // API Gateway route options
            routeOptions: {
                path: "/graphql",
                cors: true,
                mappingPolicy: "restrict"
            },

            // https://www.apollographql.com/docs/apollo-server/v2/api/apollo-server.html
            serverOptions: {
                tracing: true,

                engine: {
                    apiKey: process.env.APOLLO_ENGINE_KEY
                }
            }
        })
    ]
};

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

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

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

type Query {
  hello: String
}

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

@moleculerjs @MoleculerJS

About

:rocket: [WIP] Apollo GraphQL server for Moleculer API Gateway

License:MIT License


Languages

Language:JavaScript 100.0%