Gathering detailed insights and metrics for @ltv/moleculer-apollo-server-mixin
Gathering detailed insights and metrics for @ltv/moleculer-apollo-server-mixin
Gathering detailed insights and metrics for @ltv/moleculer-apollo-server-mixin
Gathering detailed insights and metrics for @ltv/moleculer-apollo-server-mixin
npm install @ltv/moleculer-apollo-server-mixin
Typescript
Module System
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
An Apollo Server v4 mixin for moleculerjs. Schema first & compatible with GraphQL codegen
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:
1const schema = { 2 actions: { 3 hello: { 4 graphql: { 5 query: "hello: String" 6 }, 7 handler(ctx) { 8 return "Hello Moleculer!" 9 } 10 }, 11 welcome: { 12 params: { 13 name: "string" 14 }, 15 graphql: { 16 mutation: "welcome(name: String!): String" 17 }, 18 handler(ctx) { 19 return `Hello ${ctx.params.name}`; 20 } 21 } 22 } 23}
The drawbacks of this approach:
ctx
type again. Check the below examples1graphql: { 2 mutation: "welcome(name: String!): String" 3}, 4handler(ctx: Context<{ name: string }>) { 5 return `Hello ${ctx.params.name}` 6}
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.
Finally, I decided to write this mixin to overcome the typings issue and approach with schema first.
./graphql/user.schema
)1import { gql } from '@ltv/moleculer-apollo-server-mixin'; 2 3export const typeDefs = gql` 4 extend type Query { 5 me: User! 6 user(id: ID!): User 7 allUsers: [User] 8 } 9 10 enum Role { 11 USER 12 ADMIN 13 } 14 15 type User implements Node { 16 id: ID! 17 username: String! 18 email: String! 19 role: Role! 20 createdAt: DateTime! 21 } 22`;
services/user/user.svc.ts
)1import { 2 GraphQLActionSchema, 3 ServiceSettings, 4 User, 5} from '@shared/graphql/generated'; 6import { print } from 'graphql'; 7import { typeDefs } from './graphql/user.schema'; 8 9const users: User[] = [ 10 { id: '1', username: 'user1' } as User, 11 { id: '2', username: 'user2' } as User, 12]; 13 14const name = 'user'; 15 16type UserActionSchema = { 17 resolveUsername: GraphQLActionSchema<'User'>['username']; 18}; 19 20const actions: GraphQLActionSchema<'Query' | 'User'> & UserActionSchema = { 21 allUsers: { 22 handler: async () => { 23 return users; 24 }, 25 }, 26 user: { 27 params: { 28 id: 'string', 29 }, 30 handler: async (ctx) => { 31 const { id } = ctx.params; 32 return users.find((user) => user.id === id); 33 }, 34 }, 35 resolveUsername: { 36 handler: async (ctx) => { 37 const { username } = ctx.params.$parent 38 return `transformed ${username}`; 39 }, 40 }, 41}; 42 43const settings: ServiceSettings<typeof name, keyof typeof actions> = { 44 typeDefs: print(typeDefs), 45 resolvers: { 46 Query: { 47 allUsers: 'user.allUsers', 48 user: 'user.user', 49 }, 50 User: { 51 username: 'user.resolveUsername', 52 }, 53 }, 54}; 55 56export default { name, settings, actions };
No vulnerabilities found.
No security vulnerabilities found.