-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can type-graphql be used with this? #11
Comments
I don't think it can in the near feature |
Hey @duongdev is there any progress on this Issue? Is it somehow possible to use |
@ujwal-setlur mentioned at Discord that he's using TypeGraphQL. Maybe you can ask him |
Yes, I am using So, what I have is a moleculer service acting as an API gateway using standard
// TypeGraphQL
import 'reflect-metadata';
import { buildSchemaSync } from 'type-graphql';
// Import our GraphQL API piece by piece
// Each module here returns a TypeGraphQL resolver
// Our stuff
import { UserResolver } from './user/index';
type NonEmptyArray<TItem> = [TItem, ...TItem[]];
// Our array of resolvers
const resolvers: NonEmptyArray<Function> = [UserResolver];
// Build our schema
const schema = buildSchemaSync({ validate: false, resolvers });
// Export our schema!
export default schema;
import moleculer from 'moleculer';
import { Action, Service } from 'moleculer-decorators';
import { ApolloServer } from 'apollo-server';
// Our stuff
import schema from './graphql/index'; // <-- schema built by TypeGraphQL
// Define our api service
@Service({
name: 'api'
})
export class ApiService extends moleculer.Service {
server!: ApolloServer;
// Startup handler
async started() {
const moleculerBroker = this.broker;
// Create our Apollo GraphQL server here
const server = new ApolloServer({
schema,
context: () => ({
moleculerBroker
})
});
this.server = server;
// Check if port is set, otherwise default to 3000
const port = process.env.PORT || 3000;
// Start our server
await server.listen({ port }).then(({ url }) => {
this.broker.logger.info(`🚀 Apollo Server ready at ${url}`);
});
}
// Stopped handler
stopped() {
this.server.stop();
}
} Hope this was helpful. Happy to answer any questions. |
@ujwal-setlur thank you so much. It actually solves my problem as well. Thanks to you I realized I need to use neither |
@ujwal-setlur I did exactly as you stated above and I ended up with |
@teezzan I'll need to see some code snippets... |
Thanks for your response. My laptop is dead right now. I will share the snippets once it's up. |
@ujwal-setlur Here is the code I am using currently. My aim to to get the server up and running before starting to implement the schema. The versions of some libraries are as follows.
import 'reflect-metadata';
import { Field, ObjectType, Resolver, Query, Arg } from 'type-graphql';
@ObjectType({ description: "Object representing test" })
class Recipe {
@Field()
title: string;
}
@Resolver(Recipe)
export class RecipeResolver {
constructor() { }
@Query(returns => String)
async recipe() {
return "Hello recipe";
}
}
// TypeGraphQL
import 'reflect-metadata';
import { buildSchemaSync } from 'type-graphql';
// Import our GraphQL API piece by piece
// Each module here returns a TypeGraphQL resolver
// Our stuff
import { RecipeResolver } from './recipe/index';
type NonEmptyArray<TItem> = [TItem, ...TItem[]];
// Our array of resolvers
const resolvers: NonEmptyArray<Function> = [RecipeResolver];
// Build our schema
const schema = buildSchemaSync({ validate: false, resolvers });
// Export our schema!
export default schema;
import moleculer from 'moleculer';
import { Action, Service } from 'moleculer-decorators';
import { ApolloServer } from 'apollo-server';
// Our stuff
import schema from '../graphql/index'; // <-- schema built by TypeGraphQL
// Define our api service
@Service({
name: 'api'
})
export class ApiService extends moleculer.Service {
server!: ApolloServer;
// Startup handler
async started() {
const moleculerBroker = this.broker;
// Create our Apollo GraphQL server here
const server = new ApolloServer({
schema,
context: () => ({
moleculerBroker
})
});
this.server = server;
// Check if port is set, otherwise default to 3000
const port = process.env.PORT || 3000;
// Start our server
await server.listen({ port }).then(({ url }) => {
this.broker.logger.info(`🚀 Apollo Server ready at ${url}`);
});
}
// Stopped handler
stopped() {
this.server.stop();
}
} The complete error log is as follows. [2020-11-14T16:28:11.965Z] INFO gal1l30-inspiron-3521-6867/BROKER: Registered 14 internal middleware(s).
Service name can't be empty! Maybe it is not a valid Service schema. Maybe is it not a service schema? { schema: { ApiService: [Function] } }
[2020-11-14T16:28:19.792Z] ERROR gal1l30-inspiron-3521-6867/BROKER: Failed to load service '/home/gal3li0/Documents/Apis/bstech/LH/services/api.service.ts' ServiceSchemaError: Service name can't be empty! Maybe it is not a valid Service schema. Maybe is it not a service schema?
at Service.parseServiceSchema (/home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/service.js:92:10)
at new Service (/home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/service.js:63:9)
at ServiceBroker.createService (/home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/service-broker.js:805:14)
at ServiceBroker.loadService (/home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/service-broker.js:770:16)
at /home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/runner.js:382:50
at Array.forEach (<anonymous>)
at MoleculerRunner.loadServices (/home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/runner.js:382:25)
at MoleculerRunner.startBroker (/home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/runner.js:453:8)
at /home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/runner.js:475:21 {
code: 500,
type: 'SERVICE_SCHEMA_ERROR',
data: { schema: { ApiService: [Function] } },
retryable: false
}
[Runner] Service name can't be empty! Maybe it is not a valid Service schema. Maybe is it not a service schema? ServiceSchemaError: Service name can't be empty! Maybe it is not a valid Service schema. Maybe is it not a service schema?
at Service.parseServiceSchema (/home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/service.js:92:10)
at new Service (/home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/service.js:63:9)
at ServiceBroker.createService (/home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/service-broker.js:805:14)
at ServiceBroker.loadService (/home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/service-broker.js:770:16)
at /home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/runner.js:382:50
at Array.forEach (<anonymous>)
at MoleculerRunner.loadServices (/home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/runner.js:382:25)
at MoleculerRunner.startBroker (/home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/runner.js:453:8)
at /home/gal3li0/Documents/Apis/bstech/LH/node_modules/moleculer/src/runner.js:475:21 {
code: 500,
type: 'SERVICE_SCHEMA_ERROR',
data: { schema: { ApiService: [Function] } },
retryable: false
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: `ts-node ./node_modules/moleculer/bin/moleculer-runner.js --hot --repl --env --config moleculer.config.ts services/**/*.service.ts`
npm ERR! Exit status 1
npm ERR! src
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/gal3li0/.npm/_logs/2020-11-14T16_28_19_888Z-debug.log Thanks |
Thanks @ujwal-setlur and @progresak . I solved it by looking through @progresak project. I needed to add a Thanks. |
Does anyone have any idea on how to get typegraphql authorization working? I figured it would require extracting the authorization headers from |
Solved... Apollo Server context to the rescue. Thanks |
Yep
…--ujwal
On Nov 15, 2020, 4:16 PM -0800, Yusuf Taiwo Hassan ***@***.***>, wrote:
Solved... Apollo Server context to the rescue.
https://www.apollographql.com/docs/apollo-server/security/authentication/
Thanks
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Thanks for this thread and it kind of gives options for us to similar issue we are aiming to resolve. Flow-2 --> GQL Reqeust --> API GW with Apollo server --> Resolver ( here resolver retrieves the broker from ctx passed down from Apolloserver in Api GW Service) -->ctx.broker.call (Moleculer Service (non GraphQL Aware))-->Repository --> DB? Truly appreciate if we can see some samples for this please |
@ujwal-setlur and @teezzan we managed to get it working by using "Flow-2" approach as per my message above and I presume you also must have used Flow-2. If by any chance you got it working through Flow-1 then appreciate if you can share some code snippet pls. |
@iasbm I am not quite clear on Flow-1, but my implementation is similar to Flow-2: @Resolver()
export class PingResolver {
@Query(() => String)
async Ping(@Ctx() ctx: ResolverContext): Promise<String> {
const { moleculerBroker } = ctx;
const response = moleculerBroker.call('authz.ping');
return response;
}
} |
thanks @ujwal-setlur for confirming this. Your confirmation aligns with our implementation of Flow-2, cheers |
Hey
is there a way i can use this with type-graphql?
https://github.com/19majkel94/type-graphql
The text was updated successfully, but these errors were encountered: