Adding edge facet is not working

Moved from GitHub pydgraph/136

Posted by robin-snt:

Dgraph: v20.03.1
pydgraph: 20.3.0

First I tried using json format, which didn’t work then I tried N-quads which is mutated using the following function:

def set_facet(client, src_uid, trg_uid, predicate, facets):
    '''Set edge facets

    :param client: dgraph client object
    :param src_uid: dgraph source node UID
    :type src_uid: str
    :param trg_uid: dgraph source node UID
    :type trg_uid: str
    :param predicate: dgraph edge predicate
    :type predicate: str
    :param facets: facet key, value tuple
    :type facets: ((str, str|bool|int|float), ...)
    '''

    nquads = '\n'.join(f'<{src_uid}> <{predicate}> <{trg_uid}> '
                       f'({k}={json.dumps(v)}) .' for k, v in facets)

    print('FACET:', nquads)
    txn = client.txn()

    try:
        txn.mutate(set_nquads=nquads, commit_now=True)
    finally:
        txn.discard()

I execute my python script and verify that the nquad is logged to stdout:
FACET: <0x13a4a> <result> <0x13a24> (cached=true) .

… and inspect the graph:

Screenshot from 2020-05-29 16-57-55

No facets to be seen, and I know the function executed. When I copy-paste the n-quad from stdout and execute a mutation query in ratel:

{
  set {
    <0x13a4a> <result> <0x13a24> (cached=true) .
  }
}

, the graph is mutated:

martinmr commented :

I added a test for facet mutation in Add test for mutation with facets. by martinmr · Pull Request #146 · dgraph-io/pydgraph · GitHub and I am able to mutate with facets. I tested master as well as the 20.03.1 release.

There are two possible causes for your issue that I can think of:

  1. Some issue with rate. I’d recommend using the client or ratel to retrieve the node instead of the graphical interface.
  2. Some issue with the encoding of your nquads. To discard this option, I’d suggest testing your code with a hardcoded mutation.

robin-snt commented :

Thank you for the feedback.

I added a test for facet mutation in #146 and I am able to mutate with facets.

I would argue that with the current mess that dgraph facets are in right now, there are different ways of handling facets and it seems to me that the test you added doesn’t cover all the different cases. However, the difference between the facets in that test and my situation is that I am adding facets to existing nodes, and the test is simply writing a new subgraph containing facets on some edges. I also think there should be a test for dealing with facets for schema type [uid].

I’d recommend using the client or ratel to retrieve the node instead of the graphical interface.

Isn’t ratel and the GUI the same?

I’d suggest testing your code with a hardcoded mutation.

I’m not sure this point is valid, because I print the encoded nquad to stdout and successfully run it in ratel.

EDIT: misread the test and see that it does indeed mutate.

martinmr commented :

Sorry, by not using the graphical interface I meant not using the node explorer and instead writing a query to see what you get back in the JSON response. If the mutations are present in the JSON response, then the issue would be in the node explorer.

I’ll see what additional cases I can. However, I doubt the issue is in pydgraph itself as the client is just sending the mutations and does not modify the input.

EDIT: I see the test already checks that facets to uid predicates work correctly.