Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.map((field: FieldDefinitionNode) => ({
kind: Kind.INPUT_VALUE_DEFINITION,
name: field.name,
type: makeNamedType(SearchableResourceIDs.SearchableFilterInputTypeName(getBaseType(field.type))),
// TODO: Service does not support new style descriptions so wait.
// description: field.description,
directives: [],
}));
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'.`)
}
export function getNonModelObjectArray(
obj: ObjectTypeDefinitionNode,
ctx: TransformerContext,
pMap: Map
): ObjectTypeDefinitionNode[] {
// loop over all fields in the object, picking out all nonscalars that are not @model types
for (const field of obj.fields) {
if (!isScalar(field.type)) {
const def = ctx.getType(getBaseType(field.type));
if (
def &&
def.kind === Kind.OBJECT_TYPE_DEFINITION &&
!def.directives.find(e => e.name.value === 'model') &&
pMap.get(def.name.value) === undefined
) {
// recursively find any non @model types referenced by the current
// non @model type
pMap.set(def.name.value, def);
getNonModelObjectArray(def, ctx, pMap);
}
}
}
return Array.from(pMap.values());
function checkFieldsAgainstIndex(parentFields: ReadonlyArray,
relatedTypeFields: ReadonlyArray,
inputFieldNames: string[],
keySchema: KeySchema[]): void {
const hashAttributeName = keySchema[0].AttributeName;
const tablePKType = relatedTypeFields.find(f => f.name.value === hashAttributeName).type;
const queryPKType = parentFields.find(f => f.name.value === inputFieldNames[0]).type;
const numFields = inputFieldNames.length;
if (getBaseType(tablePKType) !== getBaseType(queryPKType)) {
throw new InvalidDirectiveError(inputFieldNames[0] + ' field is not of type ' + getBaseType(tablePKType))
}
if (numFields > keySchema.length && keySchema.length !== 2) {
throw new InvalidDirectiveError('Too many fields passed in to @connection directive.')
}
if (numFields === 2) {
const sortAttributeName = keySchema[1].AttributeName;
const tableSKType = relatedTypeFields.find(f => f.name.value === sortAttributeName).type;
const querySKType = parentFields.find(f => f.name.value === inputFieldNames[1]).type;
if (getBaseType(tableSKType) !== getBaseType(querySKType)) {
throw new InvalidDirectiveError(inputFieldNames[1] + ' field is not of type ' + getBaseType(tableSKType))
}
} else if (numFields > 2) {
const tableSortFields = keySchema[1].AttributeName.split(ModelResourceIDs.ModelCompositeKeySeparator());
function checkFieldsAgainstIndex(parentFields: ReadonlyArray,
relatedTypeFields: ReadonlyArray,
inputFieldNames: string[],
keySchema: KeySchema[]): void {
const hashAttributeName = keySchema[0].AttributeName;
const tablePKType = relatedTypeFields.find(f => f.name.value === hashAttributeName).type;
const queryPKType = parentFields.find(f => f.name.value === inputFieldNames[0]).type;
const numFields = inputFieldNames.length;
if (getBaseType(tablePKType) !== getBaseType(queryPKType)) {
throw new InvalidDirectiveError(inputFieldNames[0] + ' field is not of type ' + getBaseType(tablePKType))
}
if (numFields > keySchema.length && keySchema.length !== 2) {
throw new InvalidDirectiveError('Too many fields passed in to @connection directive.')
}
if (numFields === 2) {
const sortAttributeName = keySchema[1].AttributeName;
const tableSKType = relatedTypeFields.find(f => f.name.value === sortAttributeName).type;
const querySKType = parentFields.find(f => f.name.value === inputFieldNames[1]).type;
if (getBaseType(tableSKType) !== getBaseType(querySKType)) {
throw new InvalidDirectiveError(inputFieldNames[1] + ' field is not of type ' + getBaseType(tableSKType))
}
} else if (numFields > 2) {
const tableSortFields = keySchema[1].AttributeName.split(ModelResourceIDs.ModelCompositeKeySeparator());
const tableSortKeyTypes = tableSortFields.map(name => relatedTypeFields.find(f => f.name.value === name).type);
(acc: {}, arg: FieldDefinitionNode) => ({
...acc,
[arg.name.value]: getBaseType(arg.type),
}),
{}
function addSimpleSortKey(ctx: TransformerContext, definition: ObjectTypeDefinitionNode, args: KeyArguments, elems: InputValueDefinitionNode[]): InputValueDefinitionNode[] {
let sortKeyName = args.fields[1];
const sortField = definition.fields.find(field => field.name.value === sortKeyName);
const baseType = getBaseType(sortField.type);
const resolvedTypeIfEnum = ctx.getType(baseType) as EnumTypeDefinitionNode ? 'String' : undefined;
const resolvedType = resolvedTypeIfEnum ? resolvedTypeIfEnum : baseType;
const hashKey = makeInputValueDefinition(sortKeyName, makeNamedType(ModelResourceIDs.ModelKeyConditionInputTypeName(resolvedType)));
return [hashKey, ...elems];
}
function addCompositeSortKey(definition: ObjectTypeDefinitionNode, args: KeyArguments, elems: InputValueDefinitionNode[]): InputValueDefinitionNode[] {
let queryResolver = makeQueryRelationResolver(
parentTypeName,
fieldName,
relatedType,
args.fields,
keySchema,
index ? index.IndexName : null
)
ctx.setResource(ResolverResourceIDs.ResolverResourceID(parentTypeName, fieldName), queryResolver);
ctx.mapResourceToStack(relatedTypeName, ResolverResourceIDs.ResolverResourceID(parentTypeName, fieldName));
let sortKeyType = relatedType.fields.find(f => f.name.value === keySchema[1].AttributeName).type
const sortKeyInfo = keySchema[1] ?
{ fieldName : keySchema[1].AttributeName, typeName : getBaseType(sortKeyType)} :
undefined;
this.extendTypeWithConnection(ctx, parent, field, relatedType, sortKeyInfo);
}
}
let tablePKType = relatedTypeFields.find(f => f.name.value === hashAttributeName).type;
let queryPKType = parentFields.find(f => f.name.value === inputFieldNames[0]).type;
let numFields = inputFieldNames.length;
if (getBaseType(tablePKType) !== getBaseType(queryPKType)) {
throw new InvalidDirectiveError(inputFieldNames[0] + ' field is not of type ' + getBaseType(tablePKType))
}
if (numFields > keySchema.length) {
throw new InvalidDirectiveError('Too many fields passed in for relation.')
}
if (numFields > 1) {
let sortAttributeName = keySchema[1].AttributeName;
let tableSKType = relatedTypeFields.find(f => f.name.value === sortAttributeName).type;
let querySKType = parentFields.find(f => f.name.value === inputFieldNames[1]).type;
if (getBaseType(tableSKType) !== getBaseType(querySKType)) {
throw new InvalidDirectiveError(inputFieldNames[1] + ' field is not of type ' + getBaseType(tableSKType))
}
}
}