Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Using user info in AWS Amplify GraphQL schema

I am trying to structure a database for my application and one critical portion of it is the users association with various models in the databases. I am using Amplify's authentication as well for managing users. Below is a slightly modified version of their blog post example.

type Blog @model {
  id: ID!
  name: String!
  posts: [Post] @connection(name: "BlogPosts")
}

type Post @model {
  id: ID!
  title: String!
  blog: Blog @connection(name: "BlogPosts")
  comments: [Comment] @connection(name: "PostComments")
  user: ##userid##???
  likes: 0
}

type Comment @model {
  id: ID!
  content: String
  post: Post @connection(name: "PostComments")
  user: ##userid##???
}

Here are some issues I am seeing. I may want to display a users username and maybe a profile pic. I know I can store an optional attribute for a user like a pic url but can I access other users' attributes since they are not the current authenticated user? Also, how would I go about tracking the posts that a particular user likes? I can add a list of likedBy users on the Post model but that would not scale well at all if I wanted to get all the liked posts by a user. Then on the lowest level there is the matter of storing the user who created the Post. There is no users model that I can associate that with to get all of a particular users Posts. Is the solution to just have a Users model and manage that with authentication?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Let me try to help you.

First of all, I suggest you associate your models to some sort of authentication so they know what user created what. Cognito is pretty simple to start with.

After that your models would look something like (i adjusted your models to reflect the latest version of amplify, didn't test it but you can read more about it here):

type Blog @model 
  @auth(rules: [{ allow: owner }])
{
  id: ID!
  name: String!
  posts: [Post] @hasMany(indexName: "byBlog", fields: ["id"])
}

type Post @model 
  @auth(rules: [{ allow: owner }])
{
  id: ID!
  blogID: ID! @index(name: "byBlog")
  post: Post @belongsTo(fields: ["blogID"])
  title: String!
  likes: 0
  comments: [Comment] @hasMany
}

type Comment @model 
  @auth(rules: [{ allow: owner }])
{
  id: ID!
  content: String
}

After that you can create a lambda function that will be fetching the information of the user (based on the owner attribute, which is hidden unless you specify it) you can add a new attribute to your model as:

createdBy: Owner @function(name: "getOwnerDetails-${env}")

So your now your model would look something like:

type Owner {
    email: String
    name: String
    photo: String
}

type Comment @model 
  @auth(rules: [{ allow: owner }])
{
  id: ID!
  content: String
  createdBy: Owner @function(name: "getOwnerDetails-${env}")
}

I can't give you the specifics of the lambda function because mine has some extra stuff in it and its very specific for my case, but I'm sure you can work it out, just make sure you have access to your Cognito and its attribute and all you need to do is to return it :)

hope this helps.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!