Sam Min Wong
Sam Min Wong

Reputation: 151

How can I use schema.graphql instead of schema.json in an appsync and amplify project?

My project is built with vue.js and severless framework, when I try to integrate it with Aws AppSync and Amplify (by using amplify cli), it auto generated a schema.json under /src/graphql which is almost unreadable. So every time I want to change the schema i go to the appsync console, change it there, then I do one of the following:

  1. execute amplify api update in terminal so that it updates whatever has been change in the cloud and update my local schema accordingly.

  2. download the schema.json file from the appsync console after the change. going back and forth is very consuming.

So my question is:

  1. Can I somehow use schema.graphql in my project instead of the unreadable schema.json

  2. I also noticed if I comment out every line in schema.json or even removed the schema.json completely in /src/graphql, it does not give me any error when i run it, why is this happening?

Thanks in advance.

Upvotes: 2

Views: 3404

Answers (3)

sundar_147
sundar_147

Reputation: 13

  1. Change the schema modal in amplify/backend/API/schema.graphql file as the following example.

Ex. (one to many relation) The single user will have more posts

type User @model {
  id: ID!
  name: String
  posts: [Post] @connection(keyName: "postsByUser", fields: ["id"])
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

type Post @model 
@key(name: "postsByUser", fields: ["userID"]) {
  id: ID!
  description: String!  
  userID: ID!
  user: User @connection(fields: ["userID"])
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

Then save & check this schema model is having any error by run the following command and it will throw error if anything is wrong

amplify api gql-compile

After checking give amplify push and it will change the schema, data sources, queries & dynamodb tables.

To update/generate schema.json, queries & other files in frontend use the following command

amplify add codegen bbp3andl**********  (->appsync id)

Refer this link to write schema model https://blog.grandstack.io/graphql-api-configuration-with-neo4j-graphql-js-bf7a1331c793

Upvotes: 0

Ben Nizette
Ben Nizette

Reputation: 31

This question is a little old, but it might help someone:

The schema.json file is the introspection schema [1]. It's designed to be machine-readable so your favourite library can help you validate your requests client-side, it's certainly not designed to be human-readable or editable.

Instead you should edit amplify/backend/api/<apiname>/schema.graphql. When you then amplify api push it will do the following:

  1. Process that schema file according to the directives and transformers at [2] (you can also add your own transformers if you want to add custom directives that manipulate the schema, cloudformation or other resources). The output of this process is in build/graphql.schema in your API folder.
  2. Builds cloudformation to deploy this, similar to what @peter-n does himself.
  3. Deploys it, and
  4. Fetches the introspection schema (schema.json) for you and saves it to the location given in schemaPath in .graphqlconfig.yml, which is your src/graphql directory by default.

[1] https://graphql.org/learn/introspection/

[2] https://docs.amplify.aws/cli/graphql-transformer/directives

Upvotes: 3

peter n
peter n

Reputation: 1280

I don't have much experience with Amplify, but I have used AppSync with Serverless. I maintain the schema (using the cleaner SDL format) under version control in the repo, and a CI script uploads to S3, then update AppSync via Cloudformation (embedded in serverless.yml).

The AWS::AppSync::GraphQLSchema resource allows for an inline schema (not so clean) or a pull down from S3. Having a separate file also enables you to test locally and better track changes/drift.

Upvotes: 0

Related Questions