Skip to content
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
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

p5quared
Copy link
Contributor

@p5quared p5quared commented Sep 11, 2024

Handoff Notes:

Issue

If an input GQL schema has a required field that has a default e.g.

# Pseudo-GQL
name: String! @default(value: "anonymous")

the schema at the end of the transform still considers these fields to be NonNullable on CreateInput types.

# Pseudo-GQL
type CreateUserInput {
  ...fields,
  name: String! # <- This should be Nullable, since we have provided a default value
}

Reproduction

  1. Create CDK app
  2. Define bin/cdk-test.ts:
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { Stack } from 'aws-cdk-lib';
import { AmplifyGraphqlApi, AmplifyGraphqlDefinition } from '@aws-amplify/graphql-api-construct';

const app = new cdk.App();

const stack = new Stack(app, 'CoffeeStack');

new AmplifyGraphqlApi(stack, 'CoffeeQueue', {
	definition: AmplifyGraphqlDefinition.fromString(/* GraphQL */ `
    type CoffeeQueue @model {
      id: ID!
      name: String! @default(value: "anonymous")
      order: String
    }
  `),
	authorizationModes: {
		defaultAuthorizationMode: 'API_KEY',
		apiKeyConfig: {
			description: 'Api Key for public access',
			// @ts-ignore 
			expires: cdk.Duration.days(7)
		},
	},
});
  1. cdk synth or cdk deploy
  2. Outputs:
# model-schema.gql
type CoffeeQueue @model {
  id: ID!
  name: String! @default(value: "anonymous")
  order: String
}
type CoffeeQueue {
  id: ID!
  name: String!
  order: String
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}
...
input CreateCoffeeQueueInput {
  id: ID
  name: String! # <- This should be Nullable
  order: String
}

These inputs are reflected in the AppSync console, where you will not be able to run create mutation without providing a name in this case.

Q/A

  • Why do apps using defaults currently work?
    • Default values are provided in resolvers via VTL
  • Why has nobody noticed this?
  • Why wasn't this PR merged?
    • The blocker for this PR is that we aren't positive of the downstream effects i.e. whether or not this is a breaking change.
    • Personally, I feel fairly confident that this change would not break customer apps, but I was not able to confirm any/all downstream consumers of the output schema to guarantee this.

PR

Description of changes

Un-require required input fields that have @default applied. Also update the corresponding snapshot to document this behavior.

Prior to this change, a copy of the input object was being modified but ctx.output was not being updated with the new input.

CDK / CloudFormation Parameters Changed

n/a

Issue #, if available

aws-amplify/amplify-codegen#390

Description of how you validated changes

Fixed a test snapshot and edited code to pass it.

Checklist

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@p5quared p5quared added the bug Something isn't working label Sep 11, 2024
@p5quared p5quared requested a review from a team as a code owner September 11, 2024 20:39
@p5quared p5quared force-pushed the default-transformer-unrequire branch 2 times, most recently from 0d93765 to cbc28fd Compare September 12, 2024 17:02
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');

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@p5quared p5quared force-pushed the default-transformer-unrequire branch from 2a46b1d to e00975f Compare September 16, 2024 05:16

const validateNotCompositeKeyMember = (config: DefaultValueDirectiveConfiguration): void => {
const objectDirectives = config.object.fields?.flatMap((f) => f.directives);
const primaryKeyDirective = objectDirectives?.find((dir) => dir?.name.value === 'primaryKey');
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants