Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private createMutations = (
def: ObjectTypeDefinitionNode,
directive: DirectiveNode,
ctx: TransformerContext,
nonModelArray: ObjectTypeDefinitionNode[]
) => {
const typeName = def.name.value
const mutationFields = [];
// Get any name overrides provided by the user. If an empty map it provided
// then we do not generate those fields.
const directiveArguments: ModelDirectiveArgs = getDirectiveArguments(directive)
// Configure mutations based on *mutations* argument
let shouldMakeCreate = true;
let shouldMakeUpdate = true;
let shouldMakeDelete = true;
let createFieldNameOverride = undefined;
let updateFieldNameOverride = undefined;
let deleteFieldNameOverride = undefined;
// Figure out which mutations to make and if they have name overrides
if (directiveArguments.mutations === null) {
shouldMakeCreate = false
shouldMakeUpdate = false
shouldMakeDelete = false
} else if (directiveArguments.mutations) {
private createSubscriptions = (def: ObjectTypeDefinitionNode, directive: DirectiveNode, ctx: TransformerContext) => {
const typeName = def.name.value
const subscriptionFields = []
const directiveArguments: ModelDirectiveArgs = getDirectiveArguments(directive)
const subscriptionsArgument = directiveArguments.subscriptions
const createResolver = ctx.getResource(ResolverResourceIDs.DynamoDBCreateResolverResourceID(typeName))
const updateResolver = ctx.getResource(ResolverResourceIDs.DynamoDBUpdateResolverResourceID(typeName))
const deleteResolver = ctx.getResource(ResolverResourceIDs.DynamoDBDeleteResolverResourceID(typeName))
if (subscriptionsArgument === null) {
return;
} else if (subscriptionsArgument &&
subscriptionsArgument.level === "OFF") {
return;
} else if (subscriptionsArgument &&
subscriptionsArgument.level !== "PUBLIC") {
// Add the custom subscriptions
const subscriptionToMutationsMap: { [subField: string]: string[] } = {}
const onCreate = subscriptionsArgument.onCreate || []
const onUpdate = subscriptionsArgument.onUpdate || []
public newParameterization = (
parent: ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode,
field: FieldDefinitionNode,
directive: DirectiveNode,
ctx: TransformerContext
): void => {
const parentTypeName = parent.name.value;
const fieldName = field.name.value;
const args : RelationArguments = getDirectiveArguments(directive);
const numFields = args.fields.length;
// Check that related type exists and that the connected object is annotated with @model.
const relatedTypeName = getBaseType(field.type)
const relatedType = ctx.inputDocument.definitions.find(
d => d.kind === Kind.OBJECT_TYPE_DEFINITION && d.name.value === relatedTypeName
) as ObjectTypeDefinitionNode | undefined
// Get Child object's table.
const tableLogicalID = ModelResourceIDs.ModelTableResourceID(relatedType.name.value);
const tableResource = ctx.getResource(tableLogicalID) as Table;
// Ensure that there is at least one field provided.
if (numFields === 0) {
throw new InvalidDirectiveError('No fields passed in to @connection directive.')
}
private protectQueries(ctx: TransformerContext, def: ObjectTypeDefinitionNode, rules: AuthRule[]) {
const secondaryKeyDirectivesWithQueries = (def.directives || []).filter(d => {
const isKey = d.name.value === 'key';
const args = getDirectiveArguments(d);
// @key with a name is a secondary key.
const isSecondaryKey = Boolean(args.name);
const hasQueryField = Boolean(args.queryField);
return isKey && isSecondaryKey && hasQueryField;
});
for (const keyWithQuery of secondaryKeyDirectivesWithQueries) {
const args = getDirectiveArguments(keyWithQuery);
const resolverResourceId = ResolverResourceIDs.ResolverResourceID(ctx.getQueryTypeName(), args.queryField);
this.protectListQuery(ctx, resolverResourceId, rules)
}
}
const rules = getArg('rules', []) as AuthRule[]
this.validateRules(rules)
const { operationRules, queryRules } = this.splitRules(rules);
// For each operation evaluate the rules and apply the changes to the relevant resolver.
this.protectCreateMutation(ctx, ResolverResourceIDs.DynamoDBCreateResolverResourceID(def.name.value), operationRules.create, def)
this.protectUpdateMutation(ctx, ResolverResourceIDs.DynamoDBUpdateResolverResourceID(def.name.value), operationRules.update, def)
this.protectDeleteMutation(ctx, ResolverResourceIDs.DynamoDBDeleteResolverResourceID(def.name.value), operationRules.delete, def)
this.protectGetQuery(ctx, ResolverResourceIDs.DynamoDBGetResolverResourceID(def.name.value), queryRules.get)
this.protectListQuery(ctx, ResolverResourceIDs.DynamoDBListResolverResourceID(def.name.value), queryRules.list)
this.protectConnections(ctx, def, operationRules.read)
this.protectQueries(ctx, def, operationRules.read)
// Check if subscriptions is enabled
const directiveArguments: ModelDirectiveArgs = getDirectiveArguments(modelDirective);
const subscription = this.validateSubscriptionLevel(directiveArguments);
if (subscription.level !== "OFF") {
this.protectOnCreateSubscription(ctx, operationRules.create, def,
subscription.level, subscription.onCreate);
this.protectOnUpdateSubscription(ctx, operationRules.update, def,
subscription.level, subscription.onUpdate);
this.protectOnDeleteSubscription(ctx, operationRules.delete, def,
subscription.level, subscription.onDelete);
}
}
function ensureCompositeKeySnippet(dir: DirectiveNode): string {
const args: KeyArguments = getDirectiveArguments(dir);
const argsPrefix = 'ctx.args.input';
if (args.fields.length > 2) {
const rangeKeyFields = args.fields.slice(1);
const condensedSortKey = condenseRangeKey(rangeKeyFields);
const dynamoDBFriendlySortKeyName = toCamelCase(rangeKeyFields.map(f => graphqlName(f)));
const condensedSortKeyValue = condenseRangeKey(
rangeKeyFields.map(keyField => `\${${argsPrefix}.${keyField}}`)
);
return print(compoundExpression([
ifElse(
raw(`$util.isNull($${ResourceConstants.SNIPPETS.DynamoDBNameOverrideMap})`),
set(ref(ResourceConstants.SNIPPETS.DynamoDBNameOverrideMap), obj({
[condensedSortKey]: str(dynamoDBFriendlySortKeyName)
})),
qref(`$${ResourceConstants.SNIPPETS.DynamoDBNameOverrideMap}.put("${condensedSortKey}", "${dynamoDBFriendlySortKeyName}")`)
),
function getPrimaryKey(obj: ObjectTypeDefinitionNode): DirectiveNode | undefined {
for (const directive of obj.directives) {
if (directive.name.value === 'key' && !getDirectiveArguments(directive).name) {
return directive;
}
}
}
function setQuerySnippet(definition: ObjectTypeDefinitionNode, directive: DirectiveNode, ctx: TransformerContext) {
const args: KeyArguments = getDirectiveArguments(directive);
const keys = args.fields;
const keyTypes = keys.map(k => {
const field = definition.fields.find(f => f.name.value === k);
return attributeTypeFromType(field.type, ctx);
})
return block(`Set query expression for @key`, [
set(ref(ResourceConstants.SNIPPETS.ModelQueryExpression), obj({})),
applyKeyExpressionForCompositeKey(keys, keyTypes, ResourceConstants.SNIPPETS.ModelQueryExpression)
])
}
private updateInputObjects = (definition: ObjectTypeDefinitionNode, directive: DirectiveNode, ctx: TransformerContext) => {
if (this.isPrimaryKey(directive)) {
const directiveArgs: KeyArguments = getDirectiveArguments(directive);
const createInput = ctx.getType(ModelResourceIDs.ModelCreateInputObjectName(definition.name.value)) as InputObjectTypeDefinitionNode;
if (createInput) {
ctx.putType(replaceCreateInput(definition, createInput, directiveArgs.fields));
}
const updateInput = ctx.getType(ModelResourceIDs.ModelUpdateInputObjectName(definition.name.value)) as InputObjectTypeDefinitionNode;
if (updateInput) {
ctx.putType(replaceUpdateInput(definition, updateInput, directiveArgs.fields));
}
const deleteInput = ctx.getType(ModelResourceIDs.ModelDeleteInputObjectName(definition.name.value)) as InputObjectTypeDefinitionNode;
if (deleteInput) {
ctx.putType(replaceDeleteInput(definition, deleteInput, directiveArgs.fields));
}
}
}