@aws-amplify/graphql-default-value-transformer
Advanced tools
Comparing version 0.2.0-graphql-vnext-dev-preview.1 to 0.2.0
@@ -6,15 +6,7 @@ # Change Log | ||
# [0.2.0-graphql-vnext-dev-preview.1](https://github.com/aws-amplify/amplify-cli/compare/@aws-amplify/graphql-default-value-transformer@0.2.0-graphql-vnext-dev-preview.0...@aws-amplify/graphql-default-value-transformer@0.2.0-graphql-vnext-dev-preview.1) (2021-10-07) | ||
# 0.2.0 (2021-10-10) | ||
**Note:** Version bump only for package @aws-amplify/graphql-default-value-transformer | ||
# 0.2.0-graphql-vnext-dev-preview.0 (2021-09-29) | ||
### Features | ||
* **graphql-default-value-transformer:** implemented default value directive ([6688572](https://github.com/aws-amplify/amplify-cli/commit/66885720fed4a20e9d300d8cfd7aab01ed458097)) | ||
* **graphql-default-value-transformer:** implemented default value directive ([#8291](https://github.com/aws-amplify/amplify-cli/issues/8291)) ([130aba1](https://github.com/aws-amplify/amplify-cli/commit/130aba1dda122b3289270d1a711da6e0326ecf90)) |
@@ -9,5 +9,4 @@ import { TransformerPluginBase } from '@aws-amplify/graphql-transformer-core'; | ||
generateResolvers: (ctx: TransformerContextProvider) => void; | ||
private storeAsString; | ||
private createVTLSnippet; | ||
private augmentResolver; | ||
private makeDefaultValueSnippet; | ||
private updateResolverWithDefaultValues; | ||
private getResolverObject; | ||
@@ -14,0 +13,0 @@ private addSnippetToResolverSlot; |
@@ -13,3 +13,3 @@ "use strict"; | ||
`; | ||
const nonStringStorageTypes = ['Int', 'Float', 'Boolean', 'AWSTimestamp', 'AWSJSON']; | ||
const nonStringTypes = ['Int', 'Float', 'Boolean', 'AWSTimestamp', 'AWSJSON']; | ||
class DefaultValueTransformer extends graphql_transformer_core_1.TransformerPluginBase { | ||
@@ -40,13 +40,12 @@ constructor() { | ||
const defaultValue = defaultValueArgumentValueNode.value; | ||
snippets.push(this.createVTLSnippet(fieldName, defaultValue, this.storeAsString(graphql_transformer_common_1.getBaseType(config.field.type)))); | ||
snippets.push(this.makeDefaultValueSnippet(fieldName, defaultValue, !nonStringTypes.includes(graphql_transformer_common_1.getBaseType(config.field.type)))); | ||
} | ||
this.augmentResolver(context, `create${typeName}`, snippets); | ||
this.augmentResolver(context, `update${typeName}`, snippets); | ||
this.updateResolverWithDefaultValues(context, `create${typeName}`, snippets); | ||
this.updateResolverWithDefaultValues(context, `update${typeName}`, snippets); | ||
} | ||
}; | ||
this.storeAsString = (typeName) => !nonStringStorageTypes.includes(typeName); | ||
this.createVTLSnippet = (fieldName, defaultValue, isString) => { | ||
this.makeDefaultValueSnippet = (fieldName, defaultValue, isString) => { | ||
return graphql_mapping_template_1.printBlock(`Setting "${fieldName}" to default value of "${defaultValue}"`)(graphql_mapping_template_1.qref(graphql_mapping_template_1.methodCall(graphql_mapping_template_1.ref('ctx.stash.defaultValues.put'), graphql_mapping_template_1.str(fieldName), isString ? graphql_mapping_template_1.str(defaultValue) : graphql_mapping_template_1.raw(defaultValue)))); | ||
}; | ||
this.augmentResolver = (ctx, resolverLogicalId, snippets) => { | ||
this.updateResolverWithDefaultValues = (ctx, resolverLogicalId, snippets) => { | ||
const resolver = this.getResolverObject(ctx, resolverLogicalId); | ||
@@ -73,8 +72,8 @@ if (resolver) { | ||
function validate(ctx, config) { | ||
assertModelDirective(config); | ||
assertCompatibleFieldType(ctx, config.field.type); | ||
assertValidDirectiveArguments(config.directive); | ||
assertFieldTypeAndDefaultValueTypeMatch(ctx, config); | ||
validateModelDirective(config); | ||
validateFieldType(ctx, config.field.type); | ||
validateDirectiveArguments(config.directive); | ||
validateDefaultValueType(ctx, config); | ||
} | ||
function assertCompatibleFieldType(ctx, type) { | ||
function validateFieldType(ctx, type) { | ||
const enums = ctx.output.getTypeDefinitionsOfKind(graphql_1.Kind.ENUM_TYPE_DEFINITION); | ||
@@ -88,9 +87,9 @@ if (graphql_transformer_common_1.isListType(type) || !graphql_transformer_common_1.isScalarOrEnum(type, enums)) { | ||
} | ||
function assertValidDirectiveArguments(directive) { | ||
function validateDirectiveArguments(directive) { | ||
if (directive.arguments.length === 0) | ||
throw new graphql_transformer_core_1.InvalidDirectiveError('Directive for @default must declare a value property'); | ||
throw new graphql_transformer_core_1.InvalidDirectiveError('The @default directive must have a value property'); | ||
if (directive.arguments.length > 1) | ||
throw new graphql_transformer_core_1.InvalidDirectiveError('Directive for @default only takes a value property'); | ||
throw new graphql_transformer_core_1.InvalidDirectiveError('The @default directive only takes a value property'); | ||
} | ||
function assertModelDirective(config) { | ||
function validateModelDirective(config) { | ||
const modelDirective = config.object.directives.find(dir => dir.name.value === 'model'); | ||
@@ -101,3 +100,3 @@ if (!modelDirective) { | ||
} | ||
function assertFieldTypeAndDefaultValueTypeMatch(ctx, config) { | ||
function validateDefaultValueType(ctx, config) { | ||
if (config.value === null) { | ||
@@ -104,0 +103,0 @@ throw new graphql_transformer_core_1.InvalidDirectiveError('The @default directive does not support null values.'); |
{ | ||
"name": "@aws-amplify/graphql-default-value-transformer", | ||
"version": "0.2.0-graphql-vnext-dev-preview.1", | ||
"version": "0.2.0", | ||
"description": "Amplify GraphQL default value transformer", | ||
@@ -31,7 +31,7 @@ "repository": { | ||
"dependencies": { | ||
"@aws-amplify/graphql-transformer-core": "0.10.0-graphql-vnext-dev-preview.1", | ||
"@aws-amplify/graphql-transformer-interfaces": "1.10.0-graphql-vnext-dev-preview.0", | ||
"@aws-amplify/graphql-transformer-core": "0.9.2", | ||
"@aws-amplify/graphql-transformer-interfaces": "1.10.0", | ||
"graphql": "^14.5.8", | ||
"graphql-mapping-template": "4.19.0-graphql-vnext-dev-preview.0", | ||
"graphql-transformer-common": "4.19.10-graphql-vnext-dev-preview.0", | ||
"graphql-mapping-template": "4.18.3", | ||
"graphql-transformer-common": "4.19.10", | ||
"libphonenumber-js": "^1.7.31" | ||
@@ -58,3 +58,3 @@ }, | ||
}, | ||
"gitHead": "0dc48ed1c4887af360803854e2f90556a10aee39" | ||
"gitHead": "7309d96240bc995699c2514320acc1e3f97ad73f" | ||
} |
@@ -27,3 +27,3 @@ import { DirectiveWrapper, InvalidDirectiveError, MappingTemplate, TransformerPluginBase } from '@aws-amplify/graphql-transformer-core'; | ||
const nonStringStorageTypes = ['Int', 'Float', 'Boolean', 'AWSTimestamp', 'AWSJSON']; | ||
const nonStringTypes = ['Int', 'Float', 'Boolean', 'AWSTimestamp', 'AWSJSON']; | ||
@@ -67,13 +67,11 @@ export class DefaultValueTransformer extends TransformerPluginBase { | ||
const defaultValue = defaultValueArgumentValueNode.value; | ||
snippets.push(this.createVTLSnippet(fieldName, defaultValue, this.storeAsString(getBaseType(config.field.type)))); | ||
snippets.push(this.makeDefaultValueSnippet(fieldName, defaultValue, !nonStringTypes.includes(getBaseType(config.field.type)))); | ||
} | ||
this.augmentResolver(context, `create${typeName}`, snippets); | ||
this.augmentResolver(context, `update${typeName}`, snippets); | ||
this.updateResolverWithDefaultValues(context, `create${typeName}`, snippets); | ||
this.updateResolverWithDefaultValues(context, `update${typeName}`, snippets); | ||
} | ||
}; | ||
private storeAsString = (typeName: string) => !nonStringStorageTypes.includes(typeName); | ||
private createVTLSnippet = (fieldName: string, defaultValue: string, isString: boolean): string => { | ||
private makeDefaultValueSnippet = (fieldName: string, defaultValue: string, isString: boolean): string => { | ||
return printBlock(`Setting "${fieldName}" to default value of "${defaultValue}"`)( | ||
@@ -84,3 +82,3 @@ qref(methodCall(ref('ctx.stash.defaultValues.put'), str(fieldName), isString ? str(defaultValue) : raw(defaultValue))), | ||
private augmentResolver = (ctx: TransformerContextProvider, resolverLogicalId: string, snippets: string[]): void => { | ||
private updateResolverWithDefaultValues = (ctx: TransformerContextProvider, resolverLogicalId: string, snippets: string[]): void => { | ||
const resolver = this.getResolverObject(ctx, resolverLogicalId); | ||
@@ -115,9 +113,9 @@ if (resolver) { | ||
function validate(ctx: TransformerSchemaVisitStepContextProvider, config: DefaultValueDirectiveConfiguration): void { | ||
assertModelDirective(config); | ||
assertCompatibleFieldType(ctx, config.field.type); | ||
assertValidDirectiveArguments(config.directive); | ||
assertFieldTypeAndDefaultValueTypeMatch(ctx, config); | ||
validateModelDirective(config); | ||
validateFieldType(ctx, config.field.type); | ||
validateDirectiveArguments(config.directive); | ||
validateDefaultValueType(ctx, config); | ||
} | ||
function assertCompatibleFieldType(ctx: TransformerSchemaVisitStepContextProvider, type: TypeNode): void { | ||
function validateFieldType(ctx: TransformerSchemaVisitStepContextProvider, type: TypeNode): void { | ||
const enums = ctx.output.getTypeDefinitionsOfKind(Kind.ENUM_TYPE_DEFINITION) as EnumTypeDefinitionNode[]; | ||
@@ -133,8 +131,8 @@ if (isListType(type) || !isScalarOrEnum(type, enums)) { | ||
function assertValidDirectiveArguments(directive: DirectiveNode): void { | ||
if (directive.arguments!.length === 0) throw new InvalidDirectiveError('Directive for @default must declare a value property'); | ||
if (directive.arguments!.length > 1) throw new InvalidDirectiveError('Directive for @default only takes a value property'); | ||
function validateDirectiveArguments(directive: DirectiveNode): void { | ||
if (directive.arguments!.length === 0) throw new InvalidDirectiveError('The @default directive must have a value property'); | ||
if (directive.arguments!.length > 1) throw new InvalidDirectiveError('The @default directive only takes a value property'); | ||
} | ||
function assertModelDirective(config: DefaultValueDirectiveConfiguration): void { | ||
function validateModelDirective(config: DefaultValueDirectiveConfiguration): void { | ||
const modelDirective = config.object.directives!.find(dir => dir.name.value === 'model'); | ||
@@ -146,6 +144,3 @@ if (!modelDirective) { | ||
function assertFieldTypeAndDefaultValueTypeMatch( | ||
ctx: TransformerSchemaVisitStepContextProvider, | ||
config: DefaultValueDirectiveConfiguration, | ||
): void { | ||
function validateDefaultValueType(ctx: TransformerSchemaVisitStepContextProvider, config: DefaultValueDirectiveConfiguration): void { | ||
// if field type is non-nullable, ensure value is not null | ||
@@ -152,0 +147,0 @@ if (config.value === null) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
209018
814
+ Added@aws-amplify/graphql-transformer-core@0.9.2(transitive)
+ Added@aws-amplify/graphql-transformer-interfaces@1.10.0(transitive)
+ Addedgraphql-mapping-template@4.18.3(transitive)
+ Addedgraphql-transformer-common@4.19.10(transitive)
- Removed@aws-amplify/graphql-transformer-core@0.10.0-graphql-vnext-dev-preview.1(transitive)
- Removed@aws-amplify/graphql-transformer-interfaces@1.10.0-graphql-vnext-dev-preview.0(transitive)
- Removedgraphql-mapping-template@4.19.0-graphql-vnext-dev-preview.0(transitive)
- Removedgraphql-transformer-common@4.19.10-graphql-vnext-dev-preview.0(transitive)