Multi Queries with same query name - (Unable to marshal response)

Hi, I have a simple graph of pets. Pets have a type (cat, dog) etc. The thing is dogs have a ‘Name’ but cats have ‘Nickname’. I can use aliases and normalize to query for both types using multiple queries and return an array of the results. i.e. the following query works really well:

{
  myPets(func: eq(type, "dog")) @normalize {
		Body {
                    Name : Name
                   Age: Age
                }
  }
  myPets(func: eq(type, "cat")) @normalize {
		Body {
                    Name : Nickname
                    Age : Age
                }
      }
}

and gives me this result

  "data": {
    "myPets": [
      {
        "Age": 10,
        "Name": "foo",
        "uid": "0x7d4a"
      },
      {
        "Age": 20,
        "Name": "bar",
        "uid": "0x7d4c"
      }
    ]
  }

Now if I decide to look for other types that don’t exist yet - lets say a lizard I get an error.

{
  myPets(func: eq(type, "dog")) @normalize {
		Body {
                    Name : Name
                   Age: Age
                }
  }
  myPets(func: eq(type, "cat")) @normalize {
		Body {
                    Name : Nickname
                    Age : Age
                }
      }
      myPets(func: eq(type, "lizard")) @normalize {
		Body {
                    Name : Nickname
                    Age : Age
                }
      }
}
Unable to marshal response

I noticed that if I give it a unique query name it succeeds, but then I get a seperate array i.e.
If I add this to my above query

{
  myPets(func: eq(type, "dog")) @normalize {
		Body {
                    Name : Name
                   Age: Age
                }
  }
  myPets(func: eq(type, "cat")) @normalize {
		Body {
                    Name : Nickname
                    Age : Age
                }
      }
      other(func: eq(type, "lizard")) @normalize {
		Body {
                    Name : Anyname
                    Age : Age
                }
      }
}

I get this response

  "data": {
    "myPets": [
      {
        "Age": 10,
        "Name": "foo",
        "uid": "0x7d4a"
      },
      {
        "Age": 20,
        "Name": "bar",
        "uid": "0x7d4c"
      }
    ],
    "other": []
  },

Is there any way to normalize with multiple queries, and merge the result ?

Hi.

We need more details. Please share an example of mutation. There is something curious about your query. Does Edge “Body {” really exist? What is the purpose? and why do changed from “type” predicate to “pdType”?

Cheers.

Sorry that was a cut - and paste error, for both the type and response.
I was trying to create a simple version of my app.

Here is the mutation

{
	"ID": "12345",
	"type": "dog",
	"Body": {
		"Age": 10,
		"Name": "foo"
	}
}{
	"ID": "678920",
	"type": "cat",
	"Body": {
		"Age": 20,
		"Nickname": "bar"
	}
}

What I’m trying to do is normalize the results from the two different queries into one set of results.

I get it all.

Hey @gus I think we may have a issue here.

To summarize The Opera, is that if we use multiple queries (with the same name) as presented in Thread. And one of them does not return anything (nill). Dgraph returns an error of “Unable to marshal response” - I believe he should ignore.

@codegraph I think you could change your query a bit. To achieve the same result. Like:

But you should use “Name” in all animals instead of “Nickname”.

{
  myPets(func: has(type)) 
  @filter(eq(type, "dog") OR eq(type, "cat") OR eq(type, "lizard")) @normalize {
		Body {
           Name: Name
           Age: Age
           name : Nickname
         }
  }
}

Thanks - the above query works, but the problem is for cat’s I get “name” instead of “Name”. With JSON being case sensitive, the keys are technically different.
I was hoping normalization would solve this problem for me.

Do you see it as a bug - I mean would you expect the original query to work ?

Thanks for looking into this.

What I mentioned in relation to this is that there can not be two predicates with the same Alias in the same block.

Normalization works perfectly with this in different queries. But not in the example I gave you.

In part yes, because Dgraph merges the queries and this merge should be independent of one of the queries succeeding or not in my view.

Thanks I appreciate the responses.
Should I raise a issue on github for this - or is there a better way to track it?

Let’s see what Gus states for this. But if you really need it, feel free to fill up an issue.

Hey CG,

Turns out that we find that this usage is wrong and will be enforced not to use like that.
I recommend using the approach I mentioned upthere.

issue: Enforce uniqueness naming for query blocks · Issue #2675 · dgraph-io/dgraph · GitHub

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.