|
| 1 | +import { |
| 2 | + cursorToOffset, |
| 3 | + offsetToCursor, |
| 4 | + type Connection, |
| 5 | + type ConnectionArguments, |
| 6 | +} from 'graphql-relay'; |
| 7 | +import { IResolvers } from '@graphql-tools/utils'; |
| 8 | +import { AuthContext, BaseContext, Context } from '../Context'; |
| 9 | +import { traceResolvers } from './trace'; |
| 10 | + |
| 11 | +import graphorm from '../graphorm'; |
| 12 | +import { CampaignState, type Campaign } from '../entity/campaign'; |
| 13 | +import type { GQLPost } from './posts'; |
| 14 | +import type { GQLSource } from './sources'; |
| 15 | +import { getLimit } from '../common'; |
| 16 | + |
| 17 | +interface GQLCampaign |
| 18 | + extends Pick< |
| 19 | + Campaign, |
| 20 | + 'id' | 'type' | 'flags' | 'createdAt' | 'endedAt' | 'referenceId' | 'state' |
| 21 | + > { |
| 22 | + post: GQLPost; |
| 23 | + source: GQLSource; |
| 24 | +} |
| 25 | + |
| 26 | +export const typeDefs = /* GraphQL */ ` |
| 27 | + type CampaignFlags { |
| 28 | + budget: Int! |
| 29 | + spend: Int! |
| 30 | + users: Int! |
| 31 | + clicks: Int! |
| 32 | + impressions: Int! |
| 33 | + } |
| 34 | +
|
| 35 | + type Campaign { |
| 36 | + id: String! |
| 37 | + type: String! |
| 38 | + state: String! |
| 39 | + createdAt: DateTime! |
| 40 | + endedAt: DateTime! |
| 41 | + flags: CampaignFlags! |
| 42 | + post: Post |
| 43 | + source: Source |
| 44 | + } |
| 45 | +
|
| 46 | + type CampaignEdge { |
| 47 | + node: Campaign! |
| 48 | + """ |
| 49 | + Used in before and after args |
| 50 | + """ |
| 51 | + cursor: String! |
| 52 | + } |
| 53 | +
|
| 54 | + type CampaignConnection { |
| 55 | + pageInfo: PageInfo! |
| 56 | + edges: [CampaignEdge]! |
| 57 | + } |
| 58 | +
|
| 59 | + extend type Query { |
| 60 | + campaignById( |
| 61 | + """ |
| 62 | + ID of the campaign to fetch |
| 63 | + """ |
| 64 | + id: ID! |
| 65 | + ): Campaign! @auth |
| 66 | +
|
| 67 | + campaignsList( |
| 68 | + """ |
| 69 | + Paginate after opaque cursor |
| 70 | + """ |
| 71 | + after: String |
| 72 | + """ |
| 73 | + Paginate first |
| 74 | + """ |
| 75 | + first: Int |
| 76 | + ): CampaignConnection! @auth |
| 77 | + } |
| 78 | +`; |
| 79 | + |
| 80 | +export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers< |
| 81 | + unknown, |
| 82 | + BaseContext |
| 83 | +>({ |
| 84 | + Query: { |
| 85 | + campaignById: async ( |
| 86 | + _, |
| 87 | + { id }: { id: string }, |
| 88 | + ctx: Context, |
| 89 | + info, |
| 90 | + ): Promise<GQLCampaign> => |
| 91 | + graphorm.queryOneOrFail(ctx, info, (builder) => { |
| 92 | + builder.queryBuilder.where({ id }).andWhere({ userId: ctx.userId }); |
| 93 | + |
| 94 | + return builder; |
| 95 | + }), |
| 96 | + campaignsList: async ( |
| 97 | + _, |
| 98 | + args: ConnectionArguments, |
| 99 | + ctx: AuthContext, |
| 100 | + info, |
| 101 | + ): Promise<Connection<GQLCampaign>> => { |
| 102 | + const { userId } = ctx; |
| 103 | + const { after, first = 20 } = args; |
| 104 | + const offset = after ? cursorToOffset(after) : 0; |
| 105 | + |
| 106 | + return graphorm.queryPaginated( |
| 107 | + ctx, |
| 108 | + info, |
| 109 | + () => !!after, |
| 110 | + (nodeSize) => nodeSize === first, |
| 111 | + (_, i) => offsetToCursor(offset + i + 1), |
| 112 | + (builder) => { |
| 113 | + const { alias } = builder; |
| 114 | + |
| 115 | + builder.queryBuilder.andWhere(`"${alias}"."userId" = :userId`, { |
| 116 | + userId, |
| 117 | + }); |
| 118 | + |
| 119 | + builder.queryBuilder.orderBy( |
| 120 | + `CASE WHEN "${alias}"."state" = '${CampaignState.Active}' THEN 0 ELSE 1 END`, |
| 121 | + ); |
| 122 | + builder.queryBuilder.addOrderBy(`"${alias}"."createdAt"`, 'DESC'); |
| 123 | + builder.queryBuilder.limit(getLimit({ limit: first ?? 20 })); |
| 124 | + |
| 125 | + if (after) { |
| 126 | + builder.queryBuilder.offset(offset); |
| 127 | + } |
| 128 | + |
| 129 | + return builder; |
| 130 | + }, |
| 131 | + ); |
| 132 | + }, |
| 133 | + }, |
| 134 | +}); |
0 commit comments