Skip to content

Commit

Permalink
Merge pull request #320 from cofacts/non-nulls
Browse files Browse the repository at this point in the history
refactor: add non-null to common types' fields
  • Loading branch information
MrOrz authored Oct 18, 2023
2 parents 5946237 + c3d3a1b commit 0263d29
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
21 changes: 13 additions & 8 deletions src/graphql/models/Article.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ const Article = new GraphQLObjectType({
fields: () => ({
id: { type: new GraphQLNonNull(GraphQLID) },
text: { type: GraphQLString },
createdAt: { type: GraphQLString },
createdAt: { type: new GraphQLNonNull(GraphQLString) },
updatedAt: { type: GraphQLString },
status: { type: new GraphQLNonNull(ReplyRequestStatusEnum) },
references: { type: new GraphQLList(ArticleReference) },
replyCount: {
type: GraphQLInt,
type: new GraphQLNonNull(GraphQLInt),
description: 'Number of normal article replies',
resolve: ({ normalArticleReplyCount }) => normalArticleReplyCount,
resolve: ({ normalArticleReplyCount }) => normalArticleReplyCount ?? 0,
},
articleReplies: {
type: new GraphQLList(ArticleReply),
type: new GraphQLNonNull(
new GraphQLList(new GraphQLNonNull(ArticleReply))
),
description:
'Connections between this article and replies. Sorted by the logic described in https://github.com/cofacts/rumors-line-bot/issues/78.',
args: {
Expand Down Expand Up @@ -203,7 +205,9 @@ const Article = new GraphQLObjectType({
},

articleCategories: {
type: new GraphQLList(ArticleCategory),
type: new GraphQLNonNull(
new GraphQLList(new GraphQLNonNull(ArticleCategory))
),
args: {
status: {
type: ArticleCategoryStatusEnum,
Expand Down Expand Up @@ -239,9 +243,10 @@ const Article = new GraphQLObjectType({
},

categoryCount: {
type: GraphQLInt,
type: new GraphQLNonNull(GraphQLInt),
description: 'Number of normal article categories',
resolve: ({ normalArticleCategoryCount }) => normalArticleCategoryCount,
resolve: ({ normalArticleCategoryCount }) =>
normalArticleCategoryCount ?? 0,
},

replyRequests: {
Expand Down Expand Up @@ -454,7 +459,7 @@ const Article = new GraphQLObjectType({
};
},
// eslint-disable-next-line no-use-before-define
type: ArticleConnection,
type: new GraphQLNonNull(ArticleConnection),
},
hyperlinks: {
type: new GraphQLList(Hyperlink),
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/models/ArticleReference.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const ArticleReferenceTypeEnum = new GraphQLEnumType({
export default new GraphQLObjectType({
name: 'ArticleReference',
fields: () => ({
createdAt: { type: GraphQLString },
type: { type: ArticleReferenceTypeEnum },
createdAt: { type: new GraphQLNonNull(GraphQLString) },
type: { type: new GraphQLNonNull(ArticleReferenceTypeEnum) },
permalink: { type: GraphQLString },
}),
});
Expand Down
30 changes: 21 additions & 9 deletions src/graphql/models/ArticleReply.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default new GraphQLObjectType({
name: 'ArticleReply',
description: 'The linkage between an Article and a Reply',
fields: () => ({
replyId: { type: GraphQLString },
replyId: { type: new GraphQLNonNull(GraphQLString) },

reply: {
type: Reply,
Expand All @@ -38,7 +38,7 @@ export default new GraphQLObjectType({
description: 'Cached reply type value stored in ArticleReply',
},

articleId: { type: GraphQLString },
articleId: { type: new GraphQLNonNull(GraphQLString) },

article: {
type: Article,
Expand All @@ -56,23 +56,35 @@ export default new GraphQLObjectType({
appId: { type: GraphQLNonNull(GraphQLString) },

canUpdateStatus: {
type: GraphQLBoolean,
type: new GraphQLNonNull(GraphQLBoolean),
resolve: ({ userId, appId }, args, { user }) => {
return !!user && userId === user.id && appId === user.appId;
},
},

feedbackCount: {
type: GraphQLInt,
type: new GraphQLNonNull(GraphQLInt),
resolve: ({ positiveFeedbackCount = 0, negativeFeedbackCount = 0 }) =>
positiveFeedbackCount + negativeFeedbackCount,
},

positiveFeedbackCount: { type: GraphQLInt },
negativeFeedbackCount: { type: GraphQLInt },
positiveFeedbackCount: {
type: new GraphQLNonNull(GraphQLInt),
resolve({ positiveFeedbackCount }) {
return positiveFeedbackCount ?? 0;
},
},
negativeFeedbackCount: {
type: new GraphQLNonNull(GraphQLInt),
resolve({ negativeFeedbackCount }) {
return negativeFeedbackCount ?? 0;
},
},

feedbacks: {
type: new GraphQLList(ArticleReplyFeedback),
type: new GraphQLNonNull(
new GraphQLList(new GraphQLNonNull(ArticleReplyFeedback))
),
args: {
statuses: {
type: new GraphQLList(
Expand Down Expand Up @@ -118,11 +130,11 @@ export default new GraphQLObjectType({
},

status: {
type: ArticleReplyStatusEnum,
type: new GraphQLNonNull(ArticleReplyStatusEnum),
resolve: ({ status }) => (status === undefined ? 'NORMAL' : status),
},

createdAt: { type: GraphQLString },
createdAt: { type: new GraphQLNonNull(GraphQLString) },
updatedAt: { type: GraphQLString },
}),
});
10 changes: 6 additions & 4 deletions src/graphql/models/Reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ const Reply = new GraphQLObjectType({
description: 'The user submitted this reply version',
resolve: userFieldResolver,
},
createdAt: { type: GraphQLString },
createdAt: { type: new GraphQLNonNull(GraphQLString) },
text: { type: GraphQLString },
type: { type: ReplyTypeEnum },
type: { type: new GraphQLNonNull(ReplyTypeEnum) },
reference: { type: GraphQLString },
articleReplies: {
type: new GraphQLList(ArticleReply),
type: new GraphQLNonNull(
new GraphQLList(new GraphQLNonNull(ArticleReply))
),
args: {
status: {
type: ArticleReplyStatusEnum,
Expand Down Expand Up @@ -143,7 +145,7 @@ const Reply = new GraphQLObjectType({
};
},
// eslint-disable-next-line no-use-before-define
type: ReplyConnection,
type: new GraphQLNonNull(ReplyConnection),
},
}),
});
Expand Down
4 changes: 3 additions & 1 deletion src/graphql/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,9 @@ export function createConnectionType(
* @returns {Object[]}
*/
export function filterByStatuses(entriesWithStatus, statuses) {
return entriesWithStatus.filter(({ status }) => statuses.includes(status));
return entriesWithStatus
.filter(Boolean) // Ensure no null inside
.filter(({ status }) => statuses.includes(status));
}

export const DEFAULT_ARTICLE_STATUSES = ['NORMAL'];
Expand Down

0 comments on commit 0263d29

Please sign in to comment.