@@ -77,64 +77,47 @@ Here is an example of fetching a person from a Dgraph database using the Modus S
7777package  main
7878
7979import  (
80-    " encoding/json" 
81-    " github.com/hypermodeinc/modus/sdk/go/pkg/dgraph" 
80+ 	 " encoding/json" 
81+ 	 " github.com/hypermodeinc/modus/sdk/go/pkg/dgraph" 
8282)
8383
8484//  the name of the Dgraph connection, as specified in the modus.json manifest
85- const  hostName = " my-database " 
85+ const  hostName = " my-dgraph " 
8686
8787//  declare structures used to parse the JSON document returned by Dgraph query.
8888type  Person  struct  {
89-   Name  string     ` json:"name,omitempty"` 
90-   Age   int32      ` json:"age,omitempty"` 
91-   Home  *Location ` json:"home,omitempty"` 
92- }
93- 
94- //  Dgraph location predicates follow GeoJSON format
95- type  Location  struct  {
96-   Type         string     ` json:"type,omitempty"` 
97-   Coordinates  []float32  ` json:"coordinates,omitempty"` 
89+ 	Name  string  ` json:"name,omitempty"` 
90+ 	Age   int32   ` json:"age,omitempty"` 
9891}
9992
93+ //  Dgraph returns an array of Persons
10094type  GetPersonResponse  struct  {
101-    Persons  []*Person ` json:"persons"` 
95+ 	 Persons  []*Person ` json:"persons"` 
10296}
10397
10498func  GetPerson (name  string ) (*Person , error ) {
105-   statement  :=  ` 
106-    query getPerson($name: string) { 
99+ 	statement  :=  ` query getPerson($name: string!) {
107100    persons(func: eq(name, $name))  { 
108101      name 
109102      age 
110-       home 
111103    } 
112104  } 
113105  `  
114-    variables  :=  map [string ]string {
115-      " $name" 
116-    }
106+ 	 variables  :=  map [string ]string {
107+ 		 " $name" 
108+ 	 }
117109
118-   response , err  :=  dgraph.Execute (hostName, &dgraph.Request {
119-     Query: &dgraph.Query {
120-       Query:     statement,
121-       Variables: variables,
122-     },
123-   })
124-   if  err != nil  {
125-     return  nil , err
126-   }
110+ 	response , _  :=  dgraph.Execute (hostName, &dgraph.Request {
111+ 		Query: &dgraph.Query {
112+ 			Query:     statement,
113+ 			Variables: variables,
114+ 		},
115+ 	})
127116
128-   var  data  GetPersonResponse
129-   if  err  :=  json.Unmarshal ([]byte (response.Json ), &data); err != nil  {
130-     return  nil , err
131-   }
117+ 	var  data  GetPersonResponse
118+ 	json.Unmarshal ([]byte (response.Json ), &data)
132119
133-   if  len (data.Persons ) == 0  {
134-     return  nil , nil  //  person not found
135-   }
136- 
137-   return  data.Persons [0 ], nil 
120+ 	return  data.Persons [0 ], nil 
138121}
139122
140123``` 
@@ -147,43 +130,31 @@ import { JSON } from "json-as"
147130const :  string  =  " my-dgraph" 
148131
149132//  declare classes used to parse the JSON document returned by Dgraph query.
150- @json 
151- class  GetPersonResponse  {
152-   persons:  Person [] =  []
153- }
154- 
155133@json 
156134class  Person  {
157135  name:  string  =  " " 
158136  age:  i32  =  0 
159-   home:  Location 
160137}
161- //  Dgraph location predicates follow GeoJSON format 
138+ //  Dgraph returns an array of objects 
162139@json 
163- class  Location  {
164-   type:  string  =  " Point" 
165-   coordinates:  f64 [] =  []
140+ class  GetPersonResponse  {
141+   persons:  Person [] =  []
166142}
167143
168- export  function  getPerson(name :  string ):  Person  |   null   {
144+ export  function  getPerson(name :  string ):  Person  {
169145  const =  `    
170146  query getPerson($name: string) { 
171147    persons(func: eq(name, $name))  { 
172148        name 
173149        age 
174-         home 
175150    } 
176151  } ` 
177152
178153  const =  new  dgraph .Variables ()
179154  vars .set (" $name" name )
180155
181156  const =  dgraph .execute (hostName , new  dgraph .Request (new  dgraph .Query (statement , vars )))
182- 
183157  const =  JSON .parse <GetPersonResponse >(resp .Json ).persons 
184-   if  (persons .length  ==  0 ) {
185-     return  null 
186-   }
187158  return  persons [0 ]
188159}
189160``` 
@@ -211,27 +182,19 @@ import (
211182)
212183
213184
214- //  Declare structures used to parse the JSON document returned by Dgraph query. 
185+ //  Declare structures used to parse the JSON document returned by the REST API 
215186type  Person  struct  {
216187  Name  string     ` json:"name,omitempty"` 
217188  Age   int32      ` json:"age,omitempty"` 
218-   Home  *Location ` json:"home,omitempty"` 
219189}
220190
221- //  Dgraph location predicates follow GeoJSON format
222- type  Location  struct  {
223-   Type         string     ` json:"type,omitempty"` 
224-   Coordinates  []float32  ` json:"coordinates,omitempty"` 
225- }
226191
227192//  Exposing getPerson in the generated GraphQL API
228193
229194func  GetPerson (name  string ) (*Person , error ) {
230195  url  :=  fmt.Sprintf (" https://example.com/api/person?name=%s " 
231-   response , err  :=  http.Fetch (url)
232-   if  err != nil  {
233-     return  nil , err
234-   }
196+   response , _  :=  http.Fetch (url)
197+ 
235198  //  The API returns Person object as JSON
236199  var  person  Person
237200  response.JSON (&person)
@@ -248,13 +211,6 @@ import { http } from "@hypermode/modus-sdk-as"
248211class  Person  {
249212  name:  string  =  " " 
250213  age:  i32  =  0 
251-   home:  Location 
252- }
253- //  Assuming location follows GeoJSON format
254- @json 
255- class  Location  {
256-   type:  string  =  " Point" 
257-   coordinates:  f64 [] =  []
258214}
259215
260216//  Exposing getPerson in the generated GraphQL API
@@ -263,9 +219,6 @@ export function getPerson(name: string): Person {
263219  const =  ` https://example.com/api/people?name=${name } ` 
264220
265221  const =  http .fetch (url )
266-   if  (! response .ok ) {
267-     throw  new  Error (` Failed to fetch quote. Received: ${response .status } ${response .statusText } ` )
268-   }
269222
270223  //  The API returns Person object as JSON
271224  return  response .json <Person >()
@@ -292,17 +245,10 @@ import (
292245//  the name of the GraphQL connection, as specified in the modus.json manifest
293246const  hostName = " my-graphql-api" 
294247
295- //  Declare structures used to parse the JSON document returned by Dgraph query. 
248+ //  Declare structures used to parse the JSON document returned
296249type  Person  struct  {
297250  Name  string     ` json:"name,omitempty"` 
298251  Age   int32      ` json:"age,omitempty"` 
299-   Home  *Location ` json:"home,omitempty"` 
300- }
301- 
302- //  Dgraph location predicates follow GeoJSON format
303- type  Location  struct  {
304-   Latitude     float32    ` json:"latitude,omitempty"` 
305-   Longitude    float32    ` json:"longitude,omitempty"` 
306252}
307253
308254type  GetPersonResponse  struct  {
@@ -314,21 +260,14 @@ func GetPerson(name string) (*Person, error) {
314260      getPerson(name: $name) { 
315261        age 
316262        name 
317-         home { 
318-           latitude 
319-           longitude 
320-         } 
321263      } 
322264    }`  
323265
324266  vars  :=  map [string ]any{
325267     " name" 
326268  }
327269
328-   response , err  :=  graphql.Execute [GetPersonResponse](hostName, statement, vars)
329-   if  err != nil  {
330-     return  nil , err
331-   }
270+   response , _  :=  graphql.Execute [GetPersonResponse](hostName, statement, vars)
332271
333272  return  response.Data .Person , nil 
334273}
@@ -340,7 +279,7 @@ import { graphql } from "@hypermode/modus-sdk-as"
340279//  the name of the GraphQL connection, as specified in the modus.json manifest
341280const :  string  =  " my-graphql-api" 
342281
343- //  Declare  classes used to parse the JSON document returned by Dgraph query. 
282+ //  Declare  classes used to parse the JSON document returned
344283@json 
345284class  GetPersonResponse  {
346285  getPerson:  Person 
@@ -350,25 +289,14 @@ class GetPersonResponse {
350289class  Person  {
351290  name:  string  =  " " 
352291  age:  i32  =  0 
353-   home:  Location 
354- }
355- //  Dgraph location predicates follow GeoJSON format
356- @json 
357- class  Location  {
358-   latitude:  f64 
359-   longitude:  f64 
360292}
361293
362- export  function  getPerson(name :  string ):  Person  |   null   {
294+ export  function  getPerson(name :  string ):  Person  {
363295  const =  ` 
364296    query getPerson($name: String!) { 
365297      getPerson(name: $name) { 
366298        age 
367299        name 
368-         home { 
369-           latitude 
370-           longitude 
371-         } 
372300      } 
373301    } ` 
374302
@@ -377,8 +305,6 @@ export function getPerson(name: string): Person | null {
377305
378306  const =  graphql .execute <GetPersonResponse >(hostName , statement , vars )
379307
380-   if  (! response .data ) return  null 
381- 
382308  return  response .data ! .getPerson 
383309}
384310``` 
0 commit comments