Complex GraphQL Query Help

Hi All

Im not familiar with GraphQL. I can do some basic queries, but the query i want to do now is quite complex, and I’m struggling to write one.

I’ve used the beta connectors to upload some Civil 3D corridors, and im now using Python to try and download some of the data.

Could somebody help me with a query that would do the following?

Find all objects that have a “speckle_type” property equal to “Base”, the “type” equal to “baseline”, and “alignmentId” = “abc123”

This part i can do with the following code:

query($myQuery: [JSONObject!]) {
        project(id: "f0447c2f94") {
            object(id: "2113be889574eb3362b67e6d199ca940") {
                id
                children(query: $myQuery) {
                    totalCount
                    objects {
                        data
                    }
                }
            }
        }
    }

Variables:

{
        "myQuery": [
            {
                "field": "speckle_type",
                "value": "Base",
                "operator": "="
            },
            {
                "field": "type",
                "value": "Baseline",
                "operator": "="
            },
            {
                "field": "alignmentId",
                "value": "86104",
                "operator": "="
            }
        ]
    }

This returns the following json results (ive heavily trimmed the data to just show the relevant parts i need to work with:

{
  "data": {
    "project": {
      "object": {
        "id": "2113be889574eb3362b67e6d199ca940",
        "children": {
          "totalCount": 1,
          "objects": [
            {
              "data": {
                "id": "4e4e5e5ddb0693187e66b23f6995e8cd",
                "name": "BL_XL_M60_EB_HS_OS_0004",
                "type": "Baseline",
                "units": "m",
                "elements": [
                  {
                    "id": "6aaa0892b32e1b3c729b62d8e318c1f0",
                    "name": "RG_A-M60-EB-ML-01_1",
                    "type": "BaselineRegion",
                    "units": "m",
                    "assembly": {
                      "id": "81d075ec446a89c3f1da5e3774a23165",
                      "name": "A-M60-EB-ML-01",
                      "type": "Assembly",
                      "speckle_type": "Base",
                      "applicationId": "77318",
                      "subassemblies": [
						"..."
                      ]
                    },
                    "endStation": 1057.5616426297522,
                    "speckle_type": "Base",
                    "startStation": 1042.8951959641888,
                    "applicationId": "989d2574-94f8-4aea-98cb-324879974315",
                    "appliedAssemblies": {
                      "1050": {"..." : "..."},
					  "1060": {"..." : "..."},
					  "1070": {"..." : "..."}
					  }
				  }]
			  }
			}
			]
		}
	  }
	}
  }
}

I now need to amend my GraphQL query so that within the list of “elements” of the found “baseline” objects, it only returns elements which have a “type” of “BaselineRegion”.
Of those “BaselineRegion” objects, i want to go into the “appliedAssemblies” property and then return just the keys of the dictionary. So in the result above, i just want the following list of numbers returned
[ “1050”, “1060”, “1070”]
There could be multiple “Baseline” objects found, and more than likely will be multiple “baselineRegion” objects within them, so im expecting a list of lists to be returned
Is this query possible?

At the moment im just getting the list of baselines using GraphQL and then using python to travel through the tree to et the information i want, but the GraphQL call is returning far more information that i actually need (there is a huge amount of data within the “appliedAssemblies” and i dont need any of it other than the keys in the dictionary.

Any help appreciated!

hey @jhdempsey86

you say you are writing some code in python, is there any reason, why you would not want to receive the root object with its full children tree and run the filtering login on native Base objects?

Receive docs can be found here

And here’s an example, how to run a simple flatten on a base object, to get an iterable of its object tree.

Hope this helps.

We are trying to speed up and optimise the tool we have built as much as possible, and the first way is to only pull the data we need.

At the moment, i am just pulling the whole root “Baseline” object using GraphQL and then searching through it with python. But there is a lot of data in those “Baseline” objects of which 99.9% we have no use for (The GraphQL currently returns 843582 lines when “pretty printed”, but saved as a text file is almost 50MB) and we only need a tiny part of it

Another way to skinny down the data in the GraphQL query is to apply a select: [string,] where each string is a property you want to retrieve, thereby omitting anything not specified. Crafting exactly which properties to keep may take a little work but would reduce the data “horizontally” as well as the query reduces it “vertically”.

A worked example of select with query
Query:

query Objects( $objectId: String!, 
                        $projectId: String!, 
                        $select: [String], 
                        $query: [JSONObject!] ) {
  project(id: $projectId) {
    object(id: $objectId) {
      children(select: $select, query: $query, depth: 2) {
        totalCount
        objects {
          data
        }
      }
    }
  }
}

Variables:

{
  "limit": 2,
  "objectId": "571464cfa994ba847679f49921e78866",
  "projectId": "3b81e5731b",
  "query": [
    {
      "field":"category",
      "value":"Floors",
      "operator":"="
    }],
  "select": [
    "level",
    "properties.\"Material Quantities\".*.volume",
    "properties.\"Material Quantities\".*.materialName",
    "properties.\"Material Quantities\".*.area"
  ]
}

Snipped Result:

{
  "data": {
    "project": {
      "object": {
        "children": {
          "totalCount": 460,
          "objects": [
            {
              "data": {
                "level": {
                  "name": "4FL",
                  "units": "mm",
                  "elevation": 12900.000000000002
                },
                "properties": {
                  "\"Material Quantities\"": {
                    "*": {
                      "volume": 2075765974.8735332,
                      "materialName": "Fc24",
                      "area": 11532032.976136567
                    }
                  }
                },
                "id": "005da37a4c357a4dce8c215308fdf39d"
              }
            },
            ...
           ]}}}

We don’t support getting down to an individual property on an individual object unless you are lucky enough that your data is structured “just right”—it is anticipated that you would post-process as @gergo suggested.

1 Like