Skip to content

Commit a2f7600

Browse files
committed
Update data-fetching.mdx
add GraphQL examples
1 parent 283b352 commit a2f7600

File tree

1 file changed

+59
-43
lines changed

1 file changed

+59
-43
lines changed

modus/data-fetching.mdx

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ import { JSON } from "json-as"
161161
// the name of the Dgraph connection, as specified in the modus.json manifest
162162
const hostName: string = "my-dgraph"
163163

164-
// Declare classes used to parse the JSON document returned by Dgraph query.
164+
// Declare classes used to parse the JSON document returned by Dgraph query.
165165
@json
166166
class GetPersonResponse {
167167
persons: Person[] = []
@@ -314,38 +314,45 @@ import (
314314
// the name of the GraphQL connection, as specified in the modus.json manifest
315315
const hostName = "my-graphql-api"
316316

317+
// Declare structures used to parse the JSON document returned by Dgraph query.
317318
type Person struct {
318-
FirstName string `json:"firstName,omitempty"`
319-
LastName string `json:"lastName,omitempty"`
319+
Name string `json:"name,omitempty"`
320+
Age int32 `json:"age,omitempty"`
321+
Home *Location `json:"home,omitempty"`
320322
}
321323

322-
func QuerySpecificPerson(firstName, lastName string) (*Person, error) {
323-
statement := `query queryPeople($firstName: String!, $lastName: String!) {
324-
people: queryPerson(
325-
first: 1,
326-
filter: { firstName: { eq: $firstName }, lastName: { eq: $lastName } }
327-
) {
328-
id
329-
firstName
330-
lastName
331-
}
332-
}`
324+
// Dgraph location predicates follow GeoJSON format
325+
type Location struct {
326+
Latitude float32 `json:"latitude,omitempty"`
327+
Longitude float32 `json:"longitude,omitempty"`
328+
}
329+
330+
type GetPersonResponse struct {
331+
Person *Person `json:"getPerson"`
332+
}
333+
334+
func GetPerson(name string) (*Person, error) {
335+
statement := `query getPerson($name: String!) {
336+
getPerson(name: $name) {
337+
age
338+
name
339+
home {
340+
latitude
341+
longitude
342+
}
343+
}
344+
}`
333345

334346
vars := map[string]any{
335-
"firstName": firstName,
336-
"lastName": lastName,
347+
"name": name,
337348
}
338349

339-
response, err := graphql.Execute[PeopleData](hostName, statement, vars)
350+
response, err := graphql.Execute[GetPersonResponse](hostName, statement, vars)
340351
if err != nil {
341352
return nil, err
342353
}
343354

344-
if len(response.Data.People) == 0 {
345-
return nil, nil // Person not found
346-
}
347-
348-
return response.Data.People[0], nil
355+
return response.Data.Person, nil
349356
}
350357
```
351358

@@ -355,37 +362,46 @@ import { graphql } from "@hypermode/modus-sdk-as"
355362
// the name of the GraphQL connection, as specified in the modus.json manifest
356363
const hostName: string = "my-graphql-api"
357364

365+
// Declare classes used to parse the JSON document returned by Dgraph query.
366+
@json
367+
class GetPersonResponse {
368+
getPerson: Person
369+
}
370+
358371
@json
359372
class Person {
360-
id: string | null = null
361-
firstName: string = ""
362-
lastName: string = ""
373+
name: string = ""
374+
age: i32 = 0
375+
home: Location
376+
}
377+
// Dgraph location predicates follow GeoJSON format
378+
@json
379+
class Location {
380+
latitude: f64
381+
longitude: f64
363382
}
364383

365-
export function querySpecificPerson(firstName: string, lastName: string): Person | null {
366-
const statement = `query queryPeople($firstName: String!, $lastName: String!) {
367-
people: queryPerson(
368-
first: 1,
369-
filter: { firstName: { eq: $firstName }, lastName: { eq: $lastName } }
370-
) {
371-
id
372-
firstName
373-
lastName
374-
}
375-
}`
384+
export function getPerson(name: string): Person | null {
385+
const statement = `
386+
query getPerson($name: String!) {
387+
getPerson(name: $name) {
388+
age
389+
name
390+
home {
391+
latitude
392+
longitude
393+
}
394+
}
395+
}`
376396

377397
const vars = new graphql.Variables()
378-
vars.set("firstName", firstName)
379-
vars.set("lastName", lastName)
398+
vars.set("name", name)
380399

381-
const response = graphql.execute<PeopleData>(hostName, statement, vars)
400+
const response = graphql.execute<GetPersonResponse>(hostName, statement, vars)
382401

383402
if (!response.data) return null
384403

385-
const people = response.data!.people
386-
387-
if (people.length === 0) return null
388-
return people[0]
404+
return response.data!.getPerson
389405
}
390406
```
391407

0 commit comments

Comments
 (0)