Skip to content

Commit

Permalink
fix: bail out of gql migration if @auth uses queries/mutations (#9004)
Browse files Browse the repository at this point in the history
Co-authored-by: Colin Ihrig <[email protected]>
  • Loading branch information
cjihrig and cjihrig-aws authored Nov 21, 2021
1 parent 9ecb90c commit 57a0bd5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,28 @@ describe('attemptV2TransformerMigration', () => {
expect(cliJsonFile.features.graphqltransformer.transformerversion).toBe(1);
expect(cliJsonFile.features.graphqltransformer.suppressschemamigrationprompt).toBe(false);
});

it('fails if @auth uses queries or mutations', async () => {
const apiResourceDir = resourceDir(tempProjectDir);
const schemaPath = path.join(apiResourceDir, 'schema', 'schema.graphql');

fs.writeFileSync(
schemaPath,
`
type Post @model @auth(rules: [{allow: groups, groups: ["Admin", "Dev"], queries: [get, list], operations: [create, update, delete]}]) {
id: ID!
title: String!
createdAt: String
updatedAt: String
}
`,
);
await attemptV2TransformerMigration(apiResourceDir, apiName, envName);
expect(printer.info).toHaveBeenCalledWith(expect.stringMatching('You are using queries or mutations in at least one @auth rule.'));

const cliJsonFile = await fs.readJSON(cliJsonPath(tempProjectDir), { encoding: 'utf8' });
expect(cliJsonFile.features.graphqltransformer.useexperimentalpipelinedtransformer).toBe(false);
expect(cliJsonFile.features.graphqltransformer.transformerversion).toBe(1);
expect(cliJsonFile.features.graphqltransformer.suppressschemamigrationprompt).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,27 @@ export function isImprovedPluralizationEnabled() {
export function isTransformerV2Enabled() {
return FeatureFlags.getNumber('graphqltransformer.transformerversion') === 2;
}

export function authRuleUsesQueriesOrMutations(schema: string): boolean {
const authDirectives = collectDirectives(schema).filter(directive => directive.name.value === 'auth');

for (const authDir of authDirectives) {
const rulesArg =
authDir.arguments?.filter(arg => arg.name.value === 'rules' && arg.value.kind === 'ListValue').map((arg: any) => arg.value.values) ??
[];

for (const rules of rulesArg) {
for (const rule of rules) {
for (const field of rule.fields) {
const fieldName = field.name.value;

if (fieldName === 'queries' || fieldName === 'mutations') {
return true;
}
}
}
}
}

return false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DocumentNode } from 'graphql/language';
import { printer, prompter } from 'amplify-prompts';
import * as path from 'path';
import {
authRuleUsesQueriesOrMutations,
detectCustomRootTypes,
detectDeprecatedConnectionUsage,
detectOverriddenResolvers,
Expand Down Expand Up @@ -180,6 +181,9 @@ async function canAutoMigrate(fullSchema: string, apiName: string, resourceDir:
if (doesBackupExist(resourceDir)) {
return `A schema backup already exists at ${backupLocation(resourceDir)}. Remove or copy these files to a different location.`;
}
if (authRuleUsesQueriesOrMutations(fullSchema)) {
return 'You are using queries or mutations in at least one @auth rule. These cannot be automatically migrated.';
}
return true;
}

Expand Down

0 comments on commit 57a0bd5

Please sign in to comment.