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

Custom types can't be referenced more than once #495

Open
3 tasks done
Erid opened this issue Dec 31, 2024 · 4 comments
Open
3 tasks done

Custom types can't be referenced more than once #495

Erid opened this issue Dec 31, 2024 · 4 comments

Comments

@Erid
Copy link

Erid commented Dec 31, 2024

Before opening, please confirm:

JavaScript Framework

Not applicable

Amplify APIs

GraphQL API

Amplify Version

v6

Amplify Categories

api

Backend

Amplify Gen 2

Environment information

# Put output below this line
  System:
    OS: macOS 15.2
    CPU: (8) arm64 Apple M1 Pro
    Memory: 53.89 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.10.0 - ~/.asdf/installs/nodejs/20.10.0/bin/node
    npm: 10.2.3 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 8.15.9 - ~/.asdf/installs/nodejs/20.10.0/bin/pnpm
    bun: 1.0.25 - /opt/homebrew/bin/bun
  Browsers:
    Chrome: 131.0.6778.205
    Edge: 131.0.2903.112
    Safari: 18.2
  npmPackages:
    @aws-amplify/auth: 6.9.0 => 6.9.0 
    @aws-amplify/backend: 1.11.0 => 1.11.0 
    @aws-amplify/backend-cli: 1.4.5 => 1.4.5 
    @aws-sdk/client-cognito-identity-provider: 3.716.0 => 3.716.0 
    @aws-sdk/client-lambda: 3.716.0 => 3.716.0 
    @aws-sdk/client-s3: 3.717.0 => 3.717.0 
    @aws-sdk/s3-request-presigner: 3.717.0 => 3.717.0 
    @empat/core: workspace:* => 1.0.0 
    @empat/eslint-config: workspace:* => 1.0.0 
    @types/aws-lambda: 8.10.146 => 8.10.146 
    aws-amplify: 6.11.0 => 6.11.0 
    aws-cdk: 2.173.2 => 2.173.2 
    aws-cdk-lib: 2.173.2 => 2.173.2 
    constructs: 10.4.2 => 10.4.2 
    dotenv: 16.4.5 => 16.4.5 
    esbuild: 0.24.0 => 0.24.0 
    graphql-request: 7.1.0 => 7.1.0 
    openai: 4.68.4 => 4.68.4 
    tsx: 4.19.1 => 4.19.1 
    typescript: 5.7.2 => 5.7.2 
    vitest: 2.1.3 => 2.1.3 
  npmGlobalPackages:
    @builder.io/micro-agent: 0.1.5
    @githubnext/github-copilot-cli: 0.1.36
    corepack: 0.22.0
    hasura-cli: 2.35.2
    npm: 10.2.3

Describe the bug

When defining a custom type, if I try to reference it as the return type of a query, it works, however, if I do it on more than one query it throws an error.

Expected behavior

I would expect the Schema to let me reference the same custom type more than once.

Reproduction steps

  1. Define a custom type, like:
CustomType: a.customType({
  id: a.string().required(),
  name: a.string().required(),
}),
  1. Then reference it to be part of the result of a query, like this:
getSomething: a
  .query()
  .arguments({
    arg1: a.string().required(),
  })
  .returns(a.ref("CustomType").required().array().required())
  .handler(a.handler.function(getSomething))
  .authorization((allow) => [allow.group("Admin")]),
  1. That works just as expected, however, then create another query that returns the same type, and get the following error:
api dev: Failed to instantiate data construct
api dev: Caused By: Schema validation failed.\n\nThe directive "@aws_cognito_user_pools" can only be used once at this location.\n\nGraphQL request:259:16\n258 |\n259 | type CustomType @aws_cognito_user_pools @aws_api_key @aws_cognito_user_pools(cognito_groups: ["Admin"])\n    |                ^\n260 | {\n\nGraphQL request:259:53\n258 |\n259 | type CustomType @aws_cognito_user_pools @aws_api_key @aws_cognito_user_pools(cognito_groups: ["Admin"])\n    |                                                     ^\n260 | {
api dev: Resolution: See the underlying error message for more details.
api dev: [Sandbox] Watching for file changes...
@Erid Erid changed the title Custom types can't be referenced more than oncex Custom types can't be referenced more than once Dec 31, 2024
@chrisbonifacio chrisbonifacio self-assigned this Jan 2, 2025
@chrisbonifacio
Copy link
Member

Hi @Erid , thanks for raising this issue and providing reproduction steps. Unfortunately, I have not been able to reproduce the behavior described in this issue.

I tried deploying this schema:

  CustomType: a.customType({
    id: a.string().required(),
    name: a.string().required(),
  }),
  getSomething: a
    .query()
    .arguments({
      arg1: a.string().required(),
    })
    .returns(a.ref("CustomType").required().array().required())
    .handler(a.handler.function(getSomething))
    .authorization((allow) => [allow.group("Admin")]),
  getSomething2: a
    .query()
    .arguments({
      arg1: a.string().required(),
    })
    .returns(a.ref("CustomType").required().array().required())
    .handler(a.handler.function(getSomething))
    .authorization((allow) => [allow.group("Admin")]),

but was able to compile and deploy successfully.

Can you confirm you are using the latest versions of the @aws-amplify/backend packages?

@Erid
Copy link
Author

Erid commented Jan 3, 2025

Thanks for the reply @chrisbonifacio, it's odd, I was positive that was enough to reproduce, but maybe I missed something. I'll try to reproduce by creating a new project and introduce parts of the schema bit by bit; since I can't share the company's code.

And yes, I have version 1.11.0. I'll share my project as soon as I can take to some time to reproduce, hopefully Today, if not, next week.

@chrisbonifacio
Copy link
Member

Hi @Erid, just wanted to follow up and see if you are still blocked on this issue. If you need more time to provide a reproducible app, totally understand! I did try downgrading to 1.11.0 in my local project as well but did not experience the error on deployment.

We also thought maybe the conflict in auth directives might've been caused by having the same auth rule on the schema level so I tried that but alas, the schema still compiled and deployed ☹️

const schema = a
  .schema({
    CustomType: a.customType({
      id: a.string().required(),
      name: a.string().required(),
    }),
    getSomething: a
      .query()
      .arguments({
        arg1: a.string().required(),
      })
      .returns(a.ref("CustomType").required().array().required())
      .handler(a.handler.function(getSomething))
      .authorization((allow) => [allow.group("Admin")]),
    getSomething2: a
      .query()
      .arguments({
        arg1: a.string().required(),
      })
      .returns(a.ref("CustomType").required().array().required())
      .handler(a.handler.function(getSomething))
      .authorization((allow) => [allow.group("Admin")]),
  })
  .authorization((allow) => [allow.group("Admin")]);

@Erid
Copy link
Author

Erid commented Jan 9, 2025

Thanks for the follow up @chrisbonifacio! I need some more time to repro, it's still happening to me, I "worked around it" by duplicating the type for now; but this week I didn't have the time to create the app to repro and I'm away from my computer for a few days now.

I'll have it for you next week! I need to find the root cause 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants