Having difficulty learning Gizmo

graphql

#1

So, I’m just really getting rolling with Cayley, and making quite a bit of progress. I think I’m doing okay with loading data, but I’m having a really rough time with querying it back out. GraphQL makes a lot of sense, but it’s inability to do wildcard searches (according to the Cayley documentation) is going to remove it from contention.

So, I started digging into Gizmo, and am having a surprisingly difficult time making the leap. I was hoping that I could show a relatively simple GraphQL query and some kind soul could help me out by providing a Gizmo translation.

{
    myQuery(elementType: "Script"){
        id
        name
        children {
            id
            name
        }
    }
}

What does that look like in Gizmo?

Also, is there a Gizmo equivalent to DGraph’s expand(all), that returns all connected values and outgoing edges? The nature of my data is such that I’m not always going to know ahead of time just what values are going to be available.

Thank you very much for your patience.


#2

Hi! It would be a bit more helpful if you gave a visual representation or a quad file. I have no experience with GraphQL and DGraph so it is hard to answer it without that knowledge. While I could probably help you in the right direction with Gizmo.


#3

Hey, thank you very much for the response.

First, let me say that some of what’s expressed here is obviously not idiomatic for Cayley. The IDs (and elementTypes) should be IRIs rather than strings, and the ID in Cayley terms isn’t really a value, but is the identity of the node. That said, let’s assume that I’m not in charge of defining my graph, and that this is the graph that I was handed.

The above query in GraphQL would return each of the Script nodes, along with the name and id values, and then follow any child edges and return the id and name for any of those found nodes.

expand(_all_) will follow all outgoing edges from a node and return the values or nodes for those edges… I think I found the answer of how to do that from another post, though the answers are surprising complex/verbose for what I would anticipate would be a relatively common operation.

Thank you again for your assistance.


#4

Something like this I would guess:

var path = g.M().Tag('source').Out("Child").Save("name", "name").Save("elementType", "type").Tag("target");
g.V('Script10456').Tag('source')
	.Save("name", "name")
	.Save("elementType", "type")
	.FollowRecursive(path, -1).All()

Note: the ID is used as the subject of the quad: Script456 elementType Script . etc
Note: that I added a child line under Line2 to show the recursive query.


#5

Okay, so I see how some of that is supposed to work, but I’m getting something with only a single layer, rather than JSON with some depth to it. I’m getting:

{
    "result": [
        {
            "id": "Line10456-1",
            "name": "Line1",
            "source": "Script10456",
            "target": "Line10456-1",
            "type": "ScriptStep"
        },
        {
            "id": "Line10456-2",
            "name": "Line2",
            "source": "Script10456",
            "target": "Line10456-2",
            "type": "ScriptStep"
        }
    ]
}

There are some issue here. Using 'name" and 'type" as tags for the values causes the children to stomp on the parent values.

If I change your query to this:

var path = g.M().Tag('source').Out("Child").Save("name", "cName").Save("elementType", "cType").Tag("target");
g.V('Script10456').Tag('source')
    .Save("name", "pName")
    .Save("elementType", "pType")
    .FollowRecursive(path, -1).All()

(effectively tagging the name and type with whether they come form the §arent or the ©hild) I get this, which is closer, but still not quite what I was hoping for:

{
    "result": [
        {
            "cName": "Line1",
            "cType": "ScriptStep",
            "id": "Line10456-1",
            "source": "Script10456",
            "pName": "My Script",
            "pType": "Script",
            "target": "Line10456-1",
        },
        {
            "cName": "Line2",
            "cType": "ScriptStep",
            "id": "Line10456-2",
            "source": "Script10456",
            "pName": "My Script",
            "pType": "Script",
            "target": "Line10456-2",
        }
    ]
}

What I want to see is something like this:

{
    "result": [
        {
            "id": "Script10456",
            "name": "My Script",
            "type": "Script",
            "child":  [
                {
                    "id": "Line10456-1",
                    "name": "Line1",
                    "type": "Line"
                },
                {
                    "id": "Line10456-2",
                    "name": "Line2",
                    "type": "Line"
                }
            ]
        }
    ]
}

Does that make any sense? If the answer is always a flat array, then it will greatly complicate my life if I ever want to pull more than one Script. I’ll have to loop through the whole result array to figure out A) how many scripts I got back, and B) how many lines are in each script.

Once again, my thanks for your help. I have learned a ton based upon the discussion so far. Seriously, geometric factors more understanding of how Gizmo works now than when I wrote the first post.


#6

Maybe schema-lib can help with this nested output. Not sure, haven’t used it. @dennwc does it?
I woud probably just parse it out from the flat output :roll_eyes:


#7

It looks as though schema-lib could handle it. I’m using it for initial population of my quads. To get clean, hierarchical JSON I’d probably have to round-trip it into a struct and then back out into JSON. The only downside is that I have to know the keys/edges at compile-time, not run-time…

Thank you very much for your assistance. If nobody else has any better answers, I will have to run with those you have provided… and gratefully at that.


#8

@Jorgen Yes the schema lib can handle it, but as @werkflo mentioned currently it needs to know edge names upfront. It can potentially expand all predicates, but it’s not yet exposed.

As for previous discussions, Gizmo is not that easy to use with nested inputs. Instead GraphQL can be extended with expand(all) easily, so if you are interested in this kind of functionality, please file a feature request.