Skip to content

Commit

Permalink
feature: graphql basic query and mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
felixpy committed Nov 14, 2018
1 parent 3d0c12a commit a905ef6
Show file tree
Hide file tree
Showing 24 changed files with 157 additions and 59 deletions.
10 changes: 6 additions & 4 deletions app/graphql/app/connector.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const DataLoader = require('dataloader');
const BaseConnector = require('../base/connector');

class AppConnector {
class AppConnector extends BaseConnector {
constructor(ctx) {
this.ctx = ctx;
super(ctx);
this.loaders = {
userIdLoader: new DataLoader(this.getByIds.bind(this)),
};
Expand All @@ -18,8 +19,9 @@ class AppConnector {
return this.loaders.userIdLoader.load(id);
}

getAll() {
return this.ctx.app.model.App.findAll();
getAll(param) {
const sequelizeJSON = this.getSequelizeJSON(param);
return this.ctx.app.model.App.findAll(sequelizeJSON);
}
}

Expand Down
10 changes: 7 additions & 3 deletions app/graphql/app/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ module.exports = {
app(root, { id }, ctx) {
return ctx.connector.app.getById(id);
},
apps(root, args, ctx) {
return ctx.connector.app.getAll();
apps(root, { param }, ctx) {
return ctx.connector.app.getAll(param);
},
},
App: {
roles(app, args, ctx) {
console.log('WTF', app.id);
return ctx.connector.role.getByAppId(app.id);
},
},
Mutation: {
createApp(root, { data }, ctx) {
return ctx.model.App.createWithT(data);
},
},
};
7 changes: 7 additions & 0 deletions app/graphql/app/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@ type App implements Datable {
create_time: Date!
update_time: Date

groups: [Group]!
roles: [Role]!
resources: [Resource]!
}

input AppEntity {
code: String!
name: String!
}
16 changes: 16 additions & 0 deletions app/graphql/base/connector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

class BaseConnector {
constructor(ctx) {
this.ctx = ctx;
}

getSequelizeJSON(param = {}) {
const { ctx } = this;
const { page = {}, filter = {} } = param;

return ctx.helper.parseSequelizeJSON(page, filter);
}
}

module.exports = BaseConnector;
7 changes: 7 additions & 0 deletions app/graphql/common/interfaces/datable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
__resolveType(data) {
return data.typename;
},
};
2 changes: 2 additions & 0 deletions app/graphql/common/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

module.exports = {
Date: require('./scalars/date'),

Datable: require('./interfaces/datable'),
};
10 changes: 10 additions & 0 deletions app/graphql/common/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@ scalar Date
interface Datable {
create_time: Date!
update_time: Date
}

input SearchParam {
page: String
filter: String
}

type MutationResponse {
status: Int
message: String
}
14 changes: 6 additions & 8 deletions app/graphql/group/connector.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const DataLoader = require('dataloader');
const BaseConnector = require('../base/connector');

class GroupConnector {
class GroupConnector extends BaseConnector {
constructor(ctx) {
this.ctx = ctx;
super(ctx);
this.loaders = {
groupIdLoader: new DataLoader(this.getByIds.bind(this)),
};
Expand All @@ -18,12 +19,9 @@ class GroupConnector {
return this.loaders.groupIdLoader.load(id);
}

getAll() {
return this.ctx.app.model.Group.findAll();
}

getByAppId(appId) {
return this.ctx.app.model.Group.findByAppId(appId);
getAll(param) {
const sequelizeJSON = this.getSequelizeJSON(param);
return this.ctx.app.model.Group.findAll(sequelizeJSON);
}
}

Expand Down
7 changes: 2 additions & 5 deletions app/graphql/group/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ module.exports = {
group(root, { id }, ctx) {
return ctx.connector.group.getById(id);
},
groups(root, { appId }, ctx) {
if (appId) {
return ctx.connector.group.getByAppId(appId);
}
return ctx.connector.group.getAll();
groups(root, { param }, ctx) {
return ctx.connector.group.getAll(param);
},
},
};
2 changes: 2 additions & 0 deletions app/graphql/group/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ type Group implements Datable {
name: String!
create_time: Date!
update_time: Date

roles: [Role]!
}
3 changes: 3 additions & 0 deletions app/graphql/mutation/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Mutation {
createApp(data: AppEntity!): MutationResponse
}
13 changes: 8 additions & 5 deletions app/graphql/query/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
type Query {
app(id: ID!): App
apps: [App]!
apps(param: SearchParam): [App]!

group(id: ID!): Group
groups(appId: Int): [Group]!
groups(param: SearchParam): [Group]!

role(id: ID!): Role
roles(appId: Int, groupId: Int): [Role]!
roles(param: SearchParam): [Role]!

resourceType(id: ID!): ResourceType
resourceTypes(param: SearchParam): [ResourceType]!

resource(id: ID!): Resource
resources(appId: Int, roleId: Int): [Resource]!
resources(param: SearchParam): [Resource]!

user(id: ID!): User
users: [User]!
users(param: SearchParam): [User]!
}
14 changes: 6 additions & 8 deletions app/graphql/resource/connector.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const DataLoader = require('dataloader');
const BaseConnector = require('../base/connector');

class ResourceConnector {
class ResourceConnector extends BaseConnector {
constructor(ctx) {
this.ctx = ctx;
super(ctx);
this.loaders = {
resourceIdLoader: new DataLoader(this.getByIds.bind(this)),
};
Expand All @@ -18,12 +19,9 @@ class ResourceConnector {
return this.loaders.resourceIdLoader.load(id);
}

getAll() {
return this.ctx.app.model.Resource.findAll();
}

getByAppId(appId) {
return this.ctx.app.model.Resource.findByAppId(appId);
getAll(param) {
const sequelizeJSON = this.getSequelizeJSON(param);
return this.ctx.app.model.Resource.findAll(sequelizeJSON);
}
}

Expand Down
7 changes: 2 additions & 5 deletions app/graphql/resource/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ module.exports = {
resource(root, { id }, ctx) {
return ctx.connector.resource.getById(id);
},
resources(root, { appId }, ctx) {
if (appId) {
return ctx.connector.resource.getByAppId(appId);
}
return ctx.connector.resource.getAll();
resources(root, { param }, ctx) {
return ctx.connector.resource.getAll(param);
},
},
};
28 changes: 28 additions & 0 deletions app/graphql/resourcetype/connector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const DataLoader = require('dataloader');
const BaseConnector = require('../base/connector');

class ResourceTypeConnector extends BaseConnector {
constructor(ctx) {
super(ctx);
this.loaders = {
resourceTypeIdLoader: new DataLoader(this.getByIds.bind(this)),
};
}

getByIds(ids) {
return this.ctx.app.model.ResourceType.findByIds(ids);
}

getById(id) {
return this.loaders.resourceTypeIdLoader.load(id);
}

getAll(param) {
const sequelizeJSON = this.getSequelizeJSON(param);
return this.ctx.app.model.ResourceType.findAll(sequelizeJSON);
}
}

module.exports = ResourceTypeConnector;
12 changes: 12 additions & 0 deletions app/graphql/resourcetype/resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = {
Query: {
resourceType(root, { id }, ctx) {
return ctx.connector.resourcetype.getById(id);
},
resourceTypes(root, { param }, ctx) {
return ctx.connector.resourcetype.getAll(param);
},
},
};
9 changes: 9 additions & 0 deletions app/graphql/resourcetype/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type ResourceType implements Datable {
id: ID!
code: String!
name: String!
create_time: Date!
update_time: Date

resources: [Resource]!
}
14 changes: 6 additions & 8 deletions app/graphql/role/connector.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const DataLoader = require('dataloader');
const BaseConnector = require('../base/connector');

class RoleConnector {
class RoleConnector extends BaseConnector {
constructor(ctx) {
this.ctx = ctx;
super(ctx);
this.loaders = {
roleIdLoader: new DataLoader(this.getByIds.bind(this)),
};
Expand All @@ -18,12 +19,9 @@ class RoleConnector {
return this.loaders.roleIdLoader.load(id);
}

getAll() {
return this.ctx.app.model.Role.findAll();
}

getByAppId(appId) {
return this.ctx.app.model.Role.findByAppId(appId);
getAll(param) {
const sequelizeJSON = this.getSequelizeJSON(param);
return this.ctx.app.model.Role.findAll(sequelizeJSON);
}
}

Expand Down
7 changes: 2 additions & 5 deletions app/graphql/role/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ module.exports = {
role(root, { id }, ctx) {
return ctx.connector.role.getById(id);
},
roles(root, { appId }, ctx) {
if (appId) {
return ctx.connector.role.getByAppId(appId);
}
return ctx.connector.role.getAll();
roles(root, { param }, ctx) {
return ctx.connector.role.getAll(param);
},
},
};
2 changes: 2 additions & 0 deletions app/graphql/role/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ type Role implements Datable {
name: String!
create_time: Date!
update_time: Date

resources: [Resource]!
}
10 changes: 6 additions & 4 deletions app/graphql/user/connector.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const DataLoader = require('dataloader');
const BaseConnector = require('../base/connector');

class UserConnector {
class UserConnector extends BaseConnector {
constructor(ctx) {
this.ctx = ctx;
super(ctx);
this.loaders = {
userIdLoader: new DataLoader(this.getByIds.bind(this)),
};
Expand All @@ -18,8 +19,9 @@ class UserConnector {
return this.loaders.userIdLoader.load(id);
}

getAll() {
return this.ctx.app.model.User.findAll();
getAll(param) {
const sequelizeJSON = this.getSequelizeJSON(param);
return this.ctx.app.model.User.findAll(sequelizeJSON);
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/graphql/user/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module.exports = {
user(root, { id }, ctx) {
return ctx.connector.user.getById(id);
},
users(root, args, ctx) {
return ctx.connector.user.getAll();
users(root, { param }, ctx) {
return ctx.connector.user.getAll(param);
},
},
};
4 changes: 4 additions & 0 deletions app/graphql/user/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ type User implements Datable {
name: String!
create_time: Date!
update_time: Date

groups: [Group]!
roles: [Role]!
resources: [Resource]!
}
4 changes: 2 additions & 2 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ module.exports = app => {
router.put('/api/role/:id', controller.role.update);
router.delete('/api/role/:id', controller.role.destroy);
router.get('/api/role/:id/resource', app.middlewares.search(), controller.role.getResource);
router.post('/api/group/:id/resource', controller.group.addResource);
router.delete('/api/group/:id/resource', controller.group.removeResource);
router.post('/api/role/:id/resource', controller.role.addResource);
router.delete('/api/role/:id/resource', controller.role.removeResource);

router.get('/api/resource', app.middlewares.search(), controller.resource.index);
router.post('/api/resource', controller.resource.create);
Expand Down

0 comments on commit a905ef6

Please sign in to comment.