Welcome folks,

AWS AppSync is a service that allows developers to easily build apps with real-time and offline capabilities. GraphQL is an open source data query and manipulation language, and a runtime for fulfilling queries with existing data. The way that I would describe AppSync is that it is basically a Firebase like service but with more comprehensive database query and search features, as well as a fully managed. AWS Appsync can be connected to any Data sources we have, in this article we use DynamoDB as Database and we can also connect lambda functions or EC2 instances.


Before I started to explore appsync, I have created a table named "post" with following fields

{
	author: String!
	id: String!
	url: String
	content: String
	title: String
}

Setup:


Step 1: Go to your aws console panel and search for aws appsync

Step 2: click on import from DynamoDB

Step 3: Select your region (this should be same as DynamoDB region) and select your table.

Step 4: Name your API and click next it will create the entire schema (Representation of your table model) for you. Ta Da!

In your schema panel, it will look like this,

input CreatePostInput {
	author: String!
	id: String!
	url: String
	content: String
	title: String
}

input DeletePostInput {
	author: String!
	id: String!
}

type Mutation {
	createPost(input: CreatePostInput!): Post
	updatePost(input: UpdatePostInput!): Post
	deletePost(input: DeletePostInput!): Post
}

type Post {
	author: String!
	id: String!
	url: String
	content: String
	title: String
}

type PostConnection {
	items: [Post]
	nextToken: String
}

type Query {
	getPost(id: String!, author: String!): Post
	listPosts(filter: TablePostFilterInput, limit: Int, nextToken: String): PostConnection
}

type Subscription {
	onCreatePost(
		author: String,
		id: String,
		url: String,
		content: String,
		title: String
	): Post
		@aws_subscribe(mutations: ["createPost"])
	onUpdatePost(
		author: String,
		id: String,
		url: String,
		content: String,
		title: String
	): Post
		@aws_subscribe(mutations: ["updatePost"])
	onDeletePost(
		author: String,
		id: String,
		url: String,
		content: String,
		title: String
	): Post
		@aws_subscribe(mutations: ["deletePost"])
}

input UpdatePostInput {
	author: String!
	id: String!
	url: String
}

Now let's deep into the basic concepts in aws appsync,

In GraphQL the endpoint retrieving data will be always same; for eg: http://hostname/graphql [single endpoint]

Queries

As its name suggests, queries are data requests made by the client from the server. simply query is something like the normal select statement in the database.

Example

input CreatePostInput {
	author: String!
	id: String!
	url: String
	content: String
	title: String
}
type Query {
	listPosts(filter: TablePostFilterInput, limit: Int, nextToken: String): PostConnection
}```

From the client side,

```javascript
query listPosts {
  listPosts {
    items {
      author
      id
      url
      content
      title
    }
  }
}

This will fetch author, id, url, content and title from post table.

Mutations

Mutations are used to create, update or delete data. The structure is almost similar to queries except for the fact that you need to include the word ‘mutation’ in the beginning. For instance,

input CreatePostInput {
	author: String!
	id: String!
	url: String
	content: String
	title: String
}

type Mutation {
	createPost(input: CreatePostInput!): Post
}

Example

mutation createPost($createpostinput: CreatePostInput!) {
    createPost(input: $createpostinput) {
      author
      id
      url
      content
      title
    }
  }

Subscriptions

Subscriptions can be used to set up and retain a real-time link to the server. This allows you to get instant information regarding relevant events. Mostly, a client needs to subscribe to the particular event in order to get the corresponding data. Subscriptions in AWS AppSync are invoked as a response to a mutation. You configure this with a Subscription type and “@aws_subscribe()” directive in the schema to denote which mutations invoke one or more subscriptions.

onCreatePost(
		author: String,
		id: String,
		url: String,
		content: String,
		title: String
	): Post
		@aws_subscribe(mutations: ["createPost"])

The above is the representation of createpost subscription schema.

Subscription from client side:

 subscription onCreatePost {
    onCreatePost {
      author
      id
      url
      content
      title
    }
  }

AWS Appsync:

Aws Appsync - example for query using node.js

Aws Appsync - example for Mutation using node.js

Aws Appsync - example for subscription using node.js

Happy coding! 🙌