Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alessandro/web 2189 review core resolvers #3535

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 93 additions & 42 deletions packages/server/modules/core/graph/resolvers/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
publish
} from '@/modules/shared/utils/subscriptions'
import { authorizeResolver } from '@/modules/shared'
import { Knex } from 'knex'

import {
getPaginatedBranchCommitsFactory,
Expand Down Expand Up @@ -76,44 +77,50 @@ import { validateStreamAccessFactory } from '@/modules/core/services/streams/acc
import { saveActivityFactory } from '@/modules/activitystream/repositories'
import { Resolvers } from '@/modules/core/graph/generated/graphql'
import { CommitGraphQLReturn } from '@/modules/core/helpers/graphTypes'
import { getProjectDbClient } from '@/modules/multiregion/dbSelector'
import {
getProjectDbClient,
getRegisteredDbClients
} from '@/modules/multiregion/dbSelector'
import { LegacyUserCommit } from '@/modules/core/domain/commits/types'

const getStreams = getStreamsFactory({ db })

const validateStreamAccess = validateStreamAccessFactory({ authorizeResolver })
const getCommitsByUserId = legacyGetPaginatedUserCommitsPage({ db })
const getCommitsTotalCountByUserId = legacyGetPaginatedUserCommitsTotalCount({ db })

const getAuthorId = (commit: CommitGraphQLReturn) => {
if ('author' in commit) return commit.author
return commit.authorId
}

const getUserCommits = async (
publicOnly: boolean,
userId: string,
args: { limit: number; cursor?: MaybeNullOrUndefined<string> },
streamIdWhitelist?: string[]
) => {
const totalCount = await getCommitsTotalCountByUserId({
userId,
publicOnly,
streamIdWhitelist
})
if (args.limit && args.limit > 100)
throw new BadRequestError(
'Cannot return more than 100 items, please use pagination.'
)
const { commits: items, cursor } = await getCommitsByUserId({
userId,
limit: args.limit,
cursor: args.cursor,
publicOnly,
streamIdWhitelist
})

return { items, cursor, totalCount }
}
const getUserCommitsFactory =
({ db }: { db: Knex }) =>
async (
publicOnly: boolean,
userId: string,
args: { limit: number; cursor?: MaybeNullOrUndefined<string> },
streamIdWhitelist?: string[]
) => {
const getCommitsTotalCountByUserId = legacyGetPaginatedUserCommitsTotalCount({ db })
const totalCount = await getCommitsTotalCountByUserId({
userId,
publicOnly,
streamIdWhitelist
})
if (args.limit && args.limit > 100)
throw new BadRequestError(
'Cannot return more than 100 items, please use pagination.'
)
const getCommitsByUserId = legacyGetPaginatedUserCommitsPage({ db })
const { commits: items, cursor } = await getCommitsByUserId({
userId,
limit: args.limit,
cursor: args.cursor,
publicOnly,
streamIdWhitelist
})

return { items, cursor, totalCount }
}

export = {
Query: {},
Expand Down Expand Up @@ -233,24 +240,68 @@ export = {
},
LimitedUser: {
async commits(parent, args, ctx) {
// TODO: scan all regions
return await getUserCommits(
true,
parent.id,
args,
toProjectIdWhitelist(ctx.resourceAccessRules)
)
// this function is not optimized but it is deprecated
// DUI 2 only uses the totalCount
const { cursor } = args
let result: {
items: LegacyUserCommit[]
totalCount: number
cursor?: string | null
} = {
cursor,
items: [],
totalCount: 0
}
const regionClients = await getRegisteredDbClients()
for (const client of [db, ...regionClients]) {
const getUserCommits = getUserCommitsFactory({ db: client })
const { items, totalCount, cursor } = await getUserCommits(
true,
parent.id,
args,
toProjectIdWhitelist(ctx.resourceAccessRules)
)

result = {
items: [...result.items, ...items],
totalCount: result.totalCount + totalCount,
cursor: cursor ?? result.cursor // this is a bad approximation but this is not used and will be deprecated soon
}
}
return result
}
},
User: {
async commits(parent, args, context) {
// TODO: scan all regions
return await getUserCommits(
context.userId !== parent.id,
parent.id,
args,
toProjectIdWhitelist(context.resourceAccessRules)
)
// this function is not optimized but it is deprecated
// DUI 2 only uses the totalCount
const { cursor } = args
let result: {
items: LegacyUserCommit[]
totalCount: number
cursor?: string | null
} = {
cursor,
items: [],
totalCount: 0
}
const regionClients = await getRegisteredDbClients()
for (const client of [db, ...regionClients]) {
const getUserCommits = getUserCommitsFactory({ db: client })
const { items, totalCount, cursor } = await getUserCommits(
context.userId !== parent.id,
parent.id,
args,
toProjectIdWhitelist(context.resourceAccessRules)
)

result = {
items: [...result.items, ...items],
totalCount: result.totalCount + totalCount,
cursor: cursor ?? result.cursor // this is a bad approximation but this is not used and will be deprecated soon
}
}
return result
}
},
Branch: {
Expand Down
6 changes: 3 additions & 3 deletions packages/server/modules/core/graph/resolvers/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ export = {
// If limit=0, short-cut full execution and use data loader
if (args.limit === 0) {
return {
totalCount: await ctx.loaders.streams.getCommitCountWithoutGlobals.load(
parent.id
),
totalCount: await ctx.loaders
.forRegion({ db: projectDB })
.streams.getCommitCountWithoutGlobals.load(parent.id),
items: [],
cursor: null
}
Expand Down
3 changes: 1 addition & 2 deletions packages/server/modules/core/graph/resolvers/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
storeClosuresIfNotFoundFactory,
storeObjectsIfNotFoundFactory
} from '@/modules/core/repositories/objects'
import { db } from '@/db/knex'
import { createObjectsFactory } from '@/modules/core/services/objects/management'
import { getProjectDbClient } from '@/modules/multiregion/dbSelector'

Expand Down Expand Up @@ -37,7 +36,7 @@ export = {
const projectDB = await getProjectDbClient({ projectId: parent.streamId })
// The simple query branch
if (!args.query && !args.orderBy) {
const getObjectChildren = getObjectChildrenFactory({ db })
const getObjectChildren = getObjectChildrenFactory({ db: projectDB })
const result = await getObjectChildren({
streamId: parent.streamId,
objectId: parent.id,
Expand Down
1 change: 1 addition & 0 deletions packages/server/modules/core/graph/resolvers/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export = {
})
return await updateStreamAndNotify(update, userId!, resourceAccessRules)
},
// This one is only used outside of a workspace, so the project is always created in the main db
async create(_parent, args, context) {
const rateLimitResult = await getRateLimitResult('STREAM_CREATE', context.userId!)
if (isRateLimitBreached(rateLimitResult)) {
Expand Down
18 changes: 9 additions & 9 deletions packages/server/modules/core/graph/resolvers/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export = {
return stream
},

async streams(parent, args, context) {
async streams(_, args, context) {
const totalCount = await getUserStreamsCount({
userId: context.userId!,
searchQuery: args.query || undefined,
Expand All @@ -248,14 +248,14 @@ export = {
return { totalCount, cursor, items: streams }
},

async discoverableStreams(parent, args, ctx) {
async discoverableStreams(_, args, ctx) {
return await getDiscoverableStreams(
args,
toProjectIdWhitelist(ctx.resourceAccessRules)
)
},

async adminStreams(parent, args, ctx) {
async adminStreams(_, args, ctx) {
if (args.limit && args.limit > 50)
throw new BadRequestError('Cannot return more than 50 items at a time.')

Expand Down Expand Up @@ -389,7 +389,7 @@ export = {
}
},
Mutation: {
async streamCreate(parent, args, context) {
async streamCreate(_, args, context) {
const rateLimitResult = await getRateLimitResult('STREAM_CREATE', context.userId!)
if (isRateLimitBreached(rateLimitResult)) {
throw new RateLimitError(rateLimitResult)
Expand All @@ -404,7 +404,7 @@ export = {
return id
},

async streamUpdate(parent, args, context) {
async streamUpdate(_, args, context) {
await updateStreamAndNotify(
args.stream,
context.userId!,
Expand All @@ -413,7 +413,7 @@ export = {
return true
},

async streamDelete(parent, args, context) {
async streamDelete(_, args, context) {
return await deleteStreamAndNotify(
args.id,
context.userId!,
Expand All @@ -422,7 +422,7 @@ export = {
)
},

async streamsDelete(parent, args, context) {
async streamsDelete(_, args, context) {
const results = await Promise.all(
(args.ids || []).map(async (id) => {
return await deleteStreamAndNotify(
Expand All @@ -436,7 +436,7 @@ export = {
return results.every((res) => res === true)
},

async streamUpdatePermission(parent, args, context) {
async streamUpdatePermission(_, args, context) {
const result = await updateStreamRoleAndNotify(
args.permissionParams,
context.userId!,
Expand All @@ -445,7 +445,7 @@ export = {
return !!result
},

async streamRevokePermission(parent, args, context) {
async streamRevokePermission(_, args, context) {
const result = await updateStreamRoleAndNotify(
args.permissionParams,
context.userId!,
Expand Down