"values" is null while doing apoc.map.groupByMulti

query

MATCH (a)
WHERE a.id = "some_id"
        AND (
            a.unmanaged IS NULL	OR
            a.unmanaged = false
        )					// visibility clause
OPTIONAL MATCH (a)-[r]->() 	// resource 'a' with outward relations 'r'

WITH a AS resourceProps,apoc.convert.toList(r) as refList, r

RETURN resourceProps, apoc.map.groupByMulti(refList, r.`_edgeType`) as output1

refList is null for some 'a's, if I return it. But apoc procedure is returning following error

Failed to invoke function apoc.map.groupByMulti: Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "values" is null

for below query

MATCH (a)
WHERE a.id = "saasops"
        AND (
            a.unmanaged IS NULL	OR
            a.unmanaged = false
        )					// visibility clause
OPTIONAL MATCH (a)-[r]->() 	// resource 'a' with outward relations 'r'

WITH a AS resourceProps,apoc.convert.toList(r) as refList, r
WITH resourceProps, refList, r,
CASE
WHEN refList is not null THEN apoc.map.groupByMulti(refList, r.`_edgeType`) END as output1

RETURN resourceProps, output1

output1 is empty {} for all 'a's

I am not sure what your intent of using the to list method is. Are you trying to collect the relations for a given node “a?”

If so, using the collect subquery as demonstrated below will return an empty list when no relationships are found.

If you would have used a cypher collect to create the list of relationships instead of the apoc method, the rows that did not have relationships for their ‘a’ would have been dropped. I assume you are trying to include these.

MATCH (a)
WHERE a.id = "some_id"
        AND (
            a.unmanaged IS NULL	OR
            a.unmanaged = false
        )				
WITH a, collect{ MATCH (a)-[r]->()  RETURN r } as refList
RETURN a as resourceProps, 
apoc.map.groupByMulti(refList, “_edgeType”) as output1

The second parameter to groupByMulti is supposed to be a string that specifies the property used to extract the grouping key from the list passed in the first parameter. I assumed each relationship has an “_edgeType” property and you wanted to group the relations for a given “a” node into listed based on the values of that property.

Let me if know I got it wrong and will try to help if you can explain my misinterpretations.

Thanks @glilienfield

Apologies for leaving room for guess.

Yes a resource can have same relation with multiple resources, and multiple relation types, both at the same time and defined by _edgeType field.

I have my expected output format given below as well

example of resources and relations as follows(with representational notations):-

{
  "resource_id": "resource_entity_type1_id1",
  "entity_type": "resource_entity_type1",
  "name": "name1",
  "age": 384,
  "unmanaged": true
}
{
  "_edgeType": "this_has_that",
  "fromID": "resource_entity_type1_id1",
  "toID": "resource_entity_type2_id1"
}

example of expected output format as follows:-

{
  "resource_entity_type1": {
    "resource_type1_id1": {
      "properties": {
        "some_property_1": "value1",
        "resource_entityType": "resource_entity_type1",
        "resource_id": "resource_type1_id1",
        "other_fields": "other_values"
      },
      "resource_type1_id1_out_relations": {
        "relation_type1": [
          "some_resource_id1",
          "some_resource_id2"
        ],
        "relation_type2": [
          "some_resource_id3"
        ]
      },
      "resource_type1_id2": {
        "properties": {
          "some_property_1": "value2",
          "resource_entityType": "resource_entity_type1",
          "resource_id": "resource_type1_id2",
          "other_fields": "other_values"
        },
        "resource_type1_id1_out_relations": {
          "relation_type1": [
            "some_resource_id4",
            "some_resource_id5"
          ],
          "relation_type2": [
            "some_resource_id6"
          ],
          "other_fields": "other_values"
        }
      }
    }
  },
  "resource_entity_type2": {
    "resource_type2_id1": {
      "properties": {
        "some_property_1": "value3",
        "resource_entityType": "resource_entity_type2",
        "resource_id": "resource_type2_id1",
        "other_fields": "other_values"
      },
      "resource_type2_id1_out_relations": {
        "relation_type3": [
          "some_resource_id7",
          "some_resource_id8"
        ],
        "relation_type4": [
          "some_resource_id9"
        ],
        "relation_type5": []
      },
      "resource_type2_id2": {
        "properties": {
          "some_property_1": "value4",
          "resource_entityType": "resource_entity_type2",
          "resource_id": "resource_type2_id2",
          "other_fields": "other_values"
        },
        "resource_type2_id1_out_relations": {
          "relation_type3": [
            "some_resource_id1",
            "some_resource_id10",
            "some_resource_id11"
          ],
          "relation_type4": [
            "some_resource_id3"
          ],
          "relation_type5": [
            "some_resource_id4"
          ]
        }
      }
    }
  }
}

I hope it is possible to achieve.

Do you have a cypher query I can run to give me a good representation of your data? I am finding it difficult to visualize from your parameterized example. I can also use it to test a solution.

If you can provide some test data, can you also so the output too?