Graph API
Graph API Development Server Guide
Example Queries
Get Graphs by ID
- Query
- Response
{
Graph(id: 1) {
id
name
nodes: Nodes {
id
onCanvasId
fields
}
links: Links {
id
onCanvasId
source
target
fields
}
}
}
{
"data":{
"Graph":{
"id":"1",
"name":"My Graph",
"nodes":[
{
"id":"100",
"onCanvasId":"04308cf6-510c-4326-a6d6-2cae1850090d",
"fields":{
"name":"Nexus Graph"
}
},
{
"id":"101",
"onCanvasId":"2e8a1c17-1d86-4896-aa6f-945c113ca4d2",
"fields":{
"name":"People's lives"
}
}
],
"links":[
{
"id":"1000",
"onCanvasId":"bf154640-0a86-42da-81ca-bbc21dad3e0c",
"source":"04308cf6-510c-4326-a6d6-2cae1850090d",
"target":"2e8a1c17-1d86-4896-aa6f-945c113ca4d2",
"fields":{
"type":"changes"
}
}
]
}
}
}
Creating A New Graph
Creating a graph in dev server is accomplished in 3 separate request in order. The cost of atomicity reduces the complexity of the test API:
-
Creating a graph
注意Unlike real database, the graph ID here starts with 0. Be careful when using it in conditional. For example, it is not a good idea to do
return graph.id ? this.update(graph) : this.save(graph);
Instead, do
return graph.id == undefined ? this.save(graph) : this.update(graph)
- Request
- Response
mutation {
createGraph(name: "My Graph", created_on:"2024-07-15", last_updated_on: "2024-07-15", user_id: 10000) {
id
name
created_on
last_updated_on
user_id
}
}{
"data":{
"createGraph":{
"id":"2",
"name":"My Graph",
"created_on":"2024-07-15T00:00:00.000Z",
"last_updated_on":"2024-07-15T00:00:00.000Z",
"user_id":"10000"
}
}
} -
Creating all nodes of the graph
提示We can use some tool that quickly generates the
onCanvasId
for us- Request
- Response
mutation {
createManyNode(data: [
{
onCanvasId: "3acf79c6-8533-43d9-8ddf-f2f87619f779",
fields: "{\"name\": \"my source node\"}",
graph_id: 2,
},
{
onCanvasId: "b2e39487-854f-4416-a56c-ac1ff7484d92"
fields: "{\"name\": \"my target node\"}",
graph_id: 2
}
]) {
id
onCanvasId
fields
graph_id
}
}{
"data":{
"createManyNode":[
{
"id":"102",
"onCanvasId":"3acf79c6-8533-43d9-8ddf-f2f87619f779",
"fields":"{\"name\": \"my source node\"}",
"graph_id":"2"
},
{
"id":"103",
"onCanvasId":"b2e39487-854f-4416-a56c-ac1ff7484d92",
"fields":"{\"name\": \"my target node\"}",
"graph_id":"2"
}
]
}
} -
Creating all links of the graph
- Request
- Response
mutation {
createManyLink(data: [
{
onCanvasId: "0a784293-e8a1-4e0d-b1dc-14a53194f1e8",
source: "3acf79c6-8533-43d9-8ddf-f2f87619f779",
target: "b2e39487-854f-4416-a56c-ac1ff7484d92",
fields: "{\"type\": \"my link\"}",
graph_id: 2,
}
]) {
id
onCanvasId
source
target
fields
}
}{
"data":{
"createManyLink":[
{
"id":"1001",
"onCanvasId":"0a784293-e8a1-4e0d-b1dc-14a53194f1e8",
"source":"3acf79c6-8533-43d9-8ddf-f2f87619f779",
"target":"b2e39487-854f-4416-a56c-ac1ff7484d92",
"fields":"{\"type\": \"my link\"}"
}
]
}
}
Updating An Existing Graph
Updating graph metadata:
Test dev server is backed by json-graphql-server, which doesn't support nested query. Therefore we cannot update nodes and links in one-shot.
- Request
- Response
mutation {
updateGraph(
id: 3,
name: "My New Graph",
) {
id
name
last_updated_on
}
}
{
"data":{
"updateGraph":{
"id":"3",
"name":"My New Graph"
}
}
}
Updating a single node:
- Request
- Response
mutation {
updateNode(
id: 102,
onCanvasId: "3acf79c6-8533-43d9-8ddf-f2f87619f779",
fields: "{\"name\": \"my updated node\"}"
) {
id
onCanvasId
fields
graph_id
}
}
{
"data":{
"updateNode":{
"id":"102",
"onCanvasId":"3acf79c6-8533-43d9-8ddf-f2f87619f779",
"fields":"{\"name\": \"my new node\"}",
"graph_id":"2"
}
}
}
Creating a single node:
When we add a new node to an exiting graph, it's essentially updating the graph
- Request
- Response
mutation {
createNode(
onCanvasId: "4f6228fe-2b6f-49aa-b0a8-da3be27b8de8",
fields: "{\"name\": \"my new node\"}",
graph_id: 2
) {
id
onCanvasId
fields
graph_id
}
}
{
"data":{
"createNode":{
"id":"10111",
"onCanvasId":"4f6228fe-2b6f-49aa-b0a8-da3be27b8de8",
"fields":"{\"name\": \"my new node\"}",
"graph_id":"2"
}
}
}
Updating a single link:
- Request
- Response
mutation {
updateLink(
id: 1001,
onCanvasId: "0a784293-e8a1-4e0d-b1dc-14a53194f1e8",
source: "3acf79c6-8533-43d9-8ddf-f2f87619f779",
target: "b2e39487-854f-4416-a56c-ac1ff7484d92",
fields: "{\"name\": \"my updated link\"}"
) {
id
onCanvasId
source
target
fields
graph_id
}
}
{
"data": {
"updateLink": {
"id": "1001",
"onCanvasId": "0a784293-e8a1-4e0d-b1dc-14a53194f1e8",
"source": "3acf79c6-8533-43d9-8ddf-f2f87619f779",
"target": "b2e39487-854f-4416-a56c-ac1ff7484d92",
"fields": "{\"name\": \"my new link\"}",
"graph_id": "2"
}
}
}
Creating a single link:
When we connect two existing nodes, it's essentially updating the graph
- Request
- Response
mutation {
createLink(
onCanvasId: "cb7ee059-8356-4bea-a203-a40c518a1863",
source: "3acf79c6-8533-43d9-8ddf-f2f87619f779",
target: "b2e39487-854f-4416-a56c-ac1ff7484d92",
fields: {name: "my new link"},
graph_id: 2
) {
id
onCanvasId
source
target
fields
graph_id
}
}