Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (versionFieldImpl) {
const baseType = getBaseType(versionFieldImpl.type);
if (baseType === 'Int' || baseType === 'BigInt') {
// ok.
if (versionFieldImpl.type.kind !== Kind.NON_NULL_TYPE) {
updatedField = {
...updatedField,
type: makeNonNullType(versionFieldImpl.type),
};
updatedFields = updatedFields.map(f => (f.name.value === versionField ? updatedField : f));
}
} else {
throw new TransformerContractError(`The versionField "${versionField}" is required to be of type "Int" or "BigInt".`);
}
} else {
updatedField = makeField(versionField, [], makeNonNullType(makeNamedType('Int')));
updatedFields = [...updatedFields, updatedField];
}
const updatedType = {
...type,
fields: updatedFields,
};
ctx.putType(updatedType);
}
}
}
export function updateCreateInputWithConnectionField(
input: InputObjectTypeDefinitionNode,
connectionFieldName: string,
nonNull: boolean = false
): InputObjectTypeDefinitionNode {
const keyFieldExists = Boolean(input.fields.find(f => f.name.value === connectionFieldName));
// If the key field already exists then do not change the input.
// The @connection field will validate that the key field is valid.
if (keyFieldExists) {
return input;
}
const updatedFields = [
...input.fields,
makeInputValueDefinition(connectionFieldName, nonNull ? makeNonNullType(makeNamedType('ID')) : makeNamedType('ID')),
];
return {
...input,
fields: updatedFields,
};
}
private enforceVersionedFieldOnType(ctx: TransformerContext, typeName: string, versionField: string) {
const type = ctx.getType(typeName);
if (type && type.kind === Kind.OBJECT_TYPE_DEFINITION) {
let updatedFields = type.fields;
const versionFieldImpl = type.fields.find(f => f.name.value === versionField);
let updatedField = versionFieldImpl;
if (versionFieldImpl) {
const baseType = getBaseType(versionFieldImpl.type);
if (baseType === 'Int' || baseType === 'BigInt') {
// ok.
if (versionFieldImpl.type.kind !== Kind.NON_NULL_TYPE) {
updatedField = {
...updatedField,
type: makeNonNullType(versionFieldImpl.type),
};
updatedFields = updatedFields.map(f => (f.name.value === versionField ? updatedField : f));
}
} else {
throw new TransformerContractError(`The versionField "${versionField}" is required to be of type "Int" or "BigInt".`);
}
} else {
updatedField = makeField(versionField, [], makeNonNullType(makeNamedType('Int')));
updatedFields = [...updatedFields, updatedField];
}
const updatedType = {
...type,
fields: updatedFields,
};
ctx.putType(updatedType);
}
fields: input.fields.reduce((acc, f) => {
// If the field is a key, make it non-null.
if (keyFields.find(k => k === f.name.value)) {
return [...acc, makeInputValueDefinition(f.name.value, makeNonNullType(makeNamedType(getBaseType(f.type))))];
}
return [...acc, f];
}, [])
};
export function makeHttpArgument(name: string, inputType: InputObjectTypeDefinitionNode, makeNonNull: boolean): InputValueDefinitionNode {
// the URL params type that we create will need to be non-null, so build in some flexibility here
const type = makeNonNull ? makeNonNullType(makeNamedType(inputType.name.value)) : makeNamedType(inputType.name.value);
return makeInputValueDefinition(name, type);
}
private addSubscriptionOwnerArgument(ctx: TransformerContext, resolver: Resolver, makeNonNull: boolean = false) {
let subscription = ctx.getSubscription();
let createField: FieldDefinitionNode = subscription.fields.find(
field => field.name.value === resolver.Properties.FieldName,
) as FieldDefinitionNode;
const nameNode: any = makeNonNull ? makeNonNullType(makeNamedType('String')) : makeNamedType('String');
const createArguments = [makeInputValueDefinition(DEFAULT_OWNER_FIELD, nameNode)];
createField = {
...createField,
arguments: createArguments,
};
subscription = {
...subscription,
fields: subscription.fields.map(
field => field.name.value === resolver.Properties.FieldName ? createField : field,
),
};
ctx.putType(subscription);
}
private addVersionedInputToInput(ctx: TransformerContext, inputName: string, versionInput: string) {
const input = ctx.getType(inputName);
if (input && input.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION) {
const updatedFields = [...input.fields, makeInputValueDefinition(versionInput, makeNonNullType(makeNamedType('Int')))];
const updatedInput = {
...input,
fields: updatedFields,
};
ctx.putType(updatedInput);
}
}
makeNamedType(def.name.value)
));
}
if (shouldMakeUpdate) {
const updateInput = makeUpdateInputObject(def, nonModelArray, ctx)
if (!ctx.getType(updateInput.name.value)) {
ctx.addInput(updateInput)
}
const updateResolver = this.resources.makeUpdateResolver(def.name.value, updateFieldNameOverride)
const resourceId = ResolverResourceIDs.DynamoDBUpdateResolverResourceID(typeName);
ctx.setResource(resourceId, updateResolver);
ctx.mapResourceToStack(typeName, resourceId);
mutationFields.push(makeField(
updateResolver.Properties.FieldName,
[makeInputValueDefinition('input', makeNonNullType(makeNamedType(updateInput.name.value)))],
makeNamedType(def.name.value)
));
}
if (shouldMakeDelete) {
const deleteInput = makeDeleteInputObject(def)
if (!ctx.getType(deleteInput.name.value)) {
ctx.addInput(deleteInput)
}
const deleteResolver = this.resources.makeDeleteResolver(def.name.value, deleteFieldNameOverride)
const resourceId = ResolverResourceIDs.DynamoDBDeleteResolverResourceID(typeName);
ctx.setResource(resourceId, deleteResolver);
ctx.mapResourceToStack(typeName, resourceId);
mutationFields.push(makeField(
deleteResolver.Properties.FieldName,
[makeInputValueDefinition('input', makeNonNullType(makeNamedType(deleteInput.name.value)))],