Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private addSubscriptionResolvers(ctx: TransformerContext, rules: AuthRule[],
parent: ObjectTypeDefinitionNode, level: string, fieldName: string) {
const resolverResourceId = ResolverResourceIDs.ResolverResourceID("Subscription", fieldName);
const resolver = this.resources.generateSubscriptionResolver(fieldName);
// If the data source does not exist it is created and added as a resource for PUBLIC && ON levels
const noneDS = ctx.getResource(ResourceConstants.RESOURCES.NoneDataSource)
// add the rules in the subscription resolver
if (!rules || rules.length === 0) {
return;
} else if (level === "PUBLIC") {
// set the resource with no auth logic
ctx.setResource(resolverResourceId, resolver);
} else {
// Break the rules out by strategy.
const staticGroupAuthorizationRules = this.getStaticGroupRules(rules);
const ownerAuthorizationRules = this.getOwnerRules(rules);
const staticGroupAuthorizationExpression = this.resources.staticGroupAuthorizationExpression(
field.name.value,
queryBodyArgsArray.filter(a => a.type.kind === Kind.NON_NULL_TYPE).map(a => a.name.value),
headers
);
ctx.setResource(putResourceID, putResolver);
}
break;
case 'DELETE':
const deleteResourceID = ResolverResourceIDs.ResolverResourceID(parent.name.value, field.name.value);
if (!ctx.getResource(deleteResourceID)) {
const deleteResolver = this.resources.makeDeleteResolver(baseURL, path, parent.name.value, field.name.value, headers);
ctx.setResource(deleteResourceID, deleteResolver);
}
break;
case 'PATCH':
const patchResourceID = ResolverResourceIDs.ResolverResourceID(parent.name.value, field.name.value);
if (!ctx.getResource(patchResourceID)) {
const patchResolver = this.resources.makePatchResolver(
baseURL,
path,
parent.name.value,
field.name.value,
queryBodyArgsArray.filter(a => a.type.kind === Kind.NON_NULL_TYPE).map(a => a.name.value),
headers
);
ctx.setResource(patchResourceID, patchResolver);
}
break;
default:
// nothing
}
if (!ctx.getResource(lambdaDataSourceKey)) {
ctx.setResource(lambdaDataSourceKey, this.datasource(name, region));
ctx.mapResourceToStack(FUNCTION_DIRECTIVE_STACK, lambdaDataSourceKey);
}
// Add function that invokes the lambda function
const functionConfigurationKey = FunctionResourceIDs.FunctionAppSyncFunctionConfigurationID(name, region);
if (!ctx.getResource(functionConfigurationKey)) {
ctx.setResource(functionConfigurationKey, this.function(name, region));
ctx.mapResourceToStack(FUNCTION_DIRECTIVE_STACK, functionConfigurationKey);
}
// Add resolver that invokes our function
const typeName = parent.name.value;
const fieldName = definition.name.value;
const resolverKey = ResolverResourceIDs.ResolverResourceID(typeName, fieldName);
const resolver = ctx.getResource(resolverKey);
if (!resolver) {
ctx.setResource(resolverKey, this.resolver(typeName, fieldName, name, region));
ctx.mapResourceToStack(FUNCTION_DIRECTIVE_STACK, resolverKey);
} else if (resolver.Properties.Kind === 'PIPELINE') {
ctx.setResource(
resolverKey,
this.appendFunctionToResolver(resolver, FunctionResourceIDs.FunctionAppSyncFunctionConfigurationID(name, region))
);
}
};
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)
}
}
validateKeyField(existingKeyField);
const tableLogicalId = ModelResourceIDs.ModelTableResourceID(parentTypeName)
const table = ctx.getResource(tableLogicalId) as Table
const sortField = associatedSortField ? { name: associatedSortFieldName, type: sortType } : null
const updated = this.resources.updateTableForConnection(table, connectionName, connectionAttributeName,
sortField)
ctx.setResource(tableLogicalId, updated)
const getResolver = this.resources.makeGetItemConnectionResolver(
parentTypeName,
fieldName,
relatedTypeName,
connectionAttributeName
)
ctx.setResource(ResolverResourceIDs.ResolverResourceID(parentTypeName, fieldName), getResolver)
// Update the create & update input objects for this
const createInputName = ModelResourceIDs.ModelCreateInputObjectName(parentTypeName)
const createInput = ctx.getType(createInputName) as InputObjectTypeDefinitionNode
if (createInput) {
const updated = updateCreateInputWithConnectionField(createInput, connectionAttributeName, leftConnectionIsNonNull)
ctx.putType(updated)
}
const updateInputName = ModelResourceIDs.ModelUpdateInputObjectName(parentTypeName)
const updateInput = ctx.getType(updateInputName) as InputObjectTypeDefinitionNode
if (updateInput) {
const updated = updateUpdateInputWithConnectionField(updateInput, connectionAttributeName)
ctx.putType(updated)
}
} else if (leftConnectionIsList) {
ctx.addInput(bodyInputObject);
newFieldArgsArray.push(makeHttpArgument('body', bodyInputObject, makeNonNull));
}
}
// build the payload
switch (method) {
case 'GET':
const getResourceID = ResolverResourceIDs.ResolverResourceID(parent.name.value, field.name.value);
if (!ctx.getResource(getResourceID)) {
const getResolver = this.resources.makeGetResolver(baseURL, path, parent.name.value, field.name.value, headers);
ctx.setResource(getResourceID, getResolver);
}
break;
case 'POST':
const postResourceID = ResolverResourceIDs.ResolverResourceID(parent.name.value, field.name.value);
if (!ctx.getResource(postResourceID)) {
const postResolver = this.resources.makePostResolver(
baseURL,
path,
parent.name.value,
field.name.value,
queryBodyArgsArray.filter(a => a.type.kind === Kind.NON_NULL_TYPE).map(a => a.name.value),
headers
);
ctx.setResource(postResourceID, postResolver);
}
break;
case 'PUT':
const putResourceID = ResolverResourceIDs.ResolverResourceID(parent.name.value, field.name.value);
if (!ctx.getResource(putResourceID)) {
const putResolver = this.resources.makePutResolver(
case 'POST':
const postResourceID = ResolverResourceIDs.ResolverResourceID(parent.name.value, field.name.value);
if (!ctx.getResource(postResourceID)) {
const postResolver = this.resources.makePostResolver(
baseURL,
path,
parent.name.value,
field.name.value,
queryBodyArgsArray.filter(a => a.type.kind === Kind.NON_NULL_TYPE).map(a => a.name.value),
headers
);
ctx.setResource(postResourceID, postResolver);
}
break;
case 'PUT':
const putResourceID = ResolverResourceIDs.ResolverResourceID(parent.name.value, field.name.value);
if (!ctx.getResource(putResourceID)) {
const putResolver = this.resources.makePutResolver(
baseURL,
path,
parent.name.value,
field.name.value,
queryBodyArgsArray.filter(a => a.type.kind === Kind.NON_NULL_TYPE).map(a => a.name.value),
headers
);
ctx.setResource(putResourceID, putResolver);
}
break;
case 'DELETE':
const deleteResourceID = ResolverResourceIDs.ResolverResourceID(parent.name.value, field.name.value);
if (!ctx.getResource(deleteResourceID)) {
const deleteResolver = this.resources.makeDeleteResolver(baseURL, path, parent.name.value, field.name.value, headers);