aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
89 stars 79 forks source link

DataStore does not support 1 to 1 connection with both sides of connection as optional field #1419

Closed parvusville closed 1 month ago

parvusville commented 1 year ago

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

v14.21.3

Amplify CLI Version

11.0.5

What operating system are you using?

Pop Os

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

no.

Describe the bug

Our CI/CD fails with message DataStore does not support 1 to 1 connection with both sides of connection as optional field: AdditionalServicePricing.pricing. I have seen this error before too in CI/CD, but that time I also had a issue with package-lock and package.json being in desync, and after fixing that issue this Datastore message also stopped appearing. This message appears also if I run amplify codegen models. We don't use DataStore in this project.

We had many other issues affecting CI/CD, and after fixing them this error started to appear. We are using Transformer V1.

Looking at our build history, these have been failure reasons since the first error to now:

  1. New environment variable not set *Parameters: [xxx] must have values
  2. Non-model type pushed with @auth rules *Type ExecutionResult not found
  3. Non-model type pushed with @auth rules *Type CreatePricingsFromCsvResult not found
  4. Non-model type pushed with @auth rules *Type ExecutionResult not found
  5. DataStore does not support 1 to 1 connection with both sides of connection as optional field: AdditionalServicePricing.pricing

Below is the GraphQL schema. If I make AdditionalServicePricing pricing field non-mandatory, build in CI/CD goes through fine. Why does this happen, and why does this error start to show up seemingly out of nowhere? Also local amplify push goes through just fine also with pricing set to mandatory.

``` type Pricing @model @auth( rules: [ { allow: private, provider: iam } { allow: groups, groupsField: "group" } ] ) @key( name: "pricingByGroup" fields: ["group"] queryField: "pricingByGroup" ) { id: ID! name: String! group: String! elements: [PricingElement!]! useNextDayPrice: Boolean tax: Int _removed: String } type AdditionalServicePricing @model @auth( rules: [ { allow: private, provider: iam } { allow: groups, groupsField: "group" } ] ) @key(name: "byVehicle", fields: ["vehicleId"]) @key(name: "byCategory", fields: ["categoryId"]) { id: ID! group: String! key: String! vehicleId: ID categoryId: ID pricingId: ID! pricing: Pricing! @connection(fields: ["pricingId"]) } ```

Expected behavior

The build should succeed with the same schema it has succeeded previously with. Or if it should not, why it has succeeded until now?

Reproduction steps

  1. Use the attached Schema
  2. Push and observe Amplify hosting CI/CD fail.

Project Identifier

f59e77335650fce92677ec2970143a9b

Log output

``` # Put your logs below this line ```

Additional information

CICD goes fine, if I make the @connection field in question optional.

Before submitting, please confirm:

oe-bayram commented 1 year ago

Hi, could you kindly update us on the current status of this issue? Our production system is directly affected, and it's causing some roadblocks in our development process. Any information would be greatly appreciated.

oe-bayram commented 1 year ago

Employing package version overrides within the Amplify Console build settings to revert to an older Amplify CLI version (10.5.2) effectively addressed our issue as an interim solution.

jvanoers commented 1 year ago

Also jumping in with our experiences with this -- we've had this issue on our production build systems - an app made long ago with amplify.

Experienced this regression twice with different amplify-cli versions: DataStore does not support 1 to 1 connection with both sides of connection as optional field

Once with CLI v11.0.4 which was subsequently pulled as a bad release -- maybe there's a related problem here, which shows other issues relating to the broken 11.0.4 build: https://github.com/aws-amplify/amplify-category-api/issues/1401

After that release got pulled, CLI v11.0.5 worked for us, now in the latest version v11.1.1 we're seeing this issue again

I suspect this regression was introduced in the 11.0.4 build, and some spin on that broken code has been rereleased since v11.0.5 - causing this issue.

Solution: Change Build Settings -> Amplify CLI Version -> v11.0.5 which was the last working version

chrisbonifacio commented 1 year ago

