@@ -24,6 +24,13 @@ chai.use(chaiAsPromised);
2424const expect = chai . expect ;
2525
2626interface UserResponse {
27+ user : {
28+ name : string ;
29+ uid : string ;
30+ }
31+ }
32+
33+ interface UsersResponse {
2734 users : [
2835 user : {
2936 id : string ;
@@ -33,6 +40,24 @@ interface UserResponse {
3340 ] ;
3441}
3542
43+ interface UserUpdateResponse {
44+ user_upsert : { uid : string }
45+ }
46+
47+ interface EmailsResponse {
48+ emails : [
49+ email : {
50+ text : string ;
51+ subject : string ;
52+ id : string ;
53+ date : string ;
54+ from : {
55+ name : string ;
56+ }
57+ }
58+ ] ;
59+ }
60+
3661interface UserVariables {
3762 id : string ;
3863}
@@ -42,76 +67,75 @@ const connectorConfig: ConnectorConfig = {
4267 serviceId : 'my-service' ,
4368} ;
4469
70+ const userId = 'QVBJcy5ndXJ3' ;
71+
4572describe ( 'getDataConnect()' , ( ) => {
4673
47- const query1 = 'query ListUsers @auth(level: PUBLIC) { users { uid, name, address } }' ;
48- const query2 = 'query ListEmails @auth(level: NO_ACCESS) { emails { id subject text date from { name } } }' ;
49- const getUserQuery = 'query GetUser($id: User_Key!) { user(key: $id) { uid name } }' ;
74+ const queryListUsers = 'query ListUsers @auth(level: PUBLIC) { users { uid, name, address } }' ;
75+ const queryListEmails = 'query ListEmails @auth(level: NO_ACCESS) { emails { id subject text date from { name } } }' ;
76+ const queryGetUserById = 'query GetUser($id: User_Key!) { user(key: $id) { uid name } }' ;
5077 const multipleQueries = `
51- ${ query1 }
52- ${ query2 }
78+ ${ queryListUsers }
79+ ${ queryListEmails }
5380 ` ;
54- const mutation = 'mutation user { user_insert(data: {uid: "QVBJcy5ndXJ3", address: "Address", name: "Name"}) }'
55- const mutationUpdate = 'mutation UpdateUser($id: User_Key!) { user_update(key: $id, data: { name: "Name" }) }' ;
81+ const mutation = `mutation user { user_insert(data: {uid: "${ userId } ", address: "32 St", name: "Fred Car"}) }` ;
82+ const upsertUser = `mutation UpsertUser($id: String) {
83+ user_upsert(data: { uid: $id, address: "32 St.", name: "Fred" }) }` ;
5684
5785 describe ( 'executeGraphql()' , ( ) => {
86+ it ( 'executeGraphql() successfully executes a GraphQL mutation' , async ( ) => {
87+ const resp = await getDataConnect ( connectorConfig ) . executeGraphql < UserUpdateResponse , unknown > (
88+ upsertUser , { variables : { id : userId } }
89+ ) ;
90+ //{ data: { user_insert: { uid: 'QVBJcy5ndXJ3' } } }
91+ expect ( resp . data . user_upsert . uid ) . to . be . not . empty ;
92+ expect ( resp . data . user_upsert . uid ) . equals ( userId ) ;
93+ } ) ;
94+
5895 it ( 'executeGraphql() successfully executes a GraphQL' , async ( ) => {
59- const resp = await getDataConnect ( connectorConfig ) . executeGraphql < UserResponse , UserVariables > ( query1 ) ;
60- //console.dir(resp.data.users);
96+ const resp = await getDataConnect ( connectorConfig ) . executeGraphql < UsersResponse , UserVariables > ( queryListUsers ) ;
6197 expect ( resp . data . users ) . to . be . not . empty ;
6298 expect ( resp . data . users [ 0 ] . name ) . to . be . not . undefined ;
6399 expect ( resp . data . users [ 0 ] . address ) . to . be . not . undefined ;
64100 } ) ;
65101
66102 it ( 'executeGraphql() use the operationName when multiple queries are provided' , async ( ) => {
67- const resp = await getDataConnect ( connectorConfig ) . executeGraphql (
103+ const resp = await getDataConnect ( connectorConfig ) . executeGraphql < EmailsResponse , unknown > (
68104 multipleQueries ,
69105 { operationName : 'ListEmails' }
70106 ) ;
71- console . dir ( resp . data ) ;
72- // expect(resp.data.users).to.be.not.empty;
73- // expect(resp.data.users[0].name).to.be.not.undefined;
74- // expect(resp.data.users[0].address).to.be.not.undefined;
75- } ) ;
76-
77- it ( 'executeGraphql() successfully executes a GraphQL mutation' , async ( ) => {
78- const resp = await getDataConnect ( connectorConfig ) . executeGraphql (
79- mutationUpdate , { variables : { id : 'QVBJcy5ndXJ3' } }
80- ) ;
81- console . dir ( resp ) ; //{ data: { user_insert: { uid: 'QVBJcy5ndXJ3' } } }
82- //expect(resp.data.users).to.be.not.empty;
83- // expect(resp.data.users[0].name).to.be.not.undefined;
84- // expect(resp.data.users[0].address).to.be.not.undefined;
107+ expect ( resp . data . emails ) . to . be . not . empty ;
108+ expect ( resp . data . emails [ 0 ] . id ) . to . be . not . undefined ;
109+ expect ( resp . data . emails [ 0 ] . from . name ) . to . be . not . undefined ;
85110 } ) ;
86111
87- it ( 'executeGraphql() should throw for a duplicate key GraphQL mutation ' , async ( ) => {
112+ it ( 'executeGraphql() should throw for a query error ' , async ( ) => {
88113 return getDataConnect ( connectorConfig ) . executeGraphql ( mutation )
89114 . should . eventually . be . rejected . and . have . property ( 'code' , 'data-connect/query-error' ) ;
90115 } ) ;
91116
92117 it ( 'executeGraphql() successfully executes a GraphQL query with variables' , async ( ) => {
93118 const resp = await getDataConnect ( connectorConfig ) . executeGraphql < UserResponse , UserVariables > (
94- getUserQuery ,
95- { variables : { id : 'QVBJcy5ndXJ3' } }
119+ queryGetUserById ,
120+ { variables : { id : userId } }
96121 ) ;
97- console . dir ( resp ) ;
98- // expect(resp.data.users).to.be.not.empty;
99- // expect(resp.data.users[0].name).to.be.not.undefined;
100- // expect(resp.data.users[0].address).to.be.not.undefined;
122+ expect ( resp . data . user . name ) . to . be . not . undefined ;
123+ expect ( resp . data . user . uid ) . equals ( userId ) ;
101124 } ) ;
102125 } ) ;
103126
104127 describe ( 'executeGraphqlRead()' , ( ) => {
105128 it ( 'executeGraphqlRead() successfully executes a read-only GraphQL' , async ( ) => {
106- const resp = await getDataConnect ( connectorConfig ) . executeGraphqlRead < UserResponse , UserVariables > ( query1 ) ;
129+ const resp =
130+ await getDataConnect ( connectorConfig ) . executeGraphqlRead < UsersResponse , UserVariables > ( queryListUsers ) ;
107131 expect ( resp . data . users ) . to . be . not . empty ;
108132 expect ( resp . data . users [ 0 ] . name ) . to . be . not . undefined ;
109133 expect ( resp . data . users [ 0 ] . address ) . to . be . not . undefined ;
110134 } ) ;
111135
112136 it ( 'executeGraphqlRead() should throw for a GraphQL mutation' , async ( ) => {
113137 return getDataConnect ( connectorConfig ) . executeGraphqlRead ( mutation )
114- . should . eventually . be . rejected . and . have . property ( 'code' , 'data-connect/permission-denied' ) ;
138+ . should . eventually . be . rejected ;
115139 } ) ;
116140 } ) ;
117141} ) ;
0 commit comments