Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
}
}
// 3. Check that fields exists and are valid key types.
const fieldMap = new Map();
for (const field of definition.fields) {
fieldMap.set(field.name.value, field);
}
for (const fieldName of directiveArgs.fields) {
if (!fieldMap.has(fieldName)) {
const checkedKeyName = directiveArgs.name ? directiveArgs.name : "";
throw new InvalidDirectiveError(`You cannot specify a non-existant field '${fieldName}' in @key '${checkedKeyName}' on type '${definition.name.value}'.`);
} else {
const existingField = fieldMap.get(fieldName);
const ddbKeyType = attributeTypeFromType(existingField.type, ctx);
if (this.isPrimaryKey(directive) && !isNonNullType(existingField.type)) {
throw new InvalidDirectiveError(`The primary @key on type '${definition.name.value}' must reference non-null fields.`);
} else if (ddbKeyType !== 'S' && ddbKeyType !== 'N' && ddbKeyType !== 'B') {
throw new InvalidDirectiveError(`A @key on type '${definition.name.value}' cannot reference non-scalar field ${fieldName}.`);
}
}
}
}
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.`)
}
function validateKeyFieldNewConnection(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 connection cannot be lists and must be of Non-Null types.`)
}
export function expectNonNullFields(type: ObjectTypeDefinitionNode, fields: string[]) {
for (const fieldName of fields) {
const foundField = type.fields.find((f: FieldDefinitionNode) => f.name.value === fieldName);
expect(foundField).toBeDefined();
expect(isNonNullType(foundField.type)).toBeTruthy();
}
}
export function expectNullableFields(type: ObjectTypeDefinitionNode, fields: string[]) {
for (const fieldName of fields) {
const foundField = type.fields.find((f: FieldDefinitionNode) => f.name.value === fieldName);
expect(foundField).toBeDefined();
expect(isNonNullType(foundField.type)).toBeFalsy();
}
}
export function expectNullableInputValues(type: InputObjectTypeDefinitionNode, fields: string[]) {
for (const fieldName of fields) {
const foundField = type.fields.find((f: InputValueDefinitionNode) => f.name.value === fieldName);
expect(foundField).toBeDefined();
expect(isNonNullType(foundField.type)).toBeFalsy();
}
}
return true
}
}
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(
}
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}". ` +
`It should be a scalar that maps to a DynamoDB "String", "Number", or "Binary"`