Skip to content

Commit b7484e6

Browse files
authored
Merge pull request #142 from openfga/docs/batch-check
docs: Update README for server-side batch check
2 parents fdab6d2 + cc16f05 commit b7484e6

File tree

3 files changed

+118
-10
lines changed

3 files changed

+118
-10
lines changed

README.md

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,14 +601,122 @@ var response = fgaClient.check(request, options).get();
601601

602602
##### Batch Check
603603

604-
Run a set of [checks](#check). Batch Check will return `allowed: false` if it encounters an error, and will return the error in the body.
605-
If 429s or 5xxs are encountered, the underlying check will retry up to 3 times before giving up.
604+
Similar to [check](#check), but instead of checking a single user-object relationship, accepts a list of relationships to check. Requires OpenFGA version 1.8.0 or greater.
605+
606+
[API Documentation](https://openfga.dev/api/service#/Relationship%20Queries/BatchCheck)
606607

607-
> Passing `ClientBatchCheckClientOptions` is optional. All fields of `ClientBatchCheckClientOptions` are optional.
608+
> Passing `ClientBatchCheckOptions` is optional. All fields of `ClientBatchCheckOptions` are optional.
608609
609610
```java
611+
var reequst = new ClientBatchCheckRequest().checks(
612+
List.of(
613+
new ClientBatchCheckItem()
614+
.user("user:81684243-9356-4421-8fbf-a4f8d36aa31b")
615+
.relation("viewer")
616+
._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a")
617+
.correlationId("cor-1") // optional, one will be generated for you if not provided
618+
.contextualTuples(List.of(
619+
new ClientTupleKey()
620+
.user("user:81684243-9356-4421-8fbf-a4f8d36aa31b")
621+
.relation("editor")
622+
._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a")
623+
)),
624+
new ClientCheckRequest()
625+
.user("user:81684243-9356-4421-8fbf-a4f8d36aa31b")
626+
.relation("admin")
627+
._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a"),
628+
.correlationId("cor-2") // optional, one will be generated for you if not provided
629+
.contextualTuples(List.of(
630+
new ClientTupleKey()
631+
.user("user:81684243-9356-4421-8fbf-a4f8d36aa31b")
632+
.relation("editor")
633+
._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a")
634+
)),
635+
new ClientCheckRequest()
636+
.user("user:81684243-9356-4421-8fbf-a4f8d36aa31b")
637+
.relation("creator")
638+
._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a")
639+
.correlationId("cor-3), // optional, one will be generated for you if not provided
640+
new ClientCheckRequest()
641+
.user("user:81684243-9356-4421-8fbf-a4f8d36aa31b")
642+
.relation("deleter")
643+
._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a")
644+
.correlationId("cor-4") // optional, one will be generated for you if not provided
645+
)
646+
);
647+
648+
var options = new ClientBatchCheckOptions()
649+
.additionalHeaders(Map.of("Some-Http-Header", "Some value"))
650+
// You can rely on the model id set in the configuration or override it for this specific request
651+
.authorizationModelId("01GXSA8YR785C4FYS3C0RTG7B1")
652+
.maxParallelRequests(5); // Max number of requests to issue in parallel, defaults to 10
653+
.maxBatchSize(20); // Max number of batches to split the list of checks into, defaults to 50
654+
655+
var response = fgaClient.batchCheck(request, options).get();
656+
657+
/*
658+
response.getResult() = [{
659+
allowed: false,
660+
correlationId: "cor-1",
661+
request: {
662+
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
663+
relation: "viewer",
664+
_object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
665+
correlationId: "cor-1",
666+
contextualTuples: [{
667+
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
668+
relation: "editor",
669+
_object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a"
670+
}]
671+
},
672+
},
673+
{
674+
allowed: false,
675+
correlationId: "cor-2",
676+
request: {
677+
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
678+
relation: "admin",
679+
_object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
680+
correlationId: "cor-2",
681+
contextualTuples: [{
682+
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
683+
relation: "editor",
684+
_object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a"
685+
}]
686+
}
687+
},
688+
{
689+
allowed: false,
690+
correlationId: "cor-3",
691+
request: {
692+
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
693+
relation: "creator",
694+
_object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
695+
correlationId: "cor-3",
696+
},
697+
error: <FgaError ...>
698+
},
699+
{
700+
allowed: true,
701+
correlationId: "cor-4",
702+
request: {
703+
user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
704+
relation: "deleter",
705+
_object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
706+
correlationId: "cor-4",
707+
}
708+
},
709+
]
710+
*/
711+
```
712+
713+
If you are using an OpenFGA version less than 1.8.0, you can use `clientBatchCheck`,
714+
which calls `check` in parallel. It will return `allowed: false` if it encounters an error, and will return the error in the body.
715+
If 429s or 5xxs are encountered, the underlying check will retry up to 3 times before giving up.
716+
717+
```
610718
var request = List.of(
611-
new ClientCheckRequest()
719+
new ClientBatchCheckItem()
612720
.user("user:81684243-9356-4421-8fbf-a4f8d36aa31b")
613721
.relation("viewer")
614722
._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a")

build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
// Quality
55
id 'jacoco'
66
id 'jvm-test-suite'
7-
id 'com.diffplug.spotless' version '6.25.0'
7+
id 'com.diffplug.spotless' version '7.0.2'
88

99
// IDE
1010
id 'idea'
@@ -66,7 +66,7 @@ dependencies {
6666
implementation "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
6767
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
6868
implementation "org.openapitools:jackson-databind-nullable:0.2.6"
69-
implementation platform("io.opentelemetry:opentelemetry-bom:1.45.0")
69+
implementation platform("io.opentelemetry:opentelemetry-bom:1.46.0")
7070
implementation "io.opentelemetry:opentelemetry-api"
7171
}
7272

@@ -78,9 +78,9 @@ testing {
7878
dependencies {
7979
implementation project()
8080
implementation "org.junit.jupiter:junit-jupiter:$junit_version"
81-
implementation "org.mockito:mockito-core:5.14.2"
81+
implementation "org.mockito:mockito-core:5.15.2"
8282
runtimeOnly "org.junit.platform:junit-platform-launcher"
83-
implementation "org.wiremock:wiremock:3.10.0"
83+
implementation "org.wiremock:wiremock:3.11.0"
8484

8585
// This test-only dependency is convenient but not widely used.
8686
// Review project activity before updating the version here.

example/example1/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
plugins {
22
id 'application'
3-
id 'com.diffplug.spotless' version '6.25.0'
4-
id 'org.jetbrains.kotlin.jvm' version '2.1.0'
3+
id 'com.diffplug.spotless' version '7.0.2'
4+
id 'org.jetbrains.kotlin.jvm' version '2.1.10'
55
}
66

77
application {

0 commit comments

Comments
 (0)