Skip to content

How to use `ClientExtensions` accessors and methods, in Amplify Gen2 app with separate backend/frontend?

2

How can I supply an accurate & useful Schema type parameter in my call to generateClient, when my Amplify Gen2 backend is separate from the frontend?

I'm building a webapp with AWS Amplify (Gen2). I really love what the team has done with Amplify (code-first for everything) with Gen2.

With this webapp, I've separated my "amplify-backend" from my "nextjs-frontend", and I've set the backend deployment to activate the frontend's build hook. In my nextjs-frontend's amplify.yml, I use ampx generate outputs to generate the client-config into the frontend's source tree. So-far, so-good.

I've been able to write some data-access client code using amplifyClient.graphql(…), and that connects to my deployed backend AppSync API, queries and mutations are workable that way. But I've lost the convenience of the ClientExtensions because I can't supply an accurate & useful type parameter. In particular, I'd like to be able to call client.models.SomeModel.list(…) providing a custom SelectionSet.

I thought that I could use one of the ampx generate graphql-client-code commands to obtain the Schema type declaration, but using the --format modelgen --model-target javascript options, I noticed that the Schema type in the generated code is deprecated, and clicking through that symbol in my IDE led me to this code, seemingly indicating that I'm barking at the wrong tree.

The comment in that code assumes that the frontend and backend are colocated on the filesystem where the code containing the call to generateClient is built. How can I obtain a reference to the needed Schema type parameter, so that the result of generateClient() is well-typed? It's not that the information isn't available, by breakpointing in the debugger, I can see that the returned object deeply contains the fields that would be needed; rather, the problem is that Typescript (and my IDE) don't have the information (the T) I need to write client code that exercises ClientExtensions<T>.

asked 2 years ago327 views

2 Answers
1

To use ClientExtensions accessors and methods in an Amplify Gen2 app with separate backend and frontend, and to supply an accurate Schema type parameter when calling generateClient, you can follow these steps:

  1. Copy the schema definition from your backend's amplify/data/resource.ts file to a new file in your frontend project, such as schema.ts.

  2. Add a path alias in your frontend's tsconfig.json that points to this new schema file:

    {
      "compilerOptions": {
        "paths": {
          "@/data-schema": ["./path/to/your/schema.ts"]
        }
      }
    }
  3. In your frontend code, import the schema and use it as the type parameter when calling generateClient:

    import { generateClient } from 'aws-amplify/api';
    import type { Schema } from '@/data-schema';
    
    const client = generateClient<Schema>();

This approach allows you to use ClientExtensions methods with proper typing:

const items = await client.models.SomeModel.list({
  selectionSet: ['id', 'name', 'description']
});

To keep your frontend schema in sync with your backend, consider setting up a script that copies the schema file from your backend to your frontend during deployment. Remember to update your frontend's schema file whenever you make changes to your backend schema to ensure type safety and consistency between your frontend and backend.
Sources
Separate frontend and backend teams - AWS Amplify Gen 2 Documentation
Fullstack TypeScript: Reintroducing AWS Amplify | Front-End Web & Mobile

answered 2 years ago

1

This is a good question, and the AI answer is helpful to understand how to solve the problem. It would be great if the AWS Amplify team could document the preferred way to connect to the schema when implementing multi-repo projects in Amplify--over here in the Amplify Gen-2 Documentation Full Stack Workflows >> Separate Frontend and Backend Teams

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.