Hi all. I got a question. I have the following thr...
# orm-help
m
Hi all. I got a question. I have the following three types
Copy code
type User @model {
	id: ID! @isUnique
    name: String
	conversations: [Conversations!]! @relation(name: "UserConversations")
	auth0UserId: String @isUnique
	nachrichten: [Message!]! @relation(name: "MessagesSendByUser")
}

type Conversations @model {
	id: ID! @isUnique
	user: [User!]! @relation(name: "UserConversations")
	messages: [Message!]! @relation(name: "MessagesInConversation")
	createdAt: DateTime!
	updatedAt: DateTime!
}

type Message @model {
	id: ID! @isUnique
	createdAt: DateTime!
	updatedAt: DateTime!
	message: String!
	conversation: Conversations @relation(name: "MessagesInConversation")
	read: Boolean
	from: User @relation(name: "MessagesSendByUser")
}
And I want that only the authenticated User can get the Conversations (which contain messages) he is involved in. How do I extend my shema? I find the graphcool doc rather not so helpful. I tried something along the lines like this.
Copy code
query getConversations($user_id: ID!, $conversationsUser_id: ID!) {
	getSomeConversations(filter: { AND: [{ id: $user_id }, { id: $conversationsUser_id }] })
}

extend type Query {
    getSomeConversations(

    )
}
But I still don't really get how to, maybe you guys can help me? Thank you
s
If you have defined a relationship between
User
and
Conversation
(as it appears you have), you should be able to do something as simple as:
Copy code
query {
  getConversationsForUser(id: MY_ID) {
    messages {
      read
      message
      from {
        name
      }
    }
  }
}
You would just be querying the
User
model and retrieving the conversations.
In the resolver, you would use
getUserId(ctx)
to get the user's ID. If that method fails it will throw an authentication error.
m
But how do I make sure not somebody else queries that data who found out the ID of user ABC? I have to set some sort of permissions right? @Sam Jackson
s
That was actually a typo on my end. It should just be something like:
Copy code
query {
  getConversationsForCurrentUser {
    ...
  }
}
Don't allow them to pass an ID as a parameter. Just fetch it from the authentication token.
That way they could only retrieve conversations if they're signed in for that particular user.
m
What u mean with fetch it? Write a resolver function?
s
Then import that function and use it in your resolvers.