Follow feature: Back-end

First, you implement the GraphQL API, which provides the following functions necessary for implementing the timeline.

Create GraphQL API

Let’s create a FollowRelationship table so that one user can store the state following another user. Add the following code to ./amplify/backend/api/BoyakiGql/schema.graphql:

type FollowRelationship
	@auth(rules: [
		{allow: owner, ownerField:"followerId", provider: userPools, operations:[read, create]},
		{allow: private, provider: userPools, operations:[read]}
	@key(fields: ["followeeId", "followerId"])
	followeeId: ID!
	followerId: ID!
	timestamp: AWSTimestamp!

The points are as follows:

  • Fields
    • followeId is the username of the person being followed.
    • followerId is the username of the person following.
    • tiemstamp is the date and time when follow.
  • @auth
    • {allow: owner... allows users to create and view FollowRelationships
    • {allow: private... allows other users to view FollowRelasionship
  • @key
    • If you do not specify name or queryField, you can set Partition Key (PK) or Secondary Key (SK) of DynamoDB Table itself (not GSI).
    • This time, get a list of FollowRelationships associated with a followeId to duplicate the Post to the Timeline of followers of those who posted the Post.
    • So specify followeeId as PK and followerId as SK.

Operation Verification by Mocking

Let’s run $amplify mock api to see if the FollowRelationship API works as expected.

Amplify Mocking requires port 20002 to be available and may not be available in all Cloud IDEs. If you are using the applicable Cloud IDE, please do not follow this procedure and enjoy the atmosphere.

amplify mock api


AppSync Mock endpoint is running at http://XXX.XXX.XXX.XXX:20002

Copy the Mock endpoint and access it in any browser.


First, let’s create a FollowRelationship.

  1. In the lower left corner, click ADD NEW Query, choose Mutation, and then click +.
  2. Click on the Update Auth button to make Username test_follower.
  3. Click createFollowRelationship in the left pane and fill the checkboxes and fields as shown in the following figure.
    1. followeId: test_followee
    2. followerId: test_follower
  4. The GraphQL query is written out in the middle pane. At this time, Amplify GraphQL Explorer does not fully support the AWSTimestamp type, and if you enter a field of type AWSTimestamp in the left pane, the Output as String (enclosed in double quotes) in the middle pane. Let’s remove the double quote " in timestamp.
  5. Click to run the GraphQL Mutation.

If a Unauthorized Error occurs, click on the Update Auth button and please make sure Username of the user currently issuing Mutation matches followerId. As set by @auth, only the following users (= followers) can execute createFollowRelationship. If the error did not occur, you should try running followerId as another Username and make sure that Unauthorized Error occurs.


Next, you get a list of followers for a user.

  1. In the lower left corner, click on the text ADD NEW Mutation, choose Query, and then click +.
  2. Click listFollowRelationship in the left pane and fill the checkboxes and fields as shown in the following figure.
    1. followeId: test_followee
  3. Click ︎ to execute a GraphQL Query.
  4. The results are displayed in the right pane. Make sure that the FollowRelationship you just created is displayed.