You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been working with the new Gen 2 Amplify stuff recently and really liking it. I am specifically working on trying to move into custom handlers for the Appsync/GraphQL stuff to add custom business logic into those operations.
From the front-end, everything works fine when using code like client.models.XXX.get or client.mutations.XXX, but when I attempt to use those things in my custom handler I am having problems.
First, I tried this approach:
import type { Schema } from '@/amplify/data/resource'
import { generateClient } from 'aws-amplify/data'
import { Amplify } from 'aws-amplify'
import outputs from '@/amplify_outputs.json'
import { Game } from '@/models/game'
Amplify.configure(outputs)
const client = generateClient<Schema>()
export const createGame = async (): Promise<Game> => {
const { errors, data } = await client.mutations.createNewGame()
...
This works great in development but throws the following errors during build:
2024-10-31T03:11:13.154Z [INFO]: # Executing command: npx ampx pipeline-deploy --branch $AWS_BRANCH --app-id $AWS_APP_ID
2024-10-31T03:11:28.361Z [INFO]: SyntaxError: TypeScript validation check failed.
Resolution: Fix the syntax and type errors in your backend definition.
Cause: amplify/data/createGameHandler.ts(6,21): error TS2307: Cannot find module '../../amplify_outputs.json' or its corresponding type declarations.
I tried using TS ignore approaches and tried relative and absolute path changes but nothing seemed to allow the code to find the amplify_outputs.json file during build.
import type { Schema } from './resource'
import { generateClient } from 'aws-amplify/data'
import { Game } from '@/models/game'
const client = generateClient<Schema>()
export const handler: Schema['createNewGame']['functionHandler'] = async () => {
const { errors, data } = await client.mutations.createNewGame()
...
This allows the build to work, but during runtime I receive the following error in the browser console:
message: "Client could not be generated. This is likely due to `Amplify.configure()` not being called prior to `generateClient()` or because the configuration passed to `Amplify.configure()` is missing GraphQL provider configuration."
Expected behavior
I would expect either of the above solutions to work, because either the amplify_outputs.json file is available during build and runtime, or that the amplify_outputs.json file is not required in the handler and therefore code is not required at runtime to initialize Amplfy.
Hi @mmixan we're still in the process of adding model based graphql operations support for lambda. in the meantime, we have this documentation for accessing your graphql api using a client.graphql approach rather than client.models.
please follow this doc and let us know if you run into any issues:
Thank you @chrisbonifacio !
I had seen that doc before, but was hoping there was something easier to work with.
I spent some time with that article the other day and it made sense, but I am struggling a bit with one line: endpoint: env.<amplifyData>_GRAPHQL_ENDPOINT, // replace with your defineData name
Can you give a concrete example of what that would look like in practice? I have the generated code exported into a folder near the custom handler, but I am not following how to get to the URL in doc. Thanks!
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
GraphQL API
Amplify Version
v6
Amplify Categories
auth, api
Backend
Amplify Gen 2
Environment information
Describe the bug
I have been working with the new Gen 2 Amplify stuff recently and really liking it. I am specifically working on trying to move into custom handlers for the Appsync/GraphQL stuff to add custom business logic into those operations.
From the front-end, everything works fine when using code like
client.models.XXX.get
orclient.mutations.XXX
, but when I attempt to use those things in my custom handler I am having problems.First, I tried this approach:
This works great in development but throws the following errors during build:
I tried using TS ignore approaches and tried relative and absolute path changes but nothing seemed to allow the code to find the
amplify_outputs.json
file during build.So next, using https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/connect-bedrock/ as an example, I was attempting to remove the need for
Amplify.configure()
in the Handler, so I moved to a solution like this:This allows the build to work, but during runtime I receive the following error in the browser console:
Expected behavior
I would expect either of the above solutions to work, because either the
amplify_outputs.json
file is available during build and runtime, or that theamplify_outputs.json
file is not required in the handler and therefore code is not required at runtime to initialize Amplfy.Reproduction steps
Code Snippet
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response
The text was updated successfully, but these errors were encountered: