Skip to content

Commit fc9ecb0

Browse files
feat: docs update to have BatchCheck serverside example in Go (#1138)
Co-authored-by: Ewan Harris <[email protected]>
1 parent 7627694 commit fc9ecb0

File tree

2 files changed

+65
-52
lines changed

2 files changed

+65
-52
lines changed

docs/content/getting-started/perform-check.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,15 @@ The result's `allowed` field will return `true` if the relationship exists and `
170170
If you want to check multiple user-object-relationship combinations in a single request, you can use the [Batch Check](/api/service#Relationship%20Queries/BatchCheck) API endpoint. Batching authorization checks together in a single request significantly reduces overall network latency.
171171

172172
:::note
173-
The BatchCheck endpoint is currently only supported by the JS SDK (>=[v0.8.0](https://github.com/openfga/js-sdk/releases/tag/v0.8.0) and the Python SDK (>=[v0.9.0](https://github.com/openfga/python-sdk/releases/tag/v0.9.0)). Support in the other SDKs is being worked on.
173+
The BatchCheck endpoint is currently supported by:
174+
- JS SDK (>=[v0.8.0](https://github.com/openfga/js-sdk/releases/tag/v0.8.0))
175+
- Python SDK (>=[v0.9.0](https://github.com/openfga/python-sdk/releases/tag/v0.9.0))
176+
- Go SDK (>=[v0.7.0](https://github.com/openfga/go-sdk/releases/tag/v0.7.0))
177+
- Java SDK (>=[v0.8.0](https://github.com/openfga/java-sdk/releases/tag/v0.8.0))
174178

175-
In the SDKs that don't support the server-side `BatchCheck`, the `BatchCheck` method performs client-side batch checks by making multiple check requests with limited parallelization, in SDK versions that do support the server-side `BatchCheck`, the existing method has been renamed to `ClientBatchCheck`.
179+
Support in the other SDKs is being worked on.
180+
181+
In the SDKs that don't support the server-side `BatchCheck`, the `BatchCheck` method performs client-side batch checks by making multiple check requests with limited parallelization. In SDK versions that do support the server-side `BatchCheck`, the existing method has been renamed to `ClientBatchCheck`.
176182

177183
Refer to the README for each SDK for more information. Refer to the release notes of the relevant SDK version for more information on how to migrate from client-side to the server-side `BatchCheck`.
178184
:::

src/components/Docs/SnippetViewer/BatchCheckRequestViewer.tsx

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -108,62 +108,71 @@ const { result } = await fgaClient.batchCheck(body, options);
108108
*/`;
109109

110110
case SupportedLanguage.GO_SDK:
111-
return `// The Go SDK does not yet support server-side batch checks. This currently just calls the check endpoint in parallel.
111+
return `// Requires >=v0.7.0 for the server side BatchCheck, earlier versions support a client-side BatchCheck with a slightly different interface
112+
body := ClientBatchCheckRequest{
113+
Checks: []ClientBatchCheckItem{${checks
114+
.map(
115+
(check) => `
116+
{
117+
User: "${check.user}",
118+
Relation: "${check.relation}",
119+
Object: "${check.object}",
120+
CorrelationId: "${check.correlation_id}",${
121+
check.contextualTuples
122+
? `
123+
ContextualTuples: []ClientContextualTupleKey{
124+
${check.contextualTuples
125+
.map(
126+
(tuple) => `{
127+
User: "${tuple.user}",
128+
Relation: "${tuple.relation}",
129+
Object: "${tuple.object}",
130+
},`,
131+
)
132+
.join('\n ')}
133+
},`
134+
: ''
135+
}${
136+
check.context
137+
? `
138+
Context: &map[string]interface{}${JSON.stringify(check.context)},`
139+
: ''
140+
}
141+
},`,
142+
)
143+
.join('')}
144+
},
145+
}
146+
147+
options := BatchCheckOptions{
148+
MaxBatchSize: openfga.PtrInt32(50), // optional, default is 50, can be used to limit the number of checks in a single server request
149+
MaxParallelRequests: openfga.PtrInt32(10), // optional, default is 10, can be used to limit the parallelization of the BatchCheck chunks${
150+
modelId
151+
? `,
152+
AuthorizationModelId: openfga.PtrString("${modelId}"),`
153+
: ''
154+
}
155+
}
156+
157+
data, err := fgaClient.BatchCheck(context.Background()).Body(body).Options(options).Execute()
112158
113-
body := ClientBatchCheckBody{${checks
159+
/*
160+
// Results are a map keyed by correlationId
161+
// Example:
162+
data.GetResult() = map[string]BatchCheckSingleResult{${checks
114163
.map(
115164
(check) => `
116-
{
117-
User: "${check.user}",
118-
Relation: "${check.relation}",
119-
Object: "${check.object}",${
120-
check.contextualTuples
121-
? `
122-
ContextualTuples: []ClientTupleKey{
123-
${check.contextualTuples
124-
.map(
125-
(tuple) => `
126-
{
127-
User: "${tuple.user}",
128-
Relation: "${tuple.relation}",
129-
Object: "${tuple.object}",
130-
},`,
131-
)
132-
.join('')}
133-
},`
134-
: ''
165+
"${check.correlation_id}": {
166+
Allowed: ${check.allowed},${
167+
check.allowed
168+
? ''
169+
: `
170+
Error: <FgaError ...>,`
135171
}
136172
},`,
137173
)
138174
.join('')}
139175
}
140-
options := ClientBatchCheckOptions{${modelId ? `\n AuthorizationModelId: PtrString("${modelId}"),\n` : ''}}
141-
data, err := fgaClient.BatchCheck(context.Background()).Body(requestBody).Options(options).Execute()
142-
/*
143-
data = [${checks
144-
.map(
145-
(check) => `{
146-
Allowed: ${check.allowed},
147-
Request: {
148-
User: '${check.user}',
149-
Relation: '${check.relation}',
150-
Object: '${check.object}'${
151-
check.contextualTuples
152-
? `,\n ContextualTuples: [${check.contextualTuples
153-
.map(
154-
(tuple) => `{
155-
User: '${tuple.user}',
156-
Relation: '${tuple.relation}',
157-
Object: '${tuple.object}'
158-
}`,
159-
)
160-
.join(',\n ')}]
161-
`
162-
: '\n '
163-
}}
164-
}`,
165-
)
166-
.join(', ')}]
167176
*/
168177
`;
169178

@@ -362,7 +371,6 @@ Reply:${checks
362371
`;
363372

364373
case SupportedLanguage.CURL: {
365-
/* eslint-disable max-len */
366374
return `curl -X POST $FGA_API_URL/stores/$FGA_STORE_ID/batch-check \\
367375
-H "Authorization: Bearer $FGA_API_TOKEN" \\ # Not needed if service does not require authorization
368376
-H "content-type: application/json" \\
@@ -405,7 +413,6 @@ Reply:${checks
405413
default:
406414
assertNever(lang);
407415
}
408-
/* eslint-enable no-tabs */
409416
}
410417

411418
export function BatchCheckRequestViewer(opts: BatchCheckRequestViewerOpts): JSX.Element {

0 commit comments

Comments
 (0)