跳转至

使用全局节点 ID

节点 id 是一个对象的全局唯一标识符。在调用 GraphQL query 和 mutation 时都需要用到它。本节将介绍如何生成用于调用 Admin GraphQL API 的节点 ID。

节点 id 是一个采用 base64url 编码的字符串,格式为 <NODE_TYPE>:<ID>。例如,在 User:97b1c929-842c-415c-a7df-6967efdda160 中,节点类型是 "User",而 "97b1c929-842c-415c-a7df-6967efdda160" 则是这个特定用户的 ID。

1. 生成 User 节点类型的 id

您可以使用自己熟悉的编程语言或工具,将节点 ID 编码为 base64url,示例如下:

package main

import (
    "encoding/base64"
    "fmt"
)

func main() {
    nodeID := base64.RawURLEncoding.EncodeToString([]byte("User:97b1c929-842c-415c-a7df-6967efdda160"))
    fmt.Println(nodeID)
}
import base64

base64.urlsafe_b64encode(b'User:97b1c929-842c-415c-a7df-6967efdda160').replace(b'=', b'')
<?php
function base64url_encode($data) {
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

$encoded_id = base64url_encode("User:97b1c929-842c-415c-a7df-6967efdda160");
echo $encoded_id;
const encoder = require('base64-url');

const encoded_id = encoder.encode("User:97b1c929-842c-415c-a7df-6967efdda160");
console.log(encoded_id);

获取 User 对象示例

Query:

query {
  node(id: "<BASE64URL_ENCODED_USER_NODE_ID>") {
    ... on User {
      id
      standardAttributes
    }
  }
}

2. 生成 AuditLog 节点类型的 id

package main

import (
    "encoding/base64"
    "fmt"
)

func main() {
    nodeID := base64.RawURLEncoding.EncodeToString([]byte("AuditLog:000000000004d2e7"))
    fmt.Println(nodeID)
}
import base64

base64.urlsafe_b64encode(b'AuditLog:000000000004d2e7').replace(b'=', b'')
<?php
function base64url_encode($data) {
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

$encoded_id = base64url_encode("AuditLog:000000000004d2e7");
echo $encoded_id;
const encoder = require('base64-url');

const encoded_id = encoder.encode("AuditLog:000000000004d2e7");
console.log(encoded_id);

获取 AuditLog 对象示例

Query:

{
  node(id: "<BASE64URL_ENCODED_AUDIT_LOG_NODE_ID>") {
    ... on AuditLog {
      id
      data
    }
  }
}

3. 生成 Session 节点类型的 id

package main

import (
    "encoding/base64"
    "fmt"
)

func main() {
    nodeID := base64.RawURLEncoding.EncodeToString([]byte("Session:8891767c-992c-4c09-bbae-9a8337ae661k"))
    fmt.Println(nodeID)
}
import base64

base64.urlsafe_b64encode(b'Session:8891767c-992c-4c09-bbae-9a8337ae661k').replace(b'=', b'')
<?php
function base64url_encode($data) {
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

$encoded_id = base64url_encode("Session:8891767c-992c-4c09-bbae-9a8337ae661k");
echo $encoded_id;
const encoder = require('base64-url');

const encoded_id = encoder.encode("Session:8891767c-992c-4c09-bbae-9a8337ae661k");
console.log(encoded_id);

获取 Session 对象示例

Query:

{
  node(id: "<BASE64URL_ENCODED_SESSION_NODE_ID>") {
    ... on Session {
      id
      displayName
      type
      createdAt
    }
  }
}

4. 生成 Authenticator 节点类型的 id

package main

import (
    "encoding/base64"
    "fmt"
)

func main() {
    nodeID := base64.RawURLEncoding.EncodeToString([]byte("Authenticator:95435c90-8501-4735-a8b6-412601605a25"))
    fmt.Println(nodeID)
}
import base64

base64.urlsafe_b64encode(b'Authenticator:95435c90-8501-4735-a8b6-412601605a25').replace(b'=', b'')
<?php
function base64url_encode($data) {
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

$encoded_id = base64url_encode("Authenticator:95435c90-8501-4735-a8b6-412601605a25");
echo $encoded_id;
const encoder = require('base64-url');

const encoded_id = encoder.encode("Authenticator:95435c90-8501-4735-a8b6-412601605a25");
console.log(encoded_id);

获取 Authenticator 对象示例

Query:

{
  node(id: "<BASE64URL_ENCODED_AUTHENTICATOR_NODE_ID>") {
    id
    ... on Authenticator {
      id
      kind
      createdAt
    }
  }
}

5. 生成 Authorization 节点类型的 id

package main

import (
    "encoding/base64"
    "fmt"
)

func main() {
    nodeID := base64.RawURLEncoding.EncodeToString([]byte("Authorization:12345a90-7801-41d5-c3b6-655501605b56"))
    fmt.Println(nodeID)
}
import base64

base64.urlsafe_b64encode(b'Authorization:12345a90-7801-41d5-c3b6-655501605b56').replace(b'=', b'')
<?php
function base64url_encode($data) {
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

$encoded_id = base64url_encode("Authorization:12345a90-7801-41d5-c3b6-655501605b56");
echo $encoded_id;
const encoder = require('base64-url');

const encoded_id = encoder.encode("Authorization:12345a90-7801-41d5-c3b6-655501605b56");
console.log(encoded_id);

获取 Authorization 对象示例

Query:

{
  node(id: "<BASE64URL_ENCODED_AUTHORIZATION_NODE_ID>") {
    id
    ... on Authorization {
      id
      clientID
      scopes
      createdAt
    }
  }
}

6. 生成 Identity 节点类型的 id

package main

import (
    "encoding/base64"
    "fmt"
)

func main() {
    nodeID := base64.RawURLEncoding.EncodeToString([]byte("Identity:12345a90-7801-41d5-c3b6-655501605b56"))
    fmt.Println(nodeID)
}
import base64

base64.urlsafe_b64encode(b'Identity:12345a90-7801-41d5-c3b6-655501605b56').replace(b'=', b'')
<?php
function base64url_encode($data) {
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

$encoded_id = base64url_encode("Identity:12345a90-7801-41d5-c3b6-655501605b56");
echo $encoded_id;
const encoder = require('base64-url');

const encoded_id = encoder.encode("Identity:12345a90-7801-41d5-c3b6-655501605b56");
console.log(encoded_id);

获取 Identity 对象示例

Query:

{
  node(id: "<BASE64URL_ENCODED_IDENTITY_NODE_ID>") {
    id
    ... on Identity {
      id
      claims
      type
    }
  }
}