Skip to content

Commit

Permalink
Add CategoryMap, Tag and Media Types (#83)
Browse files Browse the repository at this point in the history
* BugFix article schema

* BugFix content type

* Add CategoryMap type

* Add Media Type

* Add Tag Type

* Add article schema file

* Add media type enum type

* Add getCategory resolver

* Update media model

* Add getMedia resolver

* Add getTag resolver

* Link article type to category, tag and media type

* Link issue type to article type

* Link user type to article and media type

* Merge all schemas and types

* Update CORS and enable tracing

* BugFix ListStyleEnumType

* Resolve circular dependency issue
  • Loading branch information
rutajdash authored May 7, 2021
1 parent 0dc1ec1 commit 893912a
Show file tree
Hide file tree
Showing 18 changed files with 380 additions and 129 deletions.
7 changes: 4 additions & 3 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ const PORT = process.env.PORT || DEFAULT_PORT;
const CORS_OPTIONS = {
origin:
!process.env.NODE_ENV || process.env.NODE_ENV === 'development'
? 'http://localhost:3000'
? ['http://localhost:3000', 'http://localhost:8080', 'http://localhost:5000']
: process.env.NODE_ENV === 'staging'
? 'http://mm.server1.dashnet.in'
: 'https://mondaymorning.nitrkl.ac.in',
? ['http://localhost:3000', 'http://localhost:8080', 'http://localhost:5000', 'https://mm.server1.dashnet.in']
: ['https://mondaymorning.nitrkl.ac.in', 'https://mondaymorning.nitrkl.in'],
};
app.use(CORS(CORS_OPTIONS));

Expand Down Expand Up @@ -168,6 +168,7 @@ const apolloServer = new ApolloServer({
cors: CORS_OPTIONS,
playground: !process.env.NODE_ENV || process.env.NODE_ENV !== 'production',
debug: !process.env.NODE_ENV || process.env.NODE_ENV === 'development',
tracing: !process.env.NODE_ENV || process.env.NODE_ENV !== 'production',
});

/** Attach Express Server with Apollo Server */
Expand Down
19 changes: 13 additions & 6 deletions server/schema/article/article.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,24 @@ const ArticleSchema = new Schema(
/**
* Only for list types
*
* For type 6 - ordered list
* [0 - Uppercase Alphabets, 1 - Lowercase Alphabaets, 2 - Uppercase Roman Numbers, 3 - Lowercase Roman Numbers, 4 - Standards Numbers]
*
* For type 7 - unordered list
* [0 - Bullet/Filled Circle, 1 - Hollow Circle, 2 - Dash, 3 - Filled Square, 4 - Hollow Square]
* [
* 0 - Uppercase Alphabets
* 1 - Lowercase Alphabaets
* 2 - Uppercase Roman Numbers
* 3 - Lowercase Roman Numbers
* 4 - Standards Numbers
* 5 - Bullet/Filled Circle
* 6 - Hollow Circle
* 7 - Dash
* 8 - Filled Square
* 9 - Hollow Square
* ]
*/
listStyle: {
type: Number,
required: false,
min: 0,
max: 4,
max: 9,
},
},
textFormatting: [
Expand Down
5 changes: 2 additions & 3 deletions server/schema/article/article.mutation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const ContentType = require('../common/content.type');
const {
GraphQLObjectType,
// GraphQLScalarType,
Expand All @@ -19,7 +18,7 @@ const {
// GraphQLTime,
// GraphQLDateTime,
// GraphQLJSON,
// GraphQLJSONObject,
GraphQLJSONObject,
} = require('../scalars');
const { ArticleTypeEnumType, StatusEnumType } = require('./article.enum.types');
const {
Expand Down Expand Up @@ -74,7 +73,7 @@ module.exports = new GraphQLObjectType({
type: ArticleType,
args: {
id: { type: new GraphQLNonNull(GraphQLID) },
content: { type: new GraphQLNonNull(ContentType) },
content: { type: new GraphQLNonNull(new GraphQLList(GraphQLJSONObject)) },
},
resolve: updateArticleContent,
},
Expand Down
20 changes: 20 additions & 0 deletions server/schema/article/article.schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @module app.schema.User
* @description User Schema
*
* @requires module:app.schema.scalars
* @requires module:app.schema.UserQuery
* @requires module:app.schema.UserMutation
*
* @version v1
* @since 0.1.0
*/

const { GraphQLSchema } = require('../scalars');
const ArticleQuery = require('./article.query');
const ArticleMutation = require('./article.mutation');

module.exports = new GraphQLSchema({
query: ArticleQuery,
mutation: ArticleMutation,
});
75 changes: 36 additions & 39 deletions server/schema/article/article.type.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const UserDetailType = require('../common/userDetail.type');
const UserType = require('../user/user.type');
const { getUser } = require('../user/user.resolver');
// const UserType = require('../user/user.type');
// const { getUser } = require('../user/user.resolver');
const ContentType = require('../common/content.type');
const {
GraphQLObjectType,
Expand All @@ -25,58 +25,55 @@ const {
// GraphQLJSONObject,
} = require('../scalars');
const { ArticleTypeEnumType, StatusEnumType } = require('./article.enum.types');
const CategoryMapType = require('../categoryMap/categoryMap.type');
const { getCategory } = require('../categoryMap/categoryMap.resolver');
const TagType = require('../tag/tag.type');
const { getTag } = require('../tag/tag.resolver');
const MediaType = require('../media/media.type');
const { getMedia } = require('../media/media.resolver');

const CategoryType = new GraphQLObjectType({
name: 'Category',
const ArticleCategoryType = new GraphQLObjectType({
name: 'ArticleCategory',
fields: () => ({
category: { type: new GraphQLNonNull(GraphQLInt) },
subcategory: { type: GraphQLBoolean },
referenceID: { type: GraphQLID },
// TODO: Resolve to CategoryMapType
/*
referenceID: {
type: GraphQLID,
resolve: (parent) => parent.reference,
},
reference: {
type: CategoryMapType,
resolve: async (parent, args, context, info) => {},
resolve: (parent) => getCategory(null, { id: parent.reference }),
},
*/
}),
});

const TagType = new GraphQLObjectType({
name: 'Tag',
const ArticleTagType = new GraphQLObjectType({
name: 'ArticleTag',
fields: () => ({
name: { type: GraphQLString },
admin: { type: GraphQLBoolean },
referenceID: { type: GraphQLID },
// TODO: Resolve to TagType
/*
reference: {
type: TagType,
resolve: async (parent, args, context, info) => {},
resolve: (parent) => getTag(null, { id: parent.reference }),
},
*/
}),
});

const CoverMediaType = new GraphQLObjectType({
name: 'CoverMedia',
fields: () => ({
squareID: { type: GraphQLID },
// TODO: Resolve to MediaType
/*
square: {
type: MediaType,
resolve: async (parent, args, context, info) => {},
}
*/
square: {
type: MediaType,
resolve: (parent) => getMedia(null, parent.square),
},
rectangleID: { type: GraphQLID },
// TODO: Resolve to MediaType
/*
rectangle: {
type: MediaType,
resolve: async (parent, args, context, info) => {},
}
*/
rectangle: {
type: MediaType,
resolve: (parent) => getMedia(null, parent.rectangle),
},
}),
});

Expand All @@ -101,8 +98,8 @@ const ArticleType = new GraphQLObjectType({
inshort: { type: GraphQLString },
authors: { type: new GraphQLList(UserDetailType) },
tech: { type: new GraphQLList(UserDetailType) },
category: { type: new GraphQLList(CategoryType) },
tags: { type: GraphQLList(TagType) },
category: { type: new GraphQLList(ArticleCategoryType) },
tags: { type: GraphQLList(ArticleTagType) },
coverMedia: { type: CoverMediaType },
status: { type: StatusEnumType },
isInstituteRestricted: { type: GraphQLBoolean },
Expand All @@ -112,16 +109,16 @@ const ArticleType = new GraphQLObjectType({

createdAt: { type: GraphQLDateTime },
createdBy: { type: GraphQLID },
createdByUser: {
type: UserType,
resolve: (parent, _, context, info) => getUser(null, { id: parent.createdBy }, context, info),
},
// createdByUser: {
// type: UserType,
// resolve: (parent, _, context, info) => getUser(null, { id: parent.createdBy }, context, info),
// },
updatedAt: { type: GraphQLDateTime },
updatedBy: { type: GraphQLID },
updatedByUser: {
type: UserType,
resolve: (parent, _, context, info) => getUser(null, { id: parent.updatedBy }, context, info),
},
// updatedByUser: {
// type: UserType,
// resolve: (parent, _, context, info) => getUser(null, { id: parent.updatedBy }, context, info),
// },
schemaVersion: { type: GraphQLInt },
}),
});
Expand Down
21 changes: 21 additions & 0 deletions server/schema/categoryMap/categoryMap.resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const CategoryMapModel = require('./categoryMap.model');
const { APIError } = require('../../helpers/errorHandler');

module.exports = {
getCategory: async (_parent, { id, number }, _) => {
try {
if (!id && (!number || number instanceof Number)) {
return APIError('BAD_REQUEST');
}

const _category = !id ? await CategoryMapModel.findOne({ number }) : await CategoryMapModel.findById(id);
if (!_category) {
return APIError('NOT_FOUND');
}

return _category;
} catch (error) {
return APIError(null, error);
}
},
};
58 changes: 58 additions & 0 deletions server/schema/categoryMap/categoryMap.type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const {
GraphQLObjectType,
// GraphQLScalarType,
// GraphQLUnionType,
// GraphQLInputObjectType,
// GraphQLEnumType,
// GraphQLInterfaceType,
// GraphQLSchema,
// GraphQLNonNull,
// GraphQLError,
// GraphQLList,
GraphQLString,
GraphQLID,
// GraphQLBoolean,
GraphQLInt,
// GraphQLFloat,
// GraphQLDate,
// GraphQLTime,
GraphQLDateTime,
// GraphQLJSON,
// GraphQLJSONObject,
} = require('../scalars');
// const { getUser } = require('../user/user.resolver');
// const UserType = require('../user/user.type');

const CategoryMapType = new GraphQLObjectType({
name: 'CategoryMap',
fields: () => ({
id: { type: GraphQLID },
number: { type: GraphQLInt },
name: { type: GraphQLString },
parent: {
type: new GraphQLObjectType({
name: 'ParentCategoryMap',
fields: () => ({
number: { type: GraphQLInt },
referenceID: { type: CategoryMapType },
}),
}),
},
}),

createdAt: { type: GraphQLDateTime },
createdBy: { type: GraphQLID },
// createdByUser: {
// type: UserType,
// resolve: (parent, _, context, info) => getUser(null, { id: parent.createdBy }, context, info),
// },
updatedAt: { type: GraphQLDateTime },
updatedBy: { type: GraphQLID },
// updatedByUser: {
// type: UserType,
// resolve: (parent, _, context, info) => getUser(null, { id: parent.updatedBy }, context, info),
// },
schemaVersion: { type: GraphQLInt },
});

module.exports = CategoryMapType;
19 changes: 7 additions & 12 deletions server/schema/common/content.enum.types.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,19 @@ module.exports = {
JUSTIFY: { value: 3 },
},
}),
OrderedListStyleEnumType: new GraphQLEnumType({
name: 'OrderedListStyleEnum',
ListStyleEnumType: new GraphQLEnumType({
name: 'ListStyleEnum',
values: {
UPPER_ALPHA: { value: 0 },
LOWER_ALPHA: { value: 1 },
UPPER_ROMAN: { value: 2 },
LOWER_ROMAN: { value: 3 },
NUMERICAL: { value: 4 },
},
}),
UnorderedListStyleEnumType: new GraphQLEnumType({
name: 'UnorderedListStyleEnum',
values: {
FILLED_CIRCLE: { value: 0 },
HOLLOW_CIRCLE: { value: 1 },
DASH: { value: 2 },
FILLED_SQUARE: { value: 3 },
HOLLOW_SQUARE: { value: 4 },
FILLED_CIRCLE: { value: 5 },
HOLLOW_CIRCLE: { value: 6 },
DASH: { value: 7 },
FILLED_SQUARE: { value: 8 },
HOLLOW_SQUARE: { value: 9 },
},
}),
};
16 changes: 3 additions & 13 deletions server/schema/common/content.type.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {
GraphQLObjectType,
// GraphQLScalarType,
GraphQLUnionType,
// GraphQLUnionType,
// GraphQLInputObjectType,
// GraphQLEnumType,
// GraphQLInterfaceType,
Expand All @@ -20,24 +20,14 @@ const {
// GraphQLJSON,
GraphQLJSONObject,
} = require('../scalars');
const {
ContentTypeEnumType,
OrderedListStyleEnumType,
UnorderedListStyleEnumType,
AlignEnumType,
} = require('./content.enum.types');
const { ContentTypeEnumType, ListStyleEnumType, AlignEnumType } = require('./content.enum.types');

const BlockFormattingType = new GraphQLObjectType({
name: 'BlockFormatting',
fields: () => ({
align: { type: AlignEnumType },
hasHeaderRow: { type: GraphQLBoolean },
listStyle: {
type: new GraphQLUnionType({
name: 'ListStyleUnion',
types: [OrderedListStyleEnumType, UnorderedListStyleEnumType],
}),
},
listStyle: { type: ListStyleEnumType },
}),
});

Expand Down
12 changes: 9 additions & 3 deletions server/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
* @since 0.1.0
*/

const { mergeSchemas } = require('graphql-tools');
const { stitchSchemas } = require('graphql-tools');
const UserSchema = require('./user/user.schema');
const ArticleSchema = require('./article/article.schema');
const IssueSchema = require('./issue/issue.schema');
const CategoryMapType = require('./categoryMap/categoryMap.type');
const TagType = require('./tag/tag.type');
const MediaType = require('./media/media.type');

module.exports = mergeSchemas({
schemas: [UserSchema, IssueSchema],
module.exports = stitchSchemas({
subschemas: [UserSchema, ArticleSchema, IssueSchema],
types: [CategoryMapType, TagType, MediaType],
mergeTypes: true,
});
Loading

0 comments on commit 893912a

Please sign in to comment.