Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@ txn.mutate(set_obj=p)
# txn.mutate(set_nquads='_:alice <name> "Alice"')
```

```python
# Delete data.

query1 = """query all($a: string)
{
all(func: eq(name, $a))
{
uid
}
}"""

variables1 = {'$a': 'Bob'}

res1 = client.query(query1, variables=variables1)

ppl1 = json.loads(res1.json)

#For mutation to delete node, use this:
txn.mutate(del_obj= person)
```

For a more complete example with multiple fields and relationships, look at the
[simple] project in the `examples` folder.

Expand Down
83 changes: 81 additions & 2 deletions examples/simple/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def drop_all(client):
def set_schema(client):
schema = """
name: string @index(exact) .
friend: uid @reverse .
age: int .
married: bool .
loc: geo .
Expand Down Expand Up @@ -78,11 +79,45 @@ def create_data(client):
print('All created nodes (map from blank node names to uids):')
for uid in assigned.uids:
print('{} => {}'.format(uid, assigned.uids[uid]))
print()
finally:
# Clean up. Calling this after txn.commit() is a no-op
# and hence safe.
txn.discard()
print('\n')


#Deleting a data
def delete_data(client):
# Create a new transaction.
txn = client.txn()
try:
query1 = """query all($a: string)
{
all(func: eq(name, $a))
{
uid
}
}"""
variables1 = {'$a': 'Bob'}
res1 = client.query(query1, variables=variables1)
ppl1 = json.loads(res1.json)
for person in ppl1['all']:
print('Query to find Uid for Bob :')
print(query1)
print('\n')
print("Bob's UID : ")
print(person)
print('\n')
print('Bob deleted')
print('\n')


assigned = txn.mutate(del_obj= person)

txn.commit()

finally:
txn.discard()


# Query for data.
Expand Down Expand Up @@ -112,8 +147,48 @@ def query_data(client):

# Print results.
print('Number of people named "Alice": {}'.format(len(ppl['all'])))
print('\n')
for person in ppl['all']:
print('Query for Alice : \n' +query)
print('\n')
print('Result :')
print(person)
print('\n')

#Query to check for deleted node
def query_data01(client):
query01 = """query all($b: string)
{ all(func: eq(name, $b))
{ uid,
name,
age
friend
{
uid,
name,
age
}
~friend
{
uid,
name,
age
}
}
}"""

variables01 = {'$b': 'Bob'}
res01 = client.query(query01, variables=variables01)
ppl01 = json.loads(res01.json)

print('Number of people named "Bob": {}'.format(len(ppl01['all'])))
print('\n')
for person in ppl01['all']:
print('Query for Bob :\n' + query01)
print('\n')
print('Result :')
print(person)
print('\n')


def main():
Expand All @@ -122,7 +197,11 @@ def main():
drop_all(client)
set_schema(client)
create_data(client)
query_data(client)
query_data(client) # query for Alice
query_data01(client) # query for Bob
delete_data(client) # delete Bob
query_data(client) # query for Alice
query_data01(client) # query for Bob

# Close the client stub.
client_stub.close()
Expand Down