Skip to main content

Creating query like cognite UI's query by SDK

  • December 9, 2024
  • 7 replies
  • 79 views

Forum|alt.badge.img

Hi Everyone,
I need to create query like it is creating on cognite UI’s  like 
 


 

{
      "listEntity": {
        "with": {
          "0": {
            "limit": 50,
            "nodes": {
              "filter": {
                "and": [
                  {
                    "matchAll": {}
                  },
                  {
                    "hasData": [
                      {
                        "type": "view",
                        "space": "slb-pdm-dm-governed",
                        "externalId": "Entity",
                        "version": "1_7"
                      }
                    ]
                  },
                  {
                    "or": [
                      {
                        "and": [
                          {
                            "nested": {
                              "scope": [
                                "slb-pdm-dm-governed",
                                "Entity/1_7",
                                "parent"
                              ],
                              "filter": {
                                "in": {
                                  "property": [
                                    "node",
                                    "externalId"
                                  ],
                                  "values": [
                                    "1234"

                                  ]
                                }
                              }
                            }
                          },

                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            }
          },
          "0_2": {
            "limit": 10000,
            "nodes": {
              "from": "0",
              "through": {
                "source": {
                  "type": "view",
                  "space": "slb-pdm-dm-governed",
                  "externalId": "Entity",
                  "version": "1_7"
                },
                "identifier": "parent"
              },
              "direction": "outwards",
              "filter": {
                "and": [
                  {
                    "matchAll": {}
                  },
                  {
                    "hasData": [
                      {
                        "type": "view",
                        "space": "slb-pdm-dm-governed",
                        "externalId": "Entity",
                        "version": "1_7"
                      }
                    ]
                  }
                ]
              }
            }
          }
        },
        "select": {
          "0": {
            "sources": [
              {
                "source": {
                  "type": "view",
                  "space": "slb-pdm-dm-governed",
                  "externalId": "Entity",
                  "version": "1_7"
                },
                "properties": [
                  "location",
                  "entityType",
                  "parent"
                ]
              }
            ]
          },
          "0_2": {
            "sources": [
              {
                "source": {
                  "type": "view",
                  "space": "slb-pdm-dm-governed",
                  "externalId": "Entity",
                  "version": "1_7"
                },
                "properties": [
                  "location",
                  "entityType",
                  "parent"
                ]
              }
            ]
          }
        },
        "includeTyping": true
      }
    }
  }
}

The same we need to create it from SDK method but we couldn’t find any method to create same query rather we have created query like below using sdk which fetched first all data of entity then there nested veiw, not like above query which fetches only required data.
Query from sdk

{
    "with": {
        "Entity": {
            "nodes": {
                "filter": {
                    "hasData": [
                        {
                            "type": "view",
                            "space": "slb-pdm-dm-governed",
                            "externalId": "Entity",
                            "version": "1_7"
                        }
                    ]
                },
                "chain_to": "destination",
                "direction": "outwards"
            },
            "limit": 10000
        },
        "parent.Entity": {
            "nodes": {
                "from": "Entity",
                "filter": {
                    "and": [
                        {
                            "hasData": [
                                {
                                    "type": "view",
                                    "space": "slb-pdm-dm-governed",
                                    "externalId": "Entity",
                                    "version": "1_7"
                                }
                            ]
                        },
                        {
                            "in": {
                                "property": [
                                    "node",
                                    "externalId"
                                ],
                                "values": [
                                    "1234"
                                ]
                            }
                        }
                    ]
                },
                "through": {
                    "source": {
                        "space": "slb-pdm-dm-governed",
                        "external_id": "Entity",
                        "version": "1_7",
                        "type": "view"
                    },
                    "identifier": "parent"
                },
                "chain_to": "destination",
                "direction": "outwards"
            },
            "limit": 10000
        }
    },
    "select": {
        "Entity": {
            "sources": [
                {
                    "source": {
                        "space": "slb-pdm-dm-governed",
                        "external_id": "Entity",
                        "version": "1_7",
                        "type": "view"
                    },
                    "properties": [
                        "name",
                        "parent"
                    ]
                }
            ]
        },
        "parent.Entity": {
            "sources": [
                {
                    "source": {
                        "space": "slb-pdm-dm-governed",
                        "external_id": "Entity",
                        "version": "1_7",
                        "type": "view"
                    },
                    "properties": [
                        "name"
                    ]
                }
            ]
        }
    },
    "cursors": {
        "Entity": "Z0FBQUFBQm5VVWJadTBGeTlGdVN4MXlRa0N4b3BEaGhJMTdEMmhmTzVQbjU5YVFMWUFtUUhncjlVTFBOOGxpQnYyS3BYMlZRcGR1SVk0TjJLWkc5eVVSdGVFYlB2NF9lU3FqdWZiUkxUYUtDZXhxYUF4N0Nac3d6d2g3SkhOeDhXcE1ZM3BsRUd4cG1fd01SalFPbEk0dGtjZ1dMdHJVVEQ2dGpvVzY3ZTdFQ0pxSlhxRFVFbVlldk51SktOMEhFM3FGdm1YQ2hIY293TTQ2eXRIUkxSckpCbTZfTWJOaXFIemYwRGZINzlLQV9vdk1PMGZlc1FidzR2aXlVWk5TenFhXzEwSVRsSXg0MA==",
        "parent.Entity": "Z0FBQUFBQm5VVWJaYjgzTjBLNFhhckNIZmFEREJIakR3bXp3eDJmNnBVY2NiWkFDS0xBR3JlbTNiQkpfVk5XY2ltSFl2Z2UyRWtBQVpGdDZQdUczOXRkdzdsN0tXLTBsdlRnMnhldXpFdGxLNzVENFVicUM3NVlxeVpUX0RLYk9ubUJwejJ6dDFtZXZDT0tMOVhTSW05blBQZ2JSR25lX1hHamxpUmFJWXdwUTdHU3d6SnA5ZGRneVdtYzZWVHhUVzVQTS1FTS1sdGwtSlZhR29QSW1xcHNlblFPeU5aRzhQeHdTOXNXLTlWaWZkUEZzYm5nU0hDd0N1OGJvWTB4amg3ZnZVTE5hYWM5Nw=="
    }
}


Please suggest a way to create query like Cognite.

7 replies

Mithila Jayalath
Seasoned Practitioner
Forum|alt.badge.img

@Shweta I will check on this with the engineering team and get back to you.


Forum|alt.badge.img
  • Author
  • Committed
  • 6 replies
  • December 10, 2024

@Mithila Jayalath  Okay sure, thank you. Please let me know.


Forum|alt.badge.img

One clarifying question from me, you specifically want to use client.data_modeling.instances.query(...)? Or can you adapt the GraphQL and use client.data_modeling.graphql.query(...) instead?


Forum|alt.badge.img
  • Author
  • Committed
  • 6 replies
  • December 10, 2024

Thank you ​@Håkon V. Treider for question, 
@Neerajkumar Bhatewara if you can suggest what will be the suitable option.


Hello ​@Håkon V. Treider ,
We were recommended by Cognite earlier to use the instances query for the performance reasons. As a result, we are using the same as against graphql.query or graphql endpoint.

So to answer your query we want to use the instances.query as of now.

On the sides, what is recommended option in general out of the two and the differences?

 

Regards,
Neeraj B


Forum|alt.badge.img

I agree with that recommendation. Using the GQL endpoints are fine for UIs and similar, where a small-ish subset is used or shown to the user, while instances/query should be used when dealing with larger amounts of data, e.g. like a background worker.

By looking at your GraphQL query, is it correct to assume that Entity extends CogniteAsset? And that you are trying to list out sub-assets (or sub-entities) below a list of certain parents (given by the in-filter)?


Forum|alt.badge.img

If the answer to the above question is yes, I think a simpler way than following edges is to just filter nodes using the “equals” filter on parent - or in your case, using the “in” filter


Cookie Policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie Settings