Hi @parvusville 👋 sorry for the delay. We've been trying to reproduce the error message you're running into and so far we've run into the issue of deploying non-model type with auth directives. Did you eventually add the model to all of your types to get past these errors?

@jvanoers can you run amplify diagnose --send-report and share your project identifier? That will allow us to try and deploy the same exact schema that reproduces the issue.

parvusville commented 1 year ago

Hello @chrisbonifacio . Yes, the DataStore error mentioned in the post was the last error left. I ended up changing pricing: Pricing! @connection(fields: ["pricingId"]) to pricing: Pricing @connection(fields: ["pricingId"]) on AdditionalServicePricing to be able to move on.

oe-bayram commented 1 year ago

Are there any recent updates on this issue? I need the latest Amplify CLI version which leads to this issue in our project.

cunneen commented 1 year ago

We're still stuck on 10.8.1 , still waiting for this to be fixed.

phani-srikar commented 11 months ago

Hi @parvusville, I've root caused the error to be originating from connection validations in modelgen plugin. However since you mentioned you don't use DataStore in your project, I am not sure why the models generation is invoked in the first place. Can you confirm a couple of things:

parvusville commented 11 months ago

Hello @phani-srikar . cli-inputs.json does not exist for my GraphQL API. I am using Amplify hosting for CI/CD.

cunneen commented 8 months ago

@phani-srikar we don't have a amplify/backend/api/<api_name>/cli-inputs.json file at all. We are using amplify hosting. We don't use DataStore.

The call to the code to which you referred seems to have been introduced to @aws-amplify/cli in the v11.0.0 release, via amplify-codegen PR 476, which tallies with the behaviour we've seen across the versions (and explains why we are STILL stuck on @aws-amplify/cli v10.8.1 , which is now 12 months old).

Are there any plans at all to address this?

cunneen commented 7 months ago

hello?

Still getting this error on v12.10.1

gakinson commented 3 months ago

We are also experiencing this issue, any updates on resolving this?

Or is there anyway to disable the codegen running or data store for an amplify gen 1 project.?

gakinson commented 3 months ago

@cunneen not sure if this will help you, but we are not using data store and got a workaround for this by creating a copy of the amplifyPush.sh script in the root directory and removing the codegen related lines (--codegen ${CODEGEN}) from the script. Then we modified our build spec to do the following two lines and we are able to use the latest version of amplify CLI.

version: 1
backend:
  phases:
    build:
      commands:
        - npm i -S graphql-ttl-transformer
        - export NODE_OPTIONS=--max-old-space-size=8192
        - sh ./amplifyPush.sh --simple
        - amplify push -y
#!/usr/bin/env bash
set -e
IFS='|'

help_output () {
    echo "usage: amplify-push <--environment|-e <name>> <--simple|-s>"
    echo "  --environment  The name of the Amplify environment to use"
    echo "  --simple  Optional simple flag auto-includes stack info from env cache"
    exit 1
}

init_env () {
    ENV=$1
    AMPLIFY=$2
    PROVIDERS=$3
    CODEGEN=$4
    AWSCONFIG=$5
    CATEGORIES=$6

    echo "# Start initializing Amplify environment: ${ENV}"
    if [[ -z ${STACKINFO} ]];
    then
        echo "# Initializing new Amplify environment: ${ENV} (amplify init)"
        [[ -z ${CATEGORIES} ]] && amplify init --amplify ${AMPLIFY} --providers ${PROVIDERS} --yes || amplify init --amplify ${AMPLIFY} --providers ${PROVIDERS} --categories ${CATEGORIES} --yes
        echo "# Environment ${ENV} details:"
        amplify env get --name ${ENV}
    else
        echo "STACKINFO="${STACKINFO}
        echo "# Importing Amplify environment: ${ENV} (amplify env import)"
        amplify env import --name ${ENV} --config "${STACKINFO}" --awsInfo ${AWSCONFIG} --yes;
        echo "# Initializing existing Amplify environment: ${ENV} (amplify init)"
        [[ -z ${CATEGORIES} ]] && amplify init --amplify ${AMPLIFY} --providers ${PROVIDERS} --yes || amplify init --amplify ${AMPLIFY} --providers ${PROVIDERS} --categories ${CATEGORIES} --yes
        echo "# Environment ${ENV} details:"
        amplify env get --name ${ENV}
    fi
    echo "# Done initializing Amplify environment: ${ENV}"
}

ENV=""
IS_SIMPLE=false
POSITIONAL=()
while [[ $# -gt 0 ]]
    do
    key="$1"
    case ${key} in
        -e|--environment)
        ENV=$2
        shift
        ;;
        -r|--region)
        REGION=$2
        shift
        ;;
        -s|--simple)
        IS_SIMPLE=true
        shift
        ;;
        *)
        POSITIONAL+=("$1")
        shift
        ;;
    esac
done
set -- "${POSITIONAL[@]}"

# if no provided environment name, use default env variable, then user override
if [[ ${ENV} = "" ]];
then
    ENV=${AWS_BRANCH}
fi

if [[ ${USER_BRANCH} != "" ]];
then
    ENV=${USER_BRANCH}
fi

# strip slashes, limit to 10 chars
ENV=$(echo ${ENV} | sed 's;\\;;g' | sed 's;\/;;g' | cut -c -10)

# Check valid environment name
if [[ -z ${ENV} || "${ENV}" =~ [^a-zA-Z0-9\-]+ ]] ; then help_output ; fi

AWSCONFIG="{\
\"configLevel\":\"project\",\
\"useProfile\":true,\
\"profileName\":\"default\",\
\"AmplifyAppId\":\"${AWS_APP_ID}\"\
}"
AMPLIFY="{\
\"envName\":\"${ENV}\",\
\"appId\":\"${AWS_APP_ID}\"\
}"
PROVIDERS="{\
\"awscloudformation\":${AWSCONFIG}\
}"
CODEGEN="{\
\"generateCode\":false,\
\"generateDocs\":false\
}"
CATEGORIES=""
if [[ -z ${AMPLIFY_FACEBOOK_CLIENT_ID} && -z ${AMPLIFY_GOOGLE_CLIENT_ID} && -z ${AMPLIFY_AMAZON_CLIENT_ID} ]]; then
    CATEGORIES=""
else
    AUTHCONFIG="{\
    \"facebookAppIdUserPool\":\"${AMPLIFY_FACEBOOK_CLIENT_ID}\",\
    \"facebookAppSecretUserPool\":\"${AMPLIFY_FACEBOOK_CLIENT_SECRET}\",\
    \"googleAppIdUserPool\":\"${AMPLIFY_GOOGLE_CLIENT_ID}\",\
    \"googleAppSecretUserPool\":\"${AMPLIFY_GOOGLE_CLIENT_SECRET}\",\
    \"amazonAppIdUserPool\":\"${AMPLIFY_AMAZON_CLIENT_ID}\",\
    \"amazonAppSecretUserPool\":\"${AMPLIFY_AMAZON_CLIENT_SECRET}\"\
    }"
    CATEGORIES="{\
    \"auth\":$AUTHCONFIG\
    }"
fi
# Handle old or new config file based on simple flag
if [[ ${IS_SIMPLE} ]];
then
    echo "# Getting Amplify CLI Cloud-Formation stack info from environment cache"
    export STACKINFO="$(envCache --get stackInfo)"
    init_env ${ENV} ${AMPLIFY} ${PROVIDERS} ${CODEGEN} ${AWSCONFIG} ${CATEGORIES}
    echo "# Store Amplify CLI Cloud-Formation stack info in environment cache"
    STACKINFO="$(amplify env get --json --name ${ENV})"
    envCache --set stackInfo ${STACKINFO}
    echo "STACKINFO="${STACKINFO}
else
    # old config file, above steps performed outside of this script
    init_env ${ENV} ${AMPLIFY} ${PROVIDERS} ${CODEGEN} ${AWSCONFIG} ${CATEGORIES}
fi
dpilch commented 1 month ago

CLI v12.13.0 has been released with the fix for this.

github-actions[bot] commented 1 month ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.