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

feat(server): multi region module base w/ a test (mocked) mutation #3406

Merged
merged 2 commits into from
Oct 30, 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
2 changes: 2 additions & 0 deletions packages/frontend-2/lib/common/generated/gql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4196,7 +4196,9 @@ export type WorkspacePlan = {
};

export enum WorkspacePlanStatuses {
CancelationScheduled = 'cancelationScheduled',
Canceled = 'canceled',
Expired = 'expired',
PaymentFailed = 'paymentFailed',
Trial = 'trial',
Valid = 'valid'
Expand Down
26 changes: 26 additions & 0 deletions packages/server/assets/multiregion/typedefs/multiregion.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
type ServerRegionItem {
id: String!
key: String!
name: String!
description: String!
}

input CreateServerRegionInput {
key: String!
name: String!
description: String!
}

type ServerRegionMutations {
create(input: CreateServerRegionInput!): ServerRegionItem!
}

type ServerInfoMutations {
multiRegion: ServerRegionMutations!
}

extend type Mutation {
serverInfoMutations: ServerInfoMutations!
@hasServerRole(role: SERVER_ADMIN)
@hasScope(scope: "server:setup")
}
1 change: 0 additions & 1 deletion packages/server/codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ generates:
User: '@/modules/core/helpers/graphTypes#UserGraphQLReturn'
ActiveUserMutations: '@/modules/core/helpers/graphTypes#MutationsObjectGraphQLReturn'
UserEmailMutations: '@/modules/core/helpers/graphTypes#MutationsObjectGraphQLReturn'
UserWorkspaceMutations: '@/modules/core/helpers/graphTypes#MutationsObjectGraphQLReturn'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GQL type no longer exists

ProjectMutations: '@/modules/core/helpers/graphTypes#MutationsObjectGraphQLReturn'
ProjectInviteMutations: '@/modules/core/helpers/graphTypes#MutationsObjectGraphQLReturn'
ModelMutations: '@/modules/core/helpers/graphTypes#MutationsObjectGraphQLReturn'
Expand Down
62 changes: 62 additions & 0 deletions packages/server/modules/core/graph/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,12 @@ export type CreateModelInput = {
projectId: Scalars['ID']['input'];
};

export type CreateServerRegionInput = {
description: Scalars['String']['input'];
key: Scalars['String']['input'];
name: Scalars['String']['input'];
};

export type CreateUserEmailInput = {
email: Scalars['String']['input'];
};
Expand Down Expand Up @@ -1318,6 +1324,7 @@ export type Mutation = {
/** (Re-)send the account verification e-mail */
requestVerification: Scalars['Boolean']['output'];
requestVerificationByEmail: Scalars['Boolean']['output'];
serverInfoMutations: ServerInfoMutations;
serverInfoUpdate?: Maybe<Scalars['Boolean']['output']>;
/** Note: The required scope to invoke this is not given out to app or personal access tokens */
serverInviteBatchCreate: Scalars['Boolean']['output'];
Expand Down Expand Up @@ -2830,6 +2837,11 @@ export type ServerInfo = {
workspaces: ServerWorkspacesInfo;
};

export type ServerInfoMutations = {
__typename?: 'ServerInfoMutations';
multiRegion: ServerRegionMutations;
};

export type ServerInfoUpdateInput = {
adminContact?: InputMaybe<Scalars['String']['input']>;
company?: InputMaybe<Scalars['String']['input']>;
Expand Down Expand Up @@ -2860,6 +2872,24 @@ export type ServerMigration = {
movedTo?: Maybe<Scalars['String']['output']>;
};

export type ServerRegionItem = {
__typename?: 'ServerRegionItem';
description: Scalars['String']['output'];
id: Scalars['String']['output'];
key: Scalars['String']['output'];
name: Scalars['String']['output'];
};

export type ServerRegionMutations = {
__typename?: 'ServerRegionMutations';
create: ServerRegionItem;
};


export type ServerRegionMutationsCreateArgs = {
input: CreateServerRegionInput;
};

export enum ServerRole {
ServerAdmin = 'SERVER_ADMIN',
ServerArchivedUser = 'SERVER_ARCHIVED_USER',
Expand Down Expand Up @@ -4216,7 +4246,9 @@ export type WorkspacePlan = {
};

export enum WorkspacePlanStatuses {
CancelationScheduled = 'cancelationScheduled',
Canceled = 'canceled',
Expired = 'expired',
PaymentFailed = 'paymentFailed',
Trial = 'trial',
Valid = 'valid'
Expand Down Expand Up @@ -4469,6 +4501,7 @@ export type ResolversTypes = {
CreateCommentInput: CreateCommentInput;
CreateCommentReplyInput: CreateCommentReplyInput;
CreateModelInput: CreateModelInput;
CreateServerRegionInput: CreateServerRegionInput;
CreateUserEmailInput: CreateUserEmailInput;
CreateVersionInput: CreateVersionInput;
Currency: Currency;
Expand Down Expand Up @@ -4564,10 +4597,13 @@ export type ResolversTypes = {
ServerAutomateInfo: ResolverTypeWrapper<ServerAutomateInfo>;
ServerConfiguration: ResolverTypeWrapper<ServerConfiguration>;
ServerInfo: ResolverTypeWrapper<ServerInfoGraphQLReturn>;
ServerInfoMutations: ResolverTypeWrapper<ServerInfoMutations>;
ServerInfoUpdateInput: ServerInfoUpdateInput;
ServerInvite: ResolverTypeWrapper<ServerInviteGraphQLReturnType>;
ServerInviteCreateInput: ServerInviteCreateInput;
ServerMigration: ResolverTypeWrapper<ServerMigration>;
ServerRegionItem: ResolverTypeWrapper<ServerRegionItem>;
ServerRegionMutations: ResolverTypeWrapper<ServerRegionMutations>;
ServerRole: ServerRole;
ServerRoleItem: ResolverTypeWrapper<ServerRoleItem>;
ServerStatistics: ResolverTypeWrapper<GraphQLEmptyReturn>;
Expand Down Expand Up @@ -4739,6 +4775,7 @@ export type ResolversParentTypes = {
CreateCommentInput: CreateCommentInput;
CreateCommentReplyInput: CreateCommentReplyInput;
CreateModelInput: CreateModelInput;
CreateServerRegionInput: CreateServerRegionInput;
CreateUserEmailInput: CreateUserEmailInput;
CreateVersionInput: CreateVersionInput;
DateTime: Scalars['DateTime']['output'];
Expand Down Expand Up @@ -4820,10 +4857,13 @@ export type ResolversParentTypes = {
ServerAutomateInfo: ServerAutomateInfo;
ServerConfiguration: ServerConfiguration;
ServerInfo: ServerInfoGraphQLReturn;
ServerInfoMutations: ServerInfoMutations;
ServerInfoUpdateInput: ServerInfoUpdateInput;
ServerInvite: ServerInviteGraphQLReturnType;
ServerInviteCreateInput: ServerInviteCreateInput;
ServerMigration: ServerMigration;
ServerRegionItem: ServerRegionItem;
ServerRegionMutations: ServerRegionMutations;
ServerRoleItem: ServerRoleItem;
ServerStatistics: GraphQLEmptyReturn;
ServerStats: GraphQLEmptyReturn;
Expand Down Expand Up @@ -5517,6 +5557,7 @@ export type MutationResolvers<ContextType = GraphQLContext, ParentType extends R
projectMutations?: Resolver<ResolversTypes['ProjectMutations'], ParentType, ContextType>;
requestVerification?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
requestVerificationByEmail?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType, RequireFields<MutationRequestVerificationByEmailArgs, 'email'>>;
serverInfoMutations?: Resolver<ResolversTypes['ServerInfoMutations'], ParentType, ContextType>;
serverInfoUpdate?: Resolver<Maybe<ResolversTypes['Boolean']>, ParentType, ContextType, RequireFields<MutationServerInfoUpdateArgs, 'info'>>;
serverInviteBatchCreate?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType, RequireFields<MutationServerInviteBatchCreateArgs, 'input'>>;
serverInviteCreate?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType, RequireFields<MutationServerInviteCreateArgs, 'input'>>;
Expand Down Expand Up @@ -5914,6 +5955,11 @@ export type ServerInfoResolvers<ContextType = GraphQLContext, ParentType extends
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type ServerInfoMutationsResolvers<ContextType = GraphQLContext, ParentType extends ResolversParentTypes['ServerInfoMutations'] = ResolversParentTypes['ServerInfoMutations']> = {
multiRegion?: Resolver<ResolversTypes['ServerRegionMutations'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type ServerInviteResolvers<ContextType = GraphQLContext, ParentType extends ResolversParentTypes['ServerInvite'] = ResolversParentTypes['ServerInvite']> = {
email?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
id?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
Expand All @@ -5927,6 +5973,19 @@ export type ServerMigrationResolvers<ContextType = GraphQLContext, ParentType ex
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type ServerRegionItemResolvers<ContextType = GraphQLContext, ParentType extends ResolversParentTypes['ServerRegionItem'] = ResolversParentTypes['ServerRegionItem']> = {
description?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
id?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
key?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
name?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type ServerRegionMutationsResolvers<ContextType = GraphQLContext, ParentType extends ResolversParentTypes['ServerRegionMutations'] = ResolversParentTypes['ServerRegionMutations']> = {
create?: Resolver<ResolversTypes['ServerRegionItem'], ParentType, ContextType, RequireFields<ServerRegionMutationsCreateArgs, 'input'>>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type ServerRoleItemResolvers<ContextType = GraphQLContext, ParentType extends ResolversParentTypes['ServerRoleItem'] = ResolversParentTypes['ServerRoleItem']> = {
id?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
title?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
Expand Down Expand Up @@ -6497,8 +6556,11 @@ export type Resolvers<ContextType = GraphQLContext> = {
ServerAutomateInfo?: ServerAutomateInfoResolvers<ContextType>;
ServerConfiguration?: ServerConfigurationResolvers<ContextType>;
ServerInfo?: ServerInfoResolvers<ContextType>;
ServerInfoMutations?: ServerInfoMutationsResolvers<ContextType>;
ServerInvite?: ServerInviteResolvers<ContextType>;
ServerMigration?: ServerMigrationResolvers<ContextType>;
ServerRegionItem?: ServerRegionItemResolvers<ContextType>;
ServerRegionMutations?: ServerRegionMutationsResolvers<ContextType>;
ServerRoleItem?: ServerRoleItemResolvers<ContextType>;
ServerStatistics?: ServerStatisticsResolvers<ContextType>;
ServerStats?: ServerStatsResolvers<ContextType>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,12 @@ export type CreateModelInput = {
projectId: Scalars['ID']['input'];
};

export type CreateServerRegionInput = {
description: Scalars['String']['input'];
key: Scalars['String']['input'];
name: Scalars['String']['input'];
};

export type CreateUserEmailInput = {
email: Scalars['String']['input'];
};
Expand Down Expand Up @@ -1301,6 +1307,7 @@ export type Mutation = {
/** (Re-)send the account verification e-mail */
requestVerification: Scalars['Boolean']['output'];
requestVerificationByEmail: Scalars['Boolean']['output'];
serverInfoMutations: ServerInfoMutations;
serverInfoUpdate?: Maybe<Scalars['Boolean']['output']>;
/** Note: The required scope to invoke this is not given out to app or personal access tokens */
serverInviteBatchCreate: Scalars['Boolean']['output'];
Expand Down Expand Up @@ -2813,6 +2820,11 @@ export type ServerInfo = {
workspaces: ServerWorkspacesInfo;
};

export type ServerInfoMutations = {
__typename?: 'ServerInfoMutations';
multiRegion: ServerRegionMutations;
};

export type ServerInfoUpdateInput = {
adminContact?: InputMaybe<Scalars['String']['input']>;
company?: InputMaybe<Scalars['String']['input']>;
Expand Down Expand Up @@ -2843,6 +2855,24 @@ export type ServerMigration = {
movedTo?: Maybe<Scalars['String']['output']>;
};

export type ServerRegionItem = {
__typename?: 'ServerRegionItem';
description: Scalars['String']['output'];
id: Scalars['String']['output'];
key: Scalars['String']['output'];
name: Scalars['String']['output'];
};

export type ServerRegionMutations = {
__typename?: 'ServerRegionMutations';
create: ServerRegionItem;
};


export type ServerRegionMutationsCreateArgs = {
input: CreateServerRegionInput;
};

export enum ServerRole {
ServerAdmin = 'SERVER_ADMIN',
ServerArchivedUser = 'SERVER_ARCHIVED_USER',
Expand Down Expand Up @@ -4199,7 +4229,9 @@ export type WorkspacePlan = {
};

export enum WorkspacePlanStatuses {
CancelationScheduled = 'cancelationScheduled',
Canceled = 'canceled',
Expired = 'expired',
PaymentFailed = 'paymentFailed',
Trial = 'trial',
Valid = 'valid'
Expand Down
19 changes: 15 additions & 4 deletions packages/server/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-var-requires */

import fs from 'fs'
import path from 'path'
import { appRoot, packageRoot } from '@/bootstrap'
Expand All @@ -22,6 +22,7 @@ import {
} from '@/modules/core/graph/helpers/directiveHelper'
import { AppMocksConfig } from '@/modules/mocks'
import { SpeckleModuleMocksConfig } from '@/modules/shared/helpers/mocks'
import { LogicError } from '@/modules/shared/errors'

/**
* Cached speckle module requires
Expand Down Expand Up @@ -77,7 +78,8 @@ const getEnabledModuleNames = () => {
'serverinvites',
'stats',
'webhooks',
'workspacesCore'
'workspacesCore',
'multiregion'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned before, the module itself is enabled always (for simplicity), but then functionality checks inside of it can use the feature flag

]

if (FF_AUTOMATE_MODULE_ENABLED) moduleNames.push('automate')
Expand All @@ -93,7 +95,13 @@ async function getSpeckleModules() {
const moduleNames = getEnabledModuleNames()

for (const dir of moduleNames) {
loadedModules.push(require(`./${dir}`))
const moduleIndex = await import(`./${dir}/index`)
const moduleDefinition = 'init' in moduleIndex ? moduleIndex : moduleIndex.default
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally supporting export default {} in module index files, no longer having to do any of this export = {} bullshit

if (!('init' in moduleDefinition)) {
throw new LogicError(`Module ${dir} does not have an init function`)
}

loadedModules.push(moduleDefinition)
}

return loadedModules
Expand Down Expand Up @@ -185,7 +193,10 @@ const graphComponents = (): Pick<ApolloServerOptions<any>, 'resolvers'> & {
// first pass load of resolvers
const resolversPath = path.join(fullPath, 'graph', 'resolvers')
if (fs.existsSync(resolversPath)) {
resolverObjs = [...resolverObjs, ...values(autoloadFromDirectory(resolversPath))]
const newResolverObjs = values(autoloadFromDirectory(resolversPath)).map((o) =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly supporting export default {} in GQL resolver modules

'default' in o ? o.default : o
)
resolverObjs = [...resolverObjs, ...newResolverObjs]
}

// load directives
Expand Down
5 changes: 5 additions & 0 deletions packages/server/modules/multiregion/domain/operations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { RegionServerConfig } from '@/modules/multiregion/domain/types'

export type GetAvailableRegionConfigs = () => Promise<{
[key: string]: RegionServerConfig
}>
12 changes: 12 additions & 0 deletions packages/server/modules/multiregion/domain/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type RegionServerConfig = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated based on Iain's proposed schema

postgres: {
/**
* Full Postgres connection URI (e.g. "postgres://user:password@host:port/dbname")
*/
connectionUri: string
/**
* SSL cert, if any
*/
publicTlsCertificate?: string
iainsproat marked this conversation as resolved.
Show resolved Hide resolved
}
}
6 changes: 6 additions & 0 deletions packages/server/modules/multiregion/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BaseError } from '@/modules/shared/errors'

export class MultiRegionSupportDisabledError extends BaseError {
static code = 'MULTI_REGION_SUPPORT_DISABLED'
static defaultMessage = 'Multi region support is disabled'
}
16 changes: 16 additions & 0 deletions packages/server/modules/multiregion/graph/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SpeckleModuleMocksConfig } from '@/modules/shared/helpers/mocks'
import { faker } from '@faker-js/faker'

export default {
mocks: {
ServerRegionItem: () => {
const key = faker.string.uuid()
return {
id: key,
key,
name: faker.address.country(),
description: faker.lorem.sentence()
}
}
}
} as SpeckleModuleMocksConfig
5 changes: 5 additions & 0 deletions packages/server/modules/multiregion/graph/resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Resolvers } from '@/modules/core/graph/generated/graphql'

// TODO: Real implementation? Need to discuss questions regarding data model & region config first
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I got my answers earlier today, so I should be unblocked for this


export default {} as Resolvers
7 changes: 7 additions & 0 deletions packages/server/modules/multiregion/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getFeatureFlags } from '@/modules/shared/helpers/envHelper'

export const isMultiRegionEnabled = () => {
const { FF_WORKSPACES_MODULE_ENABLED, FF_WORKSPACES_MULTI_REGION_ENABLED } =
getFeatureFlags()
return !!(FF_WORKSPACES_MODULE_ENABLED && FF_WORKSPACES_MULTI_REGION_ENABLED)
}
Loading
Loading