is there a way to filter a query based on the cur...
# prisma-whats-new
m
is there a way to filter a query based on the current logged user ?
Copy code
query {
  user {
    id
    posts (# filter by the above id here??) {
       postedBy{
         id
      }
      description
    }
  }
}
a
The nature of GraphQL is that because
posts
is a child of
user
,
posts
is already filtered by current
user
m
make sense, however currently everything is returned i see all posts - I haven’t done anything with permissions - could that be why ?
a
With that exact query, you should not see posts linked to other users. What does your schema look like?
m
The above example was a simplied version of what i’m doing the actual query i’m running is this where i’m trying to filter the
chatRooms.users
to show the right user info in the chat header
Copy code
query rooms {
  user {
    id
    name
    auth0UserId
    chatRooms {
      id
      name
      topic
      users {
        id  (# filter here to get anyone that is not the current user )
        name
        picture
      }
      messages(last:100){
        payload
        sentBy {
          id (# filter here to get message that is not mine )
          name
          picture
        }
      }
    }
  }
}
Here is the schema
Copy code
type User @model {
  id: ID! @isUnique
  picture: String
  auth0UserId: String @isUnique
  chatRooms: [ChatRoom!]! @relation(name: "ChatRoomOnUser")
  ...
}

type ChatRoom @model {
  id: ID! @isUnique
  users: [User!]! @relation(name: "ChatRoomOnUser")
  messages: [Message!]! @relation(name: "ChatRoomOnMessage")
  createdAt: DateTime!
  updatedAt: DateTime!
  topic: String
  name: String @isUnique
}

type Message @model {
  chatRoom: ChatRoom! @relation(name: "ChatRoomOnMessage")
  id: ID! @isUnique
  createdAt: DateTime!
  updatedAt: DateTime!
  payload: [String!]
  sentBy: User! @relation(name: "MessageOnUser")
}
a
In that case, I think it's best to get the userId first, and then run a second query with that userid, so you can put a filter on those relations. You probably have the userId already, from the authentication? If you store that, you can use that and run the second query directly
m
yes i do have that id, i will try that
Thank you 🙂
😎 1