Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function validateKeyField(field: FieldDefinitionNode): void {
if (!field) {
return
}
const baseType = getBaseType(field.type);
const isAList = isListType(field.type)
// The only valid key fields are single String and ID fields.
if (
(baseType === 'ID' || baseType === 'String') &&
(!isAList)
) {
return;
}
throw new InvalidDirectiveError(`If you define a field and specify it as a 'keyField', it must be of type 'ID' or 'String'.`)
}
`Found one half of connection "${connectionName}" at ${parentTypeName}.${fieldName} but no related field on type ${relatedTypeName}`
)
}
connectionName = connectionName || `${parentTypeName}.${fieldName}`
const leftConnectionIsList = isListType(field.type)
const leftConnectionIsNonNull = isNonNullType(field.type)
const rightConnectionIsList = associatedConnectionField ? isListType(associatedConnectionField.type) : undefined
const rightConnectionIsNonNull = associatedConnectionField ? isNonNullType(associatedConnectionField.type) : undefined
let connectionAttributeName = getDirectiveArgument(directive)("keyField")
const associatedSortField = associatedSortFieldName &&
parent.fields.find((f: FieldDefinitionNode) => f.name.value === associatedSortFieldName)
if (associatedSortField) {
if (isListType(associatedSortField.type)) {
throw new InvalidDirectiveError(
`sortField "${associatedSortFieldName}" is a list. It should be a scalar.`
)
}
sortType = getBaseType(associatedSortField.type)
if (!isScalar(associatedSortField.type) || sortType === STANDARD_SCALARS.Boolean) {
throw new InvalidDirectiveError(
`sortField "${associatedSortFieldName}" is of type "${sortType}". ` +
`It should be a scalar that maps to a DynamoDB "String", "Number", or "Binary"`
)
}
}
// This grabs the definition of the sort field when it lives on the foreign model.
// We use this to configure key condition arguments for the resolver on the many side of the @connection.
const foreignAssociatedSortField = associatedSortFieldName &&
function validateKeyField(field: FieldDefinitionNode): void {
if (!field) {
return
}
const isNonNull = isNonNullType(field.type);
const isAList = isListType(field.type)
// The only valid key fields are single non-null fields.
if (!isAList && isNonNull) {
return;
}
throw new InvalidDirectiveError(`All fields used for a relation cannot be lists and must be of Non-Null types.`)
}
} else {
index = (tableResource.Properties.GlobalSecondaryIndexes ?
tableResource.Properties.GlobalSecondaryIndexes.find(GSI => GSI.IndexName === args.index) : null)
|| (tableResource.Properties.LocalSecondaryIndexes ?
tableResource.Properties.LocalSecondaryIndexes.find(LSI => LSI.IndexName === args.index) : null)
if (!index) {
throw new InvalidDirectiveError('Index ' + args.index + ' does not exist for model ' + relatedTypeName)
}
// Confirm that types of query fields match types of PK/SK of the index being queried.
checkFieldsAgainstIndex(parent.fields, relatedType.fields, args.fields, index.KeySchema);
}
// If the related type is not a list, the index has to be the default index and the fields provided must match the PK/SK of the index.
if (!isListType(field.type)) {
if (args.index !== 'default') {
throw new InvalidDirectiveError('Relation is to a single object but the query index is not the default.')
}
// Start with GetItem resolver for case where the connection is to a single object.
let getResolver = makeGetItemRelationResolver(
parentTypeName,
fieldName,
relatedTypeName,
args.fields,
tableResource.Properties.KeySchema
)
ctx.setResource(ResolverResourceIDs.ResolverResourceID(parentTypeName, fieldName), getResolver);
ctx.mapResourceToStack(relatedTypeName, ResolverResourceIDs.ResolverResourceID(parentTypeName, fieldName));
} else {
} else {
index = (tableResource.Properties.GlobalSecondaryIndexes ?
tableResource.Properties.GlobalSecondaryIndexes.find(GSI => GSI.IndexName === args.keyName) : null)
|| (tableResource.Properties.LocalSecondaryIndexes ?
tableResource.Properties.LocalSecondaryIndexes.find(LSI => LSI.IndexName === args.keyName) : null)
if (!index) {
throw new InvalidDirectiveError('Key ' + args.keyName + ' does not exist for model ' + relatedTypeName)
}
// Confirm that types of query fields match types of PK/SK of the index being queried.
checkFieldsAgainstIndex(parent.fields, relatedType.fields, args.fields, index.KeySchema);
}
// If the related type is not a list, the index has to be the default index and the fields provided must match the PK/SK of the index.
if (!isListType(field.type)) {
if (args.keyName !== 'default') {
throw new InvalidDirectiveError('Connection is to a single object but the keyName provided does not reference the default table.')
}
// Start with GetItem resolver for case where the connection is to a single object.
const getResolver = this.resources.makeGetItemNewConnectionResolver(
parentTypeName,
fieldName,
relatedTypeName,
args.fields,
tableResource.Properties.KeySchema
)
ctx.setResource(ResolverResourceIDs.ResolverResourceID(parentTypeName, fieldName), getResolver);
} else {
}
}
return false
}
)
if (connectionName && !associatedConnectionField) {
throw new InvalidDirectiveError(
`Found one half of connection "${connectionName}" at ${parentTypeName}.${fieldName} but no related field on type ${relatedTypeName}`
)
}
connectionName = connectionName || `${parentTypeName}.${fieldName}`
const leftConnectionIsList = isListType(field.type)
const leftConnectionIsNonNull = isNonNullType(field.type)
const rightConnectionIsList = associatedConnectionField ? isListType(associatedConnectionField.type) : undefined
const rightConnectionIsNonNull = associatedConnectionField ? isNonNullType(associatedConnectionField.type) : undefined
let connectionAttributeName = getDirectiveArgument(directive)("keyField")
const associatedSortField = associatedSortFieldName &&
parent.fields.find((f: FieldDefinitionNode) => f.name.value === associatedSortFieldName)
if (associatedSortField) {
if (isListType(associatedSortField.type)) {
throw new InvalidDirectiveError(
`sortField "${associatedSortFieldName}" is a list. It should be a scalar.`
)
}
sortType = getBaseType(associatedSortField.type)
if (!isScalar(associatedSortField.type) || sortType === STANDARD_SCALARS.Boolean) {
throw new InvalidDirectiveError(
`sortField "${associatedSortFieldName}" is of type "${sortType}". ` +
const fieldIsList = (fieldName: string) => {
const field = parent.fields.find(field => field.name.value === fieldName);
if (field) {
return isListType(field.type);
}
return false;
}
const ownerAuthorizationExpression = this.resources.ownerAuthorizationExpressionForCreateOperationsByField(
.map((enumField: FieldDefinitionNode) => {
const typeName = getBaseType(enumField.type);
const isList = isListType(enumField.type);
const name = isList
? ModelResourceIDs.ModelFilterListInputTypeName(typeName, !supportsConditions)
: ModelResourceIDs.ModelScalarFilterInputTypeName(typeName, !supportsConditions);
const fields = [];
fields.push({
kind: Kind.INPUT_VALUE_DEFINITION,
name: {
kind: 'Name',
value: 'eq',
},
type: isList ? makeListType(makeNamedType(typeName)) : makeNamedType(typeName),
directives: [],
});
fields.push({