Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda