Ejemplo n.º 1
0
def get_tree_with_depth(uid: str, depth: int = 2) -> Optional[types.Tree]:
    query = """
        query tree($uid:string){
            tree(func: uid($uid) ) @recurse(depth:__depth__)@filter(has(type)) {
            uid
            procedure
            madeOf
          }
        }
    """.replace('__depth__', str(depth))

    variables = {'$uid': uid}
    data = execute_query(query, variables)
    if tree := data['tree']:  # tree is empty list
        return types.Tree(**tree[0])
Ejemplo n.º 2
0
def get_node(uid: str) -> Optional[types.Tree]:
    """
    query node for given uid, a dict will be returned
    """

    query = """
    query all($uid: string) {
        node(func: uid($uid))@filter(has(type)) {
            uid
            name
            procedure
        }
    }"""

    variables = {'$uid': uid}
    data = execute_query(query, variables)
    if node := data['node']:  # node is empty list
        return types.Tree(**node[0])
Ejemplo n.º 3
0
def list_tree_with_depth(uids: List[str],
                         depth: int = 2) -> Optional[List[types.Tree]]:
    """
    query tree for each uid in uids, a list of tree will be returned
    note: this will only query for 1 depth for node edge!

    github issue: https://github.com/dgraph-io/dgraph/issues/2726
    """
    query = """
        query trees($uids: string) {
              trees(func: uid($uids)) @recurse(depth:__depth__)@filter(has(type)) {
              uid
              name
              procedure
              madeOf
            }
        }
    """.replace('__depth__', str(depth))
    uids = '[' + ','.join(uids) + ']'
    variables = {'$uids': uids}
    data = execute_query(query, variables)
    if trees := data['trees']:
        return [types.Tree(**tree) for tree in trees]
Ejemplo n.º 4
0
def get_tree(uid: str) -> Optional[types.Tree]:
    """
    query tree for a single node, a tree dict will be returned
    return:
        {
        "uid": "0x11183",
        "procedure": 1,
        "madeOf": [
            {
                "uid": "0x1117b",
                "name": "原料4",
                "type": "RAW",
                "rate": "1/2"
            },
            {
                "uid": "0x1117c",
                "name": "原料5",
                "type": "RAW",
                "rate": "3/40"
            }
        ]
    }
    """
    query = """
        query tree($uid:string){
            tree(func: uid($uid) ) @recurse @filter(has(type)) {
            uid
            procedure
            madeOf
          }
        }
    """

    variables = {'$uid': uid}
    data = execute_query(query, variables)
    if tree := data['tree']:  # tree is empty list
        return types.Tree(**tree[0])