Skip to content
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

fix graphql #4

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions converter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const toGqlTypes = require('./types.js');
const { recursiveGetPackage, replacePackageName } = require('../libs/tools.js');

let tmplQuery =
`type Query {
`extend type Query {
{{FnDesc}}
}\n`;

let tmplMutation =
`type Mutation {
`extend type Mutation {
{{FnDesc}}
}`;

Expand Down
26 changes: 26 additions & 0 deletions examples/helloworld-alt/controllers/graphql/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const Message = require('../../models/message.js');

var fakeDatabase = {};

const Resolvers = {
Query: {
getMessage: (root, {id}, context) => {
if (!fakeDatabase[id]) { throw new Error('no message exists with id ' + id); }
return new Message(id, fakeDatabase[id]);
},
},
Mutation: {
createMessage: (root, {input}, context) => {
var id = require('crypto').randomBytes(10).toString('hex');
fakeDatabase[id] = input;
return new Message(id, input);
},
updateMessage: (root, {id, input}, context) => {
if (!fakeDatabase[id]) { throw new Error('no message exists with id ' + id); }
fakeDatabase[id] = input;
return new Message(id, input);
},
},
};

module.exports = Resolvers;
10 changes: 8 additions & 2 deletions examples/helloworld-alt/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path');
const app = require('express')();
const { RPCServer } = require('../../index.js');
const Hello = require('./controllers/helloworld.js');
Expand All @@ -8,8 +9,13 @@ const methods = {

const rpcServer = new RPCServer({
port: 50052, // set gRPC port on 50052
graphql: true,
protoFile: __dirname + '/../protos/hello.proto',
graphql: {
enable: true,
// schemaPath: path.join(__dirname, './schema'),
// controllerPath: path.join(__dirname, './controllers/graphql'),
// auto: false,
},
protoFile: path.join(__dirname, '../protos/hello.proto'),
packages: {
helloworld: {
Greeter: {
Expand Down
9 changes: 9 additions & 0 deletions examples/helloworld-alt/models/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Message {
constructor(id, {content, author}) {
this.id = id;
this.content = content;
this.author = author;
}
}

module.exports = Message;
23 changes: 23 additions & 0 deletions examples/helloworld-alt/schema/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const typeDefs = `
input MessageInput {
content: String
author: String
}

type Message {
id: ID!
content: String
author: String
}

extend type Query {
getMessage(id: ID!): Message
}

extend type Mutation {
createMessage(input: MessageInput): Message
updateMessage(id: ID!, input: MessageInput): Message
}
`;

module.exports = typeDefs;
60 changes: 45 additions & 15 deletions libs/rpc-server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const grpc = require('grpc');
const EventEmitter = require('events').EventEmitter;
const { ApolloServer, gql } = require('apollo-server-express');
const { ApolloServer, makeExecutableSchema, gql } = require('apollo-server-express');
const RPCService = require('./rpc-service.js');
const { genResolvers } = require('./tools.js');
const { genResolvers, readDir } = require('./tools.js');

class RPCServer extends EventEmitter {
/**
Expand All @@ -26,21 +26,51 @@ class RPCServer extends EventEmitter {
console.log('gRPC Server started %s:%d', ip, port);

// GraphQL server is not running by default. Set `graphql` to enabled.
if (graphql !== true) return this;
// Construct a schema, using GraphQL schema language from
// protobuf to GraphQL converter
const gqlSchema = this.rpcService.gqlSchema;

if (!gqlSchema) {
console.warn('GraphQL Server start failed due to missing schema.');
return this;
if (!graphql || !graphql.enable) return this;

const { schemaPath, controllerPath } = graphql;

const rootTypeDefs = `
type Query{
_: String
}
type Mutation {
_: String
}
`;

let auto = (graphql.auto !== undefined) ? graphql.auto: true;
let registerTypes = [ rootTypeDefs ];
let registerResolvers = [];

if (schemaPath && controllerPath) {
const schemas = readDir(schemaPath, '.js');
const controllers = readDir(controllerPath, '.js');
schemas.map( x => registerTypes.push(require(x)));
controllers.map( x => registerResolvers.push(require(x)));
}

if (auto) {
// Construct a schema, using GraphQL schema language from
// protobuf to GraphQL converter
const gqlSchema = this.rpcService.gqlSchema;
if (!gqlSchema) {
console.warn('GraphQL Server start failed due to missing schema.');
return this;
}
// Provide resolver functions for your schema fields
// This section will automatically generate functions and resolvers
registerTypes.push(gql`${gqlSchema}`);
registerResolvers.push(genResolvers(this.rpcService.packages));
}

const typeDefs = gql`${gqlSchema}`;
// Provide resolver functions for your schema fields
// This section will automatically generate functions and resolvers
let resolvers = genResolvers(this.rpcService.packages);
this.gqlServer = new ApolloServer({ typeDefs, resolvers });
const schema = makeExecutableSchema({
typeDefs: registerTypes,
resolvers: registerResolvers,
logger: { log: e => console.log(e) }
});

this.gqlServer = new ApolloServer({schema});

console.log('GraphQL Server is enabled.');
}
Expand Down
2 changes: 1 addition & 1 deletion libs/rpc-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class RPCService {
if (Array.isArray(this.packages)) {
// load definitions from packages
const packageDefinition = grpc.loadPackageDefinition(this.packageDefinition);
if (this.grpcServer && this.graphql === true) {
if (this.grpcServer && this.graphql && this.graphql.enable === true) {
this.gqlSchema = grpcToGraphQL(packageDefinition, this.packages);
}

Expand Down