Skip to content

Commit

Permalink
fix: add datastore query in config for auth (#8246)
Browse files Browse the repository at this point in the history
  • Loading branch information
SwaySway authored Sep 22, 2021
1 parent 74ccef0 commit 1e0a1a6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class AuthTransformer extends TransformerAuthBase implements TransformerA
this.addTypeToResourceReferences(def.name.value, rules);
// turn rules into roles and add into acm and roleMap
this.convertRulesToRoles(acm, rules);
this.modelDirectiveConfig.set(typeName, getModelConfig(modelDirective, typeName));
this.modelDirectiveConfig.set(typeName, getModelConfig(modelDirective, typeName, context.isProjectUsingDataStore()));
this.authModelConfig.set(typeName, acm);
};

Expand Down Expand Up @@ -188,7 +188,7 @@ Static group authorization should perform as expected.`,
let acm: AccessControlMatrix;
// check if the parent is already in the model config if not add it
if (!this.modelDirectiveConfig.has(typeName)) {
this.modelDirectiveConfig.set(typeName, getModelConfig(modelDirective, typeName));
this.modelDirectiveConfig.set(typeName, getModelConfig(modelDirective, typeName, context.isProjectUsingDataStore()));
acm = new AccessControlMatrix({
operations: MODEL_OPERATIONS,
resources: collectFieldNames(parent),
Expand Down
3 changes: 2 additions & 1 deletion packages/amplify-graphql-auth-transformer/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ export const ensureAuthRuleDefaults = (rules: AuthRule[]) => {
}
};

export const getModelConfig = (directive: DirectiveNode, typeName: string): ModelDirectiveConfiguration => {
export const getModelConfig = (directive: DirectiveNode, typeName: string, isDataStoreEnabled = false): ModelDirectiveConfiguration => {
const directiveWrapped: DirectiveWrapper = new DirectiveWrapper(directive);
const options = directiveWrapped.getArguments<ModelDirectiveConfiguration>({
queries: {
get: toCamelCase(['get', typeName]),
list: toCamelCase(['list', plurality(typeName, true)]),
...(isDataStoreEnabled ? { sync: toCamelCase(['sync', plurality(typeName, true)]) } : undefined),
},
mutations: {
create: toCamelCase(['create', typeName]),
Expand Down
14 changes: 8 additions & 6 deletions packages/amplify-graphql-auth-transformer/src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ export const getQueryFieldNames = (
type: QueryFieldType.LIST,
});
}
// check if this API is sync enabled and then if the model is sync enabled
// fields.add({
// typeName: 'Query',
// fieldName: camelCase(`sync ${typeName}`),
// type: QueryFieldType.SYNC,
// });

if (modelDirectiveConfig?.queries?.sync) {
fields.add({
typeName: 'Query',
fieldName: modelDirectiveConfig.queries.sync,
type: QueryFieldType.SYNC,
});
}
return fields;
};

Expand Down

0 comments on commit 1e0a1a6

Please sign in to comment.