Subject relations in Java client
# spicedb
s
Hey when using the api, let's say I want to create a relationship using the API in java. Since it takes Object and Subject, how do you pass the type to these if you want to do a reference. Let's say I want to set as a parent of one object the parent of another object how would you say the type of object#parent and the ID lets say is potato potato#parent should be passed into the API? I tried just using the hashtag, but I get regex missmatch error.
v
👋 hey If you have a relationship like
object:potato#parent@user:jane
-
object
is the resource's
object_type
-
potato
is the resource's
object_id
-
parent
is the resources
relation
-
user
is the subject's
object_type
-
jane
is the subject's
object_id
so you will end up create a https://buf.build/authzed/api/docs/main:authzed.api.v1#authzed.api.v1.PermissionsService.WriteRelationships request that looks like this (pseudonotation)
Copy code
WriteRelationshipsRequest {
  updates: [
  {
    operation: "touch",
    relationship: {
      resource: {
        object_type: "object",
        object_id: "potato",
      },
      relation: "parent",
      subject: {
        object {
          object_type: "user",
          object_id: "jane",
        }
      }
    }
  }]
}
s
no no, this is not what I am asking, but thanks, I meant if I have a relationship like
Copy code
object:someObject#parent@object:potato#parent
this reference to another objects parent is what I am struggling with
v
Have you checked the API definition for
SubjectReference
?
what part of that definition are you struggling with?
s
yea I saw that, but I cant find a way to set this optional relationship into the reference. for instance I have the following code
Copy code
java
        PermissionService.WriteRelationshipsRequest request = PermissionService.WriteRelationshipsRequest.newBuilder()
                .addUpdates(
                        RelationshipUpdate.newBuilder()
                                .setOperation(RelationshipUpdate.Operation.OPERATION_CREATE)
                                .setRelationship(
                                        Relationship.newBuilder()
                                                .setResource(
                                                        ObjectReference.newBuilder()
                                                                .setObjectType("tutorial/file")
                                                                .setObjectId("test5")
                                                                .build())
                                                .setRelation("editor")
                                                .setSubject(
                                                        SubjectReference.newBuilder()
                                                                .setObject(
                                                                        ObjectReference.newBuilder()
                                                                                .setObjectType("tutorial/group")
                                                                                .setObjectId("architecture_infrastructure")
>                                                                                 .build())
                                                                .build())
                                                .build())
                                .build())
                .build();
to this I would like to add the argument of relationship, but I can't find a function that allows me to set it.
I don't know why the syntax spacing got broken, but I think you can generally understand it. I apologize, this could also not be to the package, could be my lack of knowledge in java, but I got assigned this task and am struggling my way through 😅
But basically I can't find a way to set the optional relationship
y
in the WriteRelationshipsRequest the thing that you're talking about is an
optional_subject_relation
s
Oh my god, I apologize guys, I am blind, I was trying to set it within the object reference, the IDE that I am working in is not great, I moved 1 line down and suddenly it works. 🤦‍♂️
I was wondering it is in documentation but I can't find the function, sorry a lot for wasting the time.
y
no worries! that's how a lot of issues end up being ^.^
8 Views