amplify-codegen icon indicating copy to clipboard operation
amplify-codegen copied to clipboard

[codegen] graphql config - multiple extension outputs from a project

Open n10000k opened this issue 4 years ago • 8 comments

Is your feature request related to a problem? Please describe. I want to output typescript types to lambda functions as well as my frontend application also.

Describe the solution you'd like The ability to have many extensions with the same key as amplify within the same project:

projects:
  Workspaces:
    schemaPath: src/graphql/Workspaces/schema.json
    includes:
      - src/graphql/Workspaces/**/*.ts
    excludes:
      - ./amplify/**
    extensions:
      amplify:
        codeGenTarget: typescript
        generatedFileName: src/utils/API.ts
        docsFilePath: src/graphql/Workspaces
        maxDepth: 2
      amplify:
        codeGenTarget: typescript
        generatedFileName: amplify/backend/foobar/API.ts
        docsFilePath: amplify/backend/foobar
        maxDepth: 2
extensions:
  amplify:
    version: 3

Describe alternatives you've considered Having a folder in the root directory to allow types to be read from there, however it's not easy and requires extra permissions and npm won't let you read outside the main dir it's being ran from. So this will be an issue for lambda functions.

n10000k avatar Dec 31 '20 13:12 n10000k

Alternative solution This would work a better way instead of executing the extension many times;

projects:
  Workspaces:
    schemaPath: src/graphql/Workspaces/schema.json
    includes:
      - src/graphql/Workspaces/**/*.ts
    excludes:
      - ./amplify/**
    extensions:
      amplify:
        codeGenTarget: typescript
        generatedFiles: 
          - src/utils/API.ts
          - amplify/backend/API.ts
        docsFilePaths:
          - src/graphql/Workspaces
          - amplify/backend/Workspaces
        maxDepth: 2
extensions:
  amplify:
    version: 3

An argument made this though could be that you're restricted to one language output at this point. Your frontend is javascript, but your lambda function is python.. it's not going to like the typescript output.

n10000k avatar Dec 31 '20 13:12 n10000k

Hi @uncodable Thank you for taking time and requesting this feature. We are working on making the codegen configuration more flexible and customizable. Please look out for an RFC on this. Meanwhile, if you hadn't figured out already, you could edit the .graphqlconfig.yml file in your project root, edit the path to generatedFiles and run amplify codegen to generate the code at a different place.

phani-srikar avatar Feb 10 '21 02:02 phani-srikar

Thanks for the update @phani-srikar it's a blocker at the moment on my current project 👍🏻

n10000k avatar Feb 19 '21 14:02 n10000k

@uncodable just wanted to let you know that we've publish an RFC to address this and other code generation issues. Would love to have your feedback! https://github.com/aws-amplify/amplify-cli/issues/6898

renebrandel avatar Mar 18 '21 14:03 renebrandel

@uncodable just wanted to let you know that we've publish an RFC to address this and other code generation issues. Would love to have your feedback! aws-amplify/amplify-cli#6898

Any news when it will be ready? Some years already passed.

hackmajoris avatar Jun 18 '23 10:06 hackmajoris

@hackmajoris as a workaround i found this to be working perfectly fine

(working only if you agree to generate all possible queries and types for some reason when you add/re-add codegen)

projects:
  functionsonsteroids:
    schemaPath: amplify/backend/api/functionsonsteroids/build/schema.graphql
    includes:
      - src/graphql/**/*.graphql
    excludes:
      - ./amplify/**
    extensions:
      amplify:
        codeGenTarget: angular
        generatedFileName: src/app/API.service.ts
        docsFilePath: src/graphql
        region: eu-central-1
        apiId: null
        maxDepth: 2


  lambdaShared:
    schemaPath: amplify/backend/api/functionsonsteroids/build/schema.graphql
    includes:
      - projects/lambda-shared/src/lib/**/*.ts
    excludes:
      - ./amplify/**
    extensions:
      amplify:
        codeGenTarget: typescript
        generatedFileName: projects/lambda-shared/src/lib/generated/app-sync.models.ts
        docsFilePath: projects/lambda-shared/src/lib/generated/
        region: eu-central-1
        apiId: null
        maxDepth: 2

extensions:
  amplify:
    version: 3

vorant94 avatar Jul 05 '23 17:07 vorant94

@hackmajoris as a workaround i found this to be working perfectly fine

(working only if you agree to generate all possible queries and types for some reason when you add/re-add codegen)

projects:
  functionsonsteroids:
    schemaPath: amplify/backend/api/functionsonsteroids/build/schema.graphql
    includes:
      - src/graphql/**/*.graphql
    excludes:
      - ./amplify/**
    extensions:
      amplify:
        codeGenTarget: angular
        generatedFileName: src/app/API.service.ts
        docsFilePath: src/graphql
        region: eu-central-1
        apiId: null
        maxDepth: 2


  lambdaShared:
    schemaPath: amplify/backend/api/functionsonsteroids/build/schema.graphql
    includes:
      - projects/lambda-shared/src/lib/**/*.ts
    excludes:
      - ./amplify/**
    extensions:
      amplify:
        codeGenTarget: typescript
        generatedFileName: projects/lambda-shared/src/lib/generated/app-sync.models.ts
        docsFilePath: projects/lambda-shared/src/lib/generated/
        region: eu-central-1
        apiId: null
        maxDepth: 2

extensions:
  amplify:
    version: 3

Thank you. I have implemented the exact same approach and can confirm that it works fine. PS: I do use Nx to manage the monorepo, and https://nx.dev/more-concepts/grouping-libraries comes in rescue when dealing with sharing libraries.

hackmajoris avatar Jul 05 '23 19:07 hackmajoris

What is the easiest way to get codeGenTarget: typescript #and graphql, to output all other things equal?

charlieforward9 avatar Jul 07 '24 00:07 charlieforward9