Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove usage of scopes for approving schema version
Browse files Browse the repository at this point in the history
n1ru4l committed Jan 9, 2025
1 parent a246ee3 commit 12fdd47
Showing 6 changed files with 47 additions and 62 deletions.
5 changes: 5 additions & 0 deletions packages/services/api/src/modules/schema/module.graphql.ts
Original file line number Diff line number Diff line change
@@ -739,6 +739,11 @@ export default gql`
Contract versions of this schema version.
"""
contractVersions: ContractVersionConnection
"""
Whether the viewer can mark this schema version as valid.
Only applies to schema versions within targets using the legacy model
"""
viewerCanMarkAsValid: Boolean!
}
type SchemaVersionGithubMetadata {
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import {
import { ProjectType } from '../../../shared/entities';
import { cache } from '../../../shared/helpers';
import { parseGraphQLSource } from '../../../shared/schema';
import { Session } from '../../auth/lib/authz';
import { OrganizationManager } from '../../organization/providers/organization-manager';
import { ProjectManager } from '../../project/providers/project-manager';
import { Logger } from '../../shared/providers/logger';
@@ -34,6 +35,7 @@ export class SchemaVersionHelper {
private schemaHelper: SchemaHelper,
private projectManager: ProjectManager,
private organizationManager: OrganizationManager,
private session: Session,
private registryChecks: RegistryChecks,
private storage: Storage,
private logger: Logger,
@@ -330,10 +332,37 @@ export class SchemaVersionHelper {
return schemaLog?.sdl ?? null;
}

async getIsValid(schemaVersion: SchemaVersion) {
getIsValid(schemaVersion: SchemaVersion) {
return schemaVersion.isComposable && schemaVersion.hasContractCompositionErrors === false;
}

async getViewerCanMarkAsValid(schemaVersion: SchemaVersion) {
if (this.getIsValid(schemaVersion) === true) {
return false;
}

if (
!(await this.session.canPerformAction({
action: 'schemaVersion:approve',
organizationId: schemaVersion.organizationId,
params: {
organizationId: schemaVersion.organizationId,
projectId: schemaVersion.projectId,
targetId: schemaVersion.targetId,
},
}))
) {
return false;
}

const project = await this.projectManager.getProject({
organizationId: schemaVersion.organizationId,
projectId: schemaVersion.projectId,
});

return project.legacyRegistryModel;
}

/**
* There's a possibility that the composite schema SDL contains parts of the supergraph spec.
* This is a problem because we want to show the public schema to the user, and the supergraph spec is not part of that.
Original file line number Diff line number Diff line change
@@ -177,4 +177,7 @@ export const SchemaVersion: SchemaVersionResolvers = {
contractVersions: (version, _, { injector }) => {
return injector.get(ContractsManager).getContractVersionsForSchemaVersion(version);
},
viewerCanMarkAsValid: async (version, _arg, { injector }) => {
return injector.get(SchemaVersionHelper).getViewerCanMarkAsValid(version);
},
};
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@ const MarkAsValid_SchemaVersionFragment = graphql(`
fragment MarkAsValid_SchemaVersionFragment on SchemaVersion {
id
valid
viewerCanMarkAsValid
}
`);

@@ -54,7 +55,7 @@ export function MarkAsValid(props: {
});
}, [mutate, version]);

if (version?.valid) {
if (version?.viewerCanMarkAsValid) {
return null;
}

37 changes: 0 additions & 37 deletions packages/web/app/src/lib/access/target.ts
Original file line number Diff line number Diff line change
@@ -23,40 +23,3 @@ export function canAccessTarget(

return member.targetAccessScopes.includes(scope);
}

export function useTargetAccess({
scope,
member: mmember,
redirect = false,
organizationSlug,
projectSlug,
targetSlug,
}: {
scope: TargetAccessScope;
member: null | FragmentType<typeof CanAccessTarget_MemberFragment>;
redirect?: boolean;
organizationSlug: string;
projectSlug: string;
targetSlug: string;
}) {
const member = useFragment(CanAccessTarget_MemberFragment, mmember);
const canAccess = canAccessTarget(scope, mmember);
useRedirect({
canAccess,
redirectTo: redirect
? router => {
void router.navigate({
to: '/$organizationSlug/$projectSlug/$targetSlug',
params: {
organizationSlug,
projectSlug,
targetSlug,
},
});
}
: undefined,
entity: member,
});

return canAccess;
}
30 changes: 7 additions & 23 deletions packages/web/app/src/pages/target.tsx
Original file line number Diff line number Diff line change
@@ -20,8 +20,7 @@ import { ScrollArea } from '@/components/ui/scroll-area';
import { Accordion } from '@/components/v2/accordion';
import { GraphQLBlock, GraphQLHighlight } from '@/components/v2/graphql-block';
import { DocumentType, FragmentType, graphql, useFragment } from '@/gql';
import { ProjectType, RegistryModel } from '@/gql/graphql';
import { TargetAccessScope, useTargetAccess } from '@/lib/access/target';
import { ProjectType } from '@/gql/graphql';
import { Link, useRouter } from '@tanstack/react-router';

type CompositeSchema = Extract<
@@ -184,15 +183,6 @@ function SchemaView(props: {
const isDistributed =
project.type === ProjectType.Federation || project.type === ProjectType.Stitching;

const canManage = useTargetAccess({
scope: TargetAccessScope.RegistryWrite,
member: organization.me,
redirect: false,
organizationSlug: organization.slug,
projectSlug: project.slug,
targetSlug: target.slug,
});

const { latestSchemaVersion } = target;
if (!latestSchemaVersion) {
return noSchemaVersion;
@@ -202,8 +192,6 @@ function SchemaView(props: {
return noSchema;
}

const canMarkAsValid = project.registryModel === RegistryModel.Legacy && canManage;

const schemas = useFragment(SchemaView_SchemaFragment, target.latestSchemaVersion?.schemas.nodes);
const compositeSchemas = schemas?.filter(isCompositeSchema) as CompositeSchema[];
const singleSchema = schemas?.filter(schema => !isCompositeSchema(schema))[0] as
@@ -269,16 +257,12 @@ function SchemaView(props: {
</PopoverContent>
</Popover>
)}
{canMarkAsValid ? (
<>
<MarkAsValid
organizationSlug={organization.slug}
projectSlug={project.slug}
targetSlug={target.slug}
version={latestSchemaVersion}
/>{' '}
</>
) : null}
<MarkAsValid
organizationSlug={organization.slug}
projectSlug={project.slug}
targetSlug={target.slug}
version={latestSchemaVersion}
/>
</div>
</div>
{isDistributed ? <Schemas schemas={schemasToDisplay} /> : <Schemas schema={singleSchema} />}

0 comments on commit 12fdd47

Please sign in to comment.