@@ -2,41 +2,116 @@ import * as dgraph from "../../src";
22
33import { setSchema , setup } from "../helper" ;
44
5+ let client : dgraph . DgraphClient ;
6+
7+ async function performUpsert ( mu : dgraph . Mutation , query : string , vars : object ) {
8+ const req = new dgraph . Request ( ) ;
9+ req . addMutations ( mu ) ;
10+ req . setQuery ( query ) ;
11+ req . setCommitNow ( true ) ;
12+ if ( vars !== undefined ) {
13+ const varsMap = req . getVarsMap ( ) ;
14+ req . setQuery ( query ) ;
15+ Object . keys ( vars ) . forEach ( ( key : string ) => {
16+ varsMap . set ( key , vars [ key ] ) ;
17+ } ) ;
18+ }
19+ await expect ( client . newTxn ( ) . doRequest ( req ) ) . resolves . toBeDefined ( ) ;
20+ }
21+
22+ async function performMutation ( mu : dgraph . Mutation , blankNodeLabel : string ) : Promise < string > {
23+ mu . setCommitNow ( true ) ;
24+ const res = await client . newTxn ( ) . mutate ( mu ) ;
25+ const uid = res . getUidsMap ( ) . get ( blankNodeLabel ) ;
26+ expect ( uid ) . not . toEqual ( "" ) ;
27+ return uid ;
28+ }
29+
30+ async function performJsonMutation ( jsonObj : object , blankNodeLabel : string ) : Promise < string > {
31+ const mu = new dgraph . Mutation ( ) ;
32+ mu . setSetJson ( jsonObj ) ;
33+ return performMutation ( mu , blankNodeLabel ) ;
34+ }
35+
36+ async function performNquadMutation ( nquads : string , blankNodeLabel : string ) : Promise < string > {
37+ const mu = new dgraph . Mutation ( ) ;
38+ mu . setSetNquads ( nquads ) ;
39+ return performMutation ( mu , blankNodeLabel ) ;
40+ }
41+
42+ async function performNquadDeletion ( nquads : string , blankNodeLabel : string ) : Promise < string > {
43+ const mu = new dgraph . Mutation ( ) ;
44+ mu . setDelNquads ( nquads ) ;
45+ return performMutation ( mu , blankNodeLabel ) ;
46+ }
47+
48+ async function checkIntegrity ( updatedProfile : Object , query : string , vars ?: object ) {
49+ const res = await client . newTxn ( ) . queryWithVars ( query , vars ) ;
50+ const receivedObject = res . getJson ( ) . all [ 0 ] ;
51+ expect ( receivedObject ) . toEqual ( updatedProfile ) ;
52+ }
53+
54+ async function upsertDeletionWithVars ( ) : Promise < void > {
55+ const jsonObj = {
56+ uid : "_:prashant" ,
57+ name : "Prashant" ,
58+ "dgraph.type" : "Person" ,
59+ } ;
60+ await performJsonMutation ( jsonObj , "prashant" ) ;
61+ const expectedObj = {
62+ name : "Prashant" ,
63+ } ;
64+ const query = `{
65+ all(func: has(name)) {
66+ name
67+ }
68+ }` ;
69+ await checkIntegrity ( expectedObj , query ) ;
70+ const deleteJsonObj = {
71+ uid : "uid(user)" ,
72+ } ;
73+ const query2 = `query all($userName: string) {
74+ user as all(func: eq(name, $userName))
75+ }` ;
76+ const vars = {
77+ $userName : "Prashant" ,
78+ } ;
79+ const mu = new dgraph . Mutation ( ) ;
80+ mu . setDeleteJson ( deleteJsonObj ) ;
81+ await performUpsert ( mu , query2 , vars ) ;
82+ await checkIntegrity ( undefined , query ) ;
83+ }
84+
585describe ( "delete" , ( ) => {
686 it ( "should delete node" , async ( ) => {
7- const client = await setup ( ) ;
87+ client = await setup ( ) ;
888
9- let mu = new dgraph . Mutation ( ) ;
10- mu . setSetNquads ( '_:alice <name> "Alice" .' ) ;
11- mu . setCommitNow ( true ) ;
12- const ag = await client . newTxn ( ) . mutate ( mu ) ;
13- const uid = ag . getUidsMap ( ) . get ( "alice" ) ;
89+ const nquads = '_:alice <name> "Alice" .' ;
90+ const uid = await performNquadMutation ( nquads , "alice" ) ;
1491
1592 const q = `{
16- find_bob (func: uid(${ uid } )) {
93+ all (func: uid(${ uid } )) {
1794 name
1895 }
1996 }` ;
20- let res = await client . newTxn ( ) . query ( q ) ;
21- // tslint:disable-next-line no-unsafe-any
22- expect ( res . getJson ( ) . find_bob [ 0 ] . name ) . toEqual ( "Alice" ) ;
97+ const expectedJson = {
98+ name : "Alice" ,
99+ } ;
100+ await checkIntegrity ( expectedJson , q ) ;
23101
24- mu = new dgraph . Mutation ( ) ;
25- mu . setDelNquads ( `<${ uid } > <name> * .` ) ;
26- mu . setCommitNow ( true ) ;
27- await client . newTxn ( ) . mutate ( mu ) ;
102+ const nquads2 = `<${ uid } > <name> * .` ;
103+ await performNquadDeletion ( nquads2 , uid . toString ( ) ) ;
28104
29- res = await client . newTxn ( ) . query ( q ) ;
105+ const res = await client . newTxn ( ) . query ( q ) ;
30106 // tslint:disable-next-line no-unsafe-any
31- expect ( res . getJson ( ) . find_bob ) . toHaveLength ( 0 ) ;
107+ expect ( res . getJson ( ) . all ) . toHaveLength ( 0 ) ;
32108 } ) ;
33109
34110 it ( "should delete edges" , async ( ) => {
35- const client = await setup ( ) ;
111+ client = await setup ( ) ;
36112 await setSchema ( client , "age: int .\nmarried: bool ." ) ;
37113
38- let mu = new dgraph . Mutation ( ) ;
39- mu . setSetJson ( {
114+ const jsonObj = {
40115 uid : "_:alice" ,
41116 name : "Alice" ,
42117 age : 26 ,
@@ -57,40 +132,48 @@ describe("delete", () => {
57132 age : 29 ,
58133 } ,
59134 ] ,
60- } ) ;
61- mu . setCommitNow ( true ) ;
62- const ag = await client . newTxn ( ) . mutate ( mu ) ;
63- const uid = ag . getUidsMap ( ) . get ( "alice" ) ;
135+ } ;
136+ const uid = await performJsonMutation ( jsonObj , "alice" ) ;
64137
65- const q = `{
66- me(func: uid(${ uid } )) {
67- uid
138+ const expectedJson = jsonObj ;
139+ // tslint:disable-next-line no-dynamic-delete no-string-literal
140+ delete expectedJson [ "uid" ] ;
141+ const query = `{
142+ all(func: uid(${ uid } )) {
68143 name
69144 age
70145 loc
71146 married
72- friends {
73- uid
147+ schools {
74148 name
75- age
76149 }
77- schools {
78- uid
150+ friends {
79151 name
152+ age
80153 }
81154 }
82155 }` ;
83- let res = await client . newTxn ( ) . query ( q ) ;
84- // tslint:disable-next-line no-unsafe-any
85- expect ( res . getJson ( ) . me [ 0 ] . friends . length ) . toBe ( 2 ) ;
156+ await checkIntegrity ( expectedJson , query ) ;
86157
87- mu = new dgraph . Mutation ( ) ;
158+ const mu = new dgraph . Mutation ( ) ;
88159 dgraph . deleteEdges ( mu , uid , "friends" ) ;
89160 mu . setCommitNow ( true ) ;
90161 await client . newTxn ( ) . mutate ( mu ) ;
91162
92- res = await client . newTxn ( ) . query ( q ) ;
163+ const res = await client . newTxn ( ) . query ( query ) ;
93164 // tslint:disable-next-line no-unsafe-any
94- expect ( res . getJson ( ) . me [ 0 ] . friends ) . toBeFalsy ( ) ;
165+ expect ( res . getJson ( ) . all [ 0 ] . friends ) . toBeFalsy ( ) ;
166+ } ) ;
167+
168+ it ( "should delete a node with upsert using graphql" , async ( ) => {
169+ client = await setup ( ) ;
170+ await setSchema ( client , `
171+ name: string @index(hash) .
172+
173+ type Person {
174+ name: string
175+ }
176+ ` ) ;
177+ await upsertDeletionWithVars ( ) ;
95178 } ) ;
96179} ) ;
0 commit comments