Open naedx opened 1 month ago
npx @aws-amplify/cli codegen add --apiId XXXX --region XXXX
This command also produces the same result.
Workaround:
aws appsync get-introspection-schema --api-id XXXXX --format SDL --no-include-directives lib/api/amplify/schema.graphql
Using the command above outputs the schema as expected.
Hey👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂
Hey @naedx, Could you please share the generated mutation.js file after running the command npx ampx generate graphql-client-code --format graphql-codegen --out lib/api --statement-target graphql
?
Hi @AnilMaktala, using the schema defined in the QuickStart guide I get:
# this is an auto generated file. This will be overwritten
mutation CreateTodo(
$condition: ModelTodoConditionInput
$input: CreateTodoInput!
) {
createTodo(condition: $condition, input: $input) {
content
createdAt
id
updatedAt
__typename
}
}
mutation DeleteTodo(
$condition: ModelTodoConditionInput
$input: DeleteTodoInput!
) {
deleteTodo(condition: $condition, input: $input) {
content
createdAt
id
updatedAt
__typename
}
}
mutation UpdateTodo(
$condition: ModelTodoConditionInput
$input: UpdateTodoInput!
) {
updateTodo(condition: $condition, input: $input) {
content
createdAt
id
updatedAt
__typename
}
}
Try this file to see if it is going to create a different model:
import { type ClientSchema, a, defineData } from '@aws-amplify/backend';
const schema = a.schema({ GameSession: a .model({ id: a.id(), level: a.integer(), durationInSeconds: a.integer(), }) .authorization(allow => [allow.owner()]) });
export type Schema = ClientSchema
export const data = defineData({ schema, authorizationModes: { defaultAuthorizationMode: "userPool", }, });
Hi @darko-sameplace . The environment doesn't deploy anything with the code that you've provided:
awstest2 % npx ampx sandbox --outputs-format dart --outputs-out-dir lib --outputs-version 0
Amplify Sandbox
Identifier: XXXXXX
Stack: XXXXXX
To specify a different sandbox identifier, use --identifier
amplify/data/resource.ts(20,22): error TS2314: Generic type 'ClientSchema' requires 1 type argument(s).
TypeScript validation check failed.
Resolution: Fix the syntax and type errors in your backend definition.
To fix it I've changed:
export type Schema = ClientSchema;
to
export type Schema = ClientSchema<typeof schema>;
This is the schema it generates and deploys to the cloud:
@naedx npx ampx generate graphql-client-code
generates the client side code necessary for your application. It does not produce the GraphQL schema used by your AppSync API. Amplify does not offer a command to produce the transformed GraphQL schema.
You can use aws appsync get-introspection-schema
as you have mentioned previously.
You can also access it through the CDK constructs. In your amplify/backend.ts
file:
const backend = defineBackend({
auth,
data,
});
console.log(
// @ts-ignore
backend.data.resources.cfnResources.cfnGraphqlApi.node.scope.schema.definition
);
@naedx
npx ampx generate graphql-client-code
generates the client side code necessary for your application. It does not produce the GraphQL schema used by your AppSync API. Amplify does not offer a command to produce the transformed GraphQL schema.
Hi @dpilch ! The schema is necessary for my application. Here is my use case:
My application is in Flutter
I use a 3rd party GraphQL client.
I use graphql_flutter + graphql_codegen. They provide a wide range of functionality and work well with AppSync. The codegen listens to schema changes and updates the client code accordingly. These libraries are very popular in the Flutter community so other Flutter + Amplify users are likely to have a similar experience to mine.
As our Gen2 project evolves we frequently need to regenerate the our client to match. The current process requires calling aws appsync get-introspection-schema --api-id XXXXX --format SDL --no-include-directives lib/api/amplify/schema.graphql
while manually tracking down the api-id that corresponds to the current sandbox and injecting it into the command, defeating a simple npm script to repeat this process.
I then need to produce a file containing (1) the types from the schema.graphql
, (2) the query/mutation/subscription definitions from the npx ampx generate graphql-client-code
command and (3) the AppSync defined scalars:
scalar AWSDateTime
scalar AWSTimestamp
scalar AWSJSON
Having a flag to include the schema with what is generated by Amplify would greatly complement the Gen2 developer experience (which is fantastic so far). This would help to streamline the process and make Amplify more accessible developers who need to use 3rd party graphql clients to access functionality not provided by the Amplify client.
I will mark this as a feature request.
I've found a solution to the challenges I had above. Instead of using ampx generate graphql-client-code
I'm now using @graphql-codegen/cli
. This takes care of all the steps I had before.
I've described the setup here: https://github.com/naedx/amplify-playground/tree/dev/projects/amplify-graphql-codegen
Environment information
Description
Describe the bug
npx ampx generate graphql-client-code --format graphql-codegen --out lib/api --statement-target graphql
The command above generates
mutations.graphql
,subscriptions.graphql
andqueries.graphql
. Each of these files lists the appropriate GraphQL operations. However only the operations are generated:the user types from schema are not generated
How can this bug be reproduced?
Follow the quickstart guide to create a gen2 project
run the command
npx ampx generate graphql-client-code --format graphql-codegen --out lib/api --statement-target graphql
observe that the user types and the subtypes are not generated.