npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ltv/moleculer-apollo-server-mixin

v0.1.30

Published

An Apollo Server v4 mixin for moleculerjs. Schema first & compatible with GraphQL codegen

Downloads

31

Readme

moleculer-apollo-server-mixin

An Apollo Server v4 mixin for moleculerjs. Schema first & compatible with GraphQL codegen

Features

  • GraphQL Schema first
  • Apollo Server v4
  • Compatible with GraphQL Codegen
  • DataLoader
  • Tracing
  • Metrics

The inspiration

Generally, you can create your standalone Apollo Server and use it as a supgraphql server, btw, you have to do manything if you want to approach the micro-services architecture.

Luckily, we have moleculerjs which is very comprehensive framework for developing a micro-services system. Basically, the moleculerjs service provide the moleculer-web as a HTTP Gateway for publishing RESTFul APIs.

If you want to support the GraphQL, you have to use moleculer-apollo-server (a mixin developed by Icebob the moleculerjs author.). The problem is, this mixin is not well support typescript, and separated schema definition.

The approach of moleculer-apollo-server is using action as a graphql resolver, and using action configuration to define the GraphQL schema.

It looks like:

const schema = {
    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}`;
            }
        }
    }
}

The drawbacks of this approach:

  • You have no good insight about your schema definition, Your type, your schema are locating scattered whenever in your service schema. It will be easy for first sight, but will be difficult for maintenance.
  • You have no typing. Actually, it's not really, you can able to define the ctx type again. Check the below examples
graphql: {
    mutation: "welcome(name: String!): String"
},
handler(ctx: Context<{ name: string }>) {
    return `Hello ${ctx.params.name}`
}

Lets see. You already defined the GraphQL schema then it will not make sense when you have to define the type again. In the example, there is only one name param, and there is no issue with it. But, if you have many params, You need to define it again many time, and ..., I bet you get the point now.

  • So if you're working with javascript? Probably, you can guide your IDE with the javascript docs but you can not reuse it somewhere, and this is unsafe typings.

Finally, I decided to write this mixin to overcome the typings issue and approach with schema first.

Examples

  • schema definition (./graphql/user.schema)
import { gql } from '@ltv/moleculer-apollo-server-mixin';

export const typeDefs = gql`
  extend type Query {
    me: User!
    user(id: ID!): User
    allUsers: [User]
  }

  enum Role {
    USER
    ADMIN
  }

  type User implements Node {
    id: ID!
    username: String!
    email: String!
    role: Role!
    createdAt: DateTime!
  }
`;
  • service schema (services/user/user.svc.ts)
import {
  GraphQLActionSchema,
  ServiceSettings,
  User,
} from '@shared/graphql/generated';
import { print } from 'graphql';
import { typeDefs } from './graphql/user.schema';

const users: User[] = [
  { id: '1', username: 'user1' } as User,
  { id: '2', username: 'user2' } as User,
];

const name = 'user';

type UserActionSchema = {
  resolveUsername: GraphQLActionSchema<'User'>['username'];
};

const actions: GraphQLActionSchema<'Query' | 'User'> & UserActionSchema = {
  allUsers: {
    handler: async () => {
      return users;
    },
  },
  user: {
    params: {
      id: 'string',
    },
    handler: async (ctx) => {
      const { id } = ctx.params;
      return users.find((user) => user.id === id);
    },
  },
  resolveUsername: {
    handler: async (ctx) => {
      const { username } = ctx.params.$parent
      return `transformed ${username}`;
    },
  },
};

const settings: ServiceSettings<typeof name, keyof typeof actions> = {
  typeDefs: print(typeDefs),
  resolvers: {
    Query: {
      allUsers: 'user.allUsers',
      user: 'user.user',
    },
    User: {
      username: 'user.resolveUsername',
    },
  },
};

export default { name, settings, actions };