2

I have a problem with AWS AppSync and ApolloClient. How can I use an association between users in the Amazon Service named AppSync, that is, a connection as node and edge. What I want to do is when I follow the users, I would like to see the flow of all users with a single request. It is the request that I want to be. How do I build a structure for this?

query {
    getFeeds(id:"myUserId") {
    following {
      userFeed {
        id
        ImageDataUrl
        textData
        date
      }
    }
  }
}

The schema I created is as follows

type Comments {
    id: ID!
    date: Int!
    message: String!
    user: User
}
type Feed {
    id: ID!
    user: User!
    date: Int!
    textData: String
    ImageDataUrl: String
    VideoDataUrl: String
    likes: Like
    comments: [Comments]
}

#Objects
type Like {
    id: ID!
    number: Int!
    likers: [User]
}
}
type Query {
    getAllUsers(limit: Int): [User]
}

type User {
    id: ID!
    name: String!
    email: String!
    imageUrl: String!
    imageThumbUrl: String!
    followers: [User]
    following: [User]
    userFeed: [Feed]
}

schema {
    query: Query
}

1 Answers1

8

This is possible in AppSync today.

To accomplish this, you could add a query field to your schema called getUser (getUser makes more sense than getFeeds in this case) and it would have a resolver which retrieves a User object from a data source.

type Query {
    getAllUsers(limit: Int): [User]
    getUser(id:ID!): User
}

Then, you can also add resolvers on the User.following and User.userFeed fields. The User.following resolver would query your data source and retrieve users whom somebody is following. The User.userFeed resolver would query your data source to retrieve a list of user feeds.

Both of these resolvers (User.following and User.userFeed) should utilize $context.source in the resolver's request mapping template. This variable will contain the result of your getUser resolver. The request mapping template's job is to create a query which your data source understands.

An example request mapping template which might be attached to User.following could be similar to the following. It would query a table named "Following", which has a primary partition key of id (the id of the user):

{
    "version" : "2017-02-28",
    "operation" : "Query",
    "query" : {
        ## Provide a query expression. **
        "expression": "id = :id",
        "expressionValues" : {
            ":id" : {
                ## Use the result of getUser to populate the query parameter **
                "S" : "${ctx.source.id}"
            }
        }
    }
}

You would have to do something similar for User.userFeed resolver.

After you're all setup, you can run the below query, and the following will happen:

query {
    getUser(id:"myUserId") {
    following {
      userFeed {
        id
        ImageDataUrl
        textData
        date
      }
    }
  }
}
  1. getUser resolver will run first. It will query your User data source and retrieve the user.
  2. User.following resolver will run. It will use the result of it's parent field resolver (getUser) to query the data source for following.
  3. User.userFeed resolver will run. It will use the result of it's parent field resolver (getUser) to query the user feed data source.