-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: un-require required input fields with @default #2867
Open
p5quared
wants to merge
6
commits into
aws-amplify:main
Choose a base branch
from
p5quared:default-transformer-unrequire
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5667c3a
test(graphql-default-value-transformer): fix snapshot
p5quared 4853cef
fix(graphql-default-value-transformer): modify inputObjects on the ctx
p5quared a753e80
test(graphql-default-value-transformer): @default is not to be used o…
p5quared 5faa1f6
fix(graphql-default-value-transformer): @default may not be applied t…
p5quared de7cc9e
test(graphql-default-value-transformer): @default cannot be used on c…
p5quared e00975f
fix(graphql-default-value-transformer): @default cannot be used on co…
p5quared File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import { | |
FieldDefinitionNode, | ||
InterfaceTypeDefinitionNode, | ||
Kind, | ||
ListValueNode, | ||
ObjectTypeDefinitionNode, | ||
StringValueNode, | ||
TypeNode, | ||
|
@@ -72,10 +73,34 @@ const validateDefaultValueType = (ctx: TransformerSchemaVisitStepContextProvider | |
} | ||
}; | ||
|
||
const validateNotPrimaryKey = (field: FieldDefinitionNode): void => { | ||
const isPrimaryKeyField = | ||
field.directives!.find((dir) => dir.name.value === 'primaryKey') || | ||
(getBaseType(field.type) === 'ID' && field.type.kind === Kind.NON_NULL_TYPE && field.name.value === 'id'); | ||
|
||
if (isPrimaryKeyField) { | ||
throw new InvalidDirectiveError('The @default directive may not be applied to primaryKey fields.'); | ||
} | ||
}; | ||
|
||
const validateNotCompositeKeyMember = (config: DefaultValueDirectiveConfiguration): void => { | ||
const objectDirectives = config.object.fields?.flatMap((f) => f.directives); | ||
const primaryKeyDirective = objectDirectives?.find((dir) => dir?.name.value === 'primaryKey'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can we refactor this check to see if the field is a primary key into it's own method that can be re-used between this method and the one above? |
||
if (primaryKeyDirective) { | ||
const sortKeyFields = primaryKeyDirective.arguments?.find((arg) => arg.name.value === 'sortKeyFields')?.value as ListValueNode; | ||
const sortKeys = sortKeyFields?.values as StringValueNode[]; | ||
if (sortKeys?.some((sortKey) => sortKey.value === config.field.name.value)) { | ||
throw new InvalidDirectiveError('The @default directive may not be applied to composite key member fields.'); | ||
} | ||
} | ||
}; | ||
|
||
const validate = (ctx: TransformerSchemaVisitStepContextProvider, config: DefaultValueDirectiveConfiguration): void => { | ||
validateModelDirective(config); | ||
validateFieldType(ctx, config.field.type); | ||
validateDirectiveArguments(config.directive); | ||
validateNotPrimaryKey(config.field); | ||
validateNotCompositeKeyMember(config); | ||
|
||
// Validate the default values only for the DynamoDB datasource. | ||
// For SQL, the database determines and sets the default value. We will not validate the value in transformers. | ||
|
@@ -123,6 +148,7 @@ export class DefaultValueTransformer extends TransformerPluginBase { | |
const input = InputObjectDefinitionWrapper.fromObject(name, config.object, ctx.inputDocument); | ||
const fieldWrapper = input.fields.find((f) => f.name === config.field.name.value); | ||
fieldWrapper?.makeNullable(); | ||
ctx.output.updateInput(input.serialize()); | ||
} | ||
} | ||
}; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is one more case to consider here which is when the
@default
is applied to fields part of a composite primary key. Gen2 doc or this Gen1 doc is more explicit about the schema with composite primary key.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a separate check scanning over
sortKeyFields
.