add addFollowingToLibrary API

This commit is contained in:
Hongbo Wu 2023-11-08 11:43:11 +08:00
parent 99616501cf
commit cc53992b9a
4 changed files with 147 additions and 0 deletions

View file

@ -17,6 +17,24 @@ export type Scalars = {
JSON: any;
};
export type AddFollowingToLibraryError = {
__typename?: 'AddFollowingToLibraryError';
errorCodes: Array<AddFollowingToLibraryErrorCode>;
};
export enum AddFollowingToLibraryErrorCode {
BadRequest = 'BAD_REQUEST',
NotFound = 'NOT_FOUND',
Unauthorized = 'UNAUTHORIZED'
}
export type AddFollowingToLibraryResult = AddFollowingToLibraryError | AddFollowingToLibrarySuccess;
export type AddFollowingToLibrarySuccess = {
__typename?: 'AddFollowingToLibrarySuccess';
articleSavingRequest: ArticleSavingRequest;
};
export type AddPopularReadError = {
__typename?: 'AddPopularReadError';
errorCodes: Array<AddPopularReadErrorCode>;
@ -1305,6 +1323,7 @@ export type MoveLabelSuccess = {
export type Mutation = {
__typename?: 'Mutation';
addFollowingToLibrary: AddFollowingToLibraryResult;
addPopularRead: AddPopularReadResult;
bulkAction: BulkActionResult;
createArticle: CreateArticleResult;
@ -1368,6 +1387,11 @@ export type Mutation = {
};
export type MutationAddFollowingToLibraryArgs = {
id: Scalars['ID'];
};
export type MutationAddPopularReadArgs = {
name: Scalars['String'];
};
@ -3429,6 +3453,10 @@ export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs
/** Mapping between all available schema types and the resolvers types */
export type ResolversTypes = {
AddFollowingToLibraryError: ResolverTypeWrapper<AddFollowingToLibraryError>;
AddFollowingToLibraryErrorCode: AddFollowingToLibraryErrorCode;
AddFollowingToLibraryResult: ResolversTypes['AddFollowingToLibraryError'] | ResolversTypes['AddFollowingToLibrarySuccess'];
AddFollowingToLibrarySuccess: ResolverTypeWrapper<AddFollowingToLibrarySuccess>;
AddPopularReadError: ResolverTypeWrapper<AddPopularReadError>;
AddPopularReadErrorCode: AddPopularReadErrorCode;
AddPopularReadResult: ResolversTypes['AddPopularReadError'] | ResolversTypes['AddPopularReadSuccess'];
@ -3953,6 +3981,9 @@ export type ResolversTypes = {
/** Mapping between all available schema types and the resolvers parents */
export type ResolversParentTypes = {
AddFollowingToLibraryError: AddFollowingToLibraryError;
AddFollowingToLibraryResult: ResolversParentTypes['AddFollowingToLibraryError'] | ResolversParentTypes['AddFollowingToLibrarySuccess'];
AddFollowingToLibrarySuccess: AddFollowingToLibrarySuccess;
AddPopularReadError: AddPopularReadError;
AddPopularReadResult: ResolversParentTypes['AddPopularReadError'] | ResolversParentTypes['AddPopularReadSuccess'];
AddPopularReadSuccess: AddPopularReadSuccess;
@ -4367,6 +4398,20 @@ export type SanitizeDirectiveArgs = {
export type SanitizeDirectiveResolver<Result, Parent, ContextType = ResolverContext, Args = SanitizeDirectiveArgs> = DirectiveResolverFn<Result, Parent, ContextType, Args>;
export type AddFollowingToLibraryErrorResolvers<ContextType = ResolverContext, ParentType extends ResolversParentTypes['AddFollowingToLibraryError'] = ResolversParentTypes['AddFollowingToLibraryError']> = {
errorCodes?: Resolver<Array<ResolversTypes['AddFollowingToLibraryErrorCode']>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};
export type AddFollowingToLibraryResultResolvers<ContextType = ResolverContext, ParentType extends ResolversParentTypes['AddFollowingToLibraryResult'] = ResolversParentTypes['AddFollowingToLibraryResult']> = {
__resolveType: TypeResolveFn<'AddFollowingToLibraryError' | 'AddFollowingToLibrarySuccess', ParentType, ContextType>;
};
export type AddFollowingToLibrarySuccessResolvers<ContextType = ResolverContext, ParentType extends ResolversParentTypes['AddFollowingToLibrarySuccess'] = ResolversParentTypes['AddFollowingToLibrarySuccess']> = {
articleSavingRequest?: Resolver<ResolversTypes['ArticleSavingRequest'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};
export type AddPopularReadErrorResolvers<ContextType = ResolverContext, ParentType extends ResolversParentTypes['AddPopularReadError'] = ResolversParentTypes['AddPopularReadError']> = {
errorCodes?: Resolver<Array<ResolversTypes['AddPopularReadErrorCode']>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
@ -5308,6 +5353,7 @@ export type MoveLabelSuccessResolvers<ContextType = ResolverContext, ParentType
};
export type MutationResolvers<ContextType = ResolverContext, ParentType extends ResolversParentTypes['Mutation'] = ResolversParentTypes['Mutation']> = {
addFollowingToLibrary?: Resolver<ResolversTypes['AddFollowingToLibraryResult'], ParentType, ContextType, RequireFields<MutationAddFollowingToLibraryArgs, 'id'>>;
addPopularRead?: Resolver<ResolversTypes['AddPopularReadResult'], ParentType, ContextType, RequireFields<MutationAddPopularReadArgs, 'name'>>;
bulkAction?: Resolver<ResolversTypes['BulkActionResult'], ParentType, ContextType, RequireFields<MutationBulkActionArgs, 'action' | 'query'>>;
createArticle?: Resolver<ResolversTypes['CreateArticleResult'], ParentType, ContextType, RequireFields<MutationCreateArticleArgs, 'input'>>;
@ -6386,6 +6432,9 @@ export type WebhooksSuccessResolvers<ContextType = ResolverContext, ParentType e
};
export type Resolvers<ContextType = ResolverContext> = {
AddFollowingToLibraryError?: AddFollowingToLibraryErrorResolvers<ContextType>;
AddFollowingToLibraryResult?: AddFollowingToLibraryResultResolvers<ContextType>;
AddFollowingToLibrarySuccess?: AddFollowingToLibrarySuccessResolvers<ContextType>;
AddPopularReadError?: AddPopularReadErrorResolvers<ContextType>;
AddPopularReadResult?: AddPopularReadResultResolvers<ContextType>;
AddPopularReadSuccess?: AddPopularReadSuccessResolvers<ContextType>;

View file

@ -1,5 +1,21 @@
directive @sanitize(allowedTags: [String], maxLength: Int, minLength: Int, pattern: String) on INPUT_FIELD_DEFINITION
type AddFollowingToLibraryError {
errorCodes: [AddFollowingToLibraryErrorCode!]!
}
enum AddFollowingToLibraryErrorCode {
BAD_REQUEST
NOT_FOUND
UNAUTHORIZED
}
union AddFollowingToLibraryResult = AddFollowingToLibraryError | AddFollowingToLibrarySuccess
type AddFollowingToLibrarySuccess {
articleSavingRequest: ArticleSavingRequest!
}
type AddPopularReadError {
errorCodes: [AddPopularReadErrorCode!]!
}
@ -1169,6 +1185,7 @@ type MoveLabelSuccess {
}
type Mutation {
addFollowingToLibrary(id: ID!): AddFollowingToLibraryResult!
addPopularRead(name: String!): AddPopularReadResult!
bulkAction(action: BulkActionType!, async: Boolean, expectedCount: Int, labelIds: [ID!], query: String!): BulkActionResult!
createArticle(input: CreateArticleInput!): CreateArticleResult!

View file

@ -1,15 +1,23 @@
import { IsNull, Not } from 'typeorm'
import { LibraryItem } from '../../entity/library_item'
import {
AddFollowingToLibraryError,
AddFollowingToLibraryErrorCode,
AddFollowingToLibrarySuccess,
FeedEdge,
FeedsError,
FeedsErrorCode,
FeedsSuccess,
MutationAddFollowingToLibraryArgs,
MutationSaveFollowingArgs,
QueryFeedsArgs,
SaveFollowingError,
SaveFollowingSuccess,
} from '../../generated/graphql'
import { feedRepository } from '../../repository/feed'
import { createPageSaveRequest } from '../../services/create_page_save_request'
import { createFollowing } from '../../services/library_item'
import { analytics } from '../../utils/analytics'
import { authorized } from '../../utils/helpers'
export const feedsResolve = authorized<
@ -69,6 +77,14 @@ export const saveFollowingResolver = authorized<
SaveFollowingError,
MutationSaveFollowingArgs
>(async (_, { input }, { uid }) => {
analytics.track({
userId: uid,
event: 'save_following',
properties: {
url: input.url,
},
})
const newItem = await createFollowing(input, uid)
return {
@ -82,3 +98,49 @@ export const saveFollowingResolver = authorized<
},
}
})
export const addFollowingToLibraryResolver = authorized<
AddFollowingToLibrarySuccess,
AddFollowingToLibraryError,
MutationAddFollowingToLibraryArgs
>(async (_, { id }, { authTrx, pubsub, uid }) => {
analytics.track({
userId: uid,
event: 'add_following_to_library',
properties: {
id,
},
})
const item = await authTrx((tx) =>
tx.getRepository(LibraryItem).findOne({
where: {
id,
sharedAt: Not(IsNull()),
isInLibrary: false,
},
relations: ['user'],
})
)
if (!item) {
return {
errorCodes: [AddFollowingToLibraryErrorCode.NotFound],
}
}
const articleSavingRequest = await createPageSaveRequest({
userId: uid,
url: item.originalUrl,
articleSavingRequestId: id,
priority: 'high',
publishedAt: item.publishedAt || undefined,
savedAt: item.savedAt || undefined,
pubsub,
})
return {
__typename: 'AddFollowingToLibrarySuccess',
articleSavingRequest,
}
})

View file

@ -2687,6 +2687,24 @@ const schema = gql`
sharedAt: Date!
}
union AddFollowingToLibraryResult =
AddFollowingToLibrarySuccess
| AddFollowingToLibraryError
type AddFollowingToLibrarySuccess {
articleSavingRequest: ArticleSavingRequest!
}
type AddFollowingToLibraryError {
errorCodes: [AddFollowingToLibraryErrorCode!]!
}
enum AddFollowingToLibraryErrorCode {
UNAUTHORIZED
BAD_REQUEST
NOT_FOUND
}
# Mutations
type Mutation {
googleLogin(input: GoogleLoginInput!): LoginResult!
@ -2791,6 +2809,7 @@ const schema = gql`
input: UpdateSubscriptionInput!
): UpdateSubscriptionResult!
saveFollowing(input: SaveFollowingInput!): SaveFollowingResult!
addFollowingToLibrary(id: ID!): AddFollowingToLibraryResult!
}
# FIXME: remove sort from feedArticles after all cached tabs are closed