Last active
August 20, 2019 21:24
-
-
Save alex-knowles/5ab048caa0857435103ea7ff80a2ea92 to your computer and use it in GitHub Desktop.
Code snippets for Dataloader Blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query Leslie { | |
employee(id: "1000") { | |
name | |
office { | |
name | |
url | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": { | |
"employee": { | |
"name": "Leslie Knope", | |
"office": { | |
"name": "Parks and Recreation", | |
"url": "https://pawnee.gov/parks_and_rec" | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query LesliesDirectReports { | |
employee(id:"1000") { | |
name | |
directReports { | |
name | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": { | |
"employee": { | |
"name": "Leslie Knope", | |
"directReports": [ | |
{ | |
"name": "Andy Dwyer" | |
}, | |
{ | |
"name": "April Ludgate" | |
}, | |
{ | |
"name": "Tom Haverford" | |
} | |
] | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query LesliesDirectReportsWithOffices { | |
employee(id:"1000") { | |
name | |
office { | |
name | |
url | |
} | |
directReports { | |
name | |
office { | |
name | |
url | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": { | |
"employee": { | |
"name": "Leslie Knope", | |
"office": { | |
"name": "Parks and Recreation", | |
"url": "https://pawnee.gov/parks_and_rec" | |
}, | |
"directReports": [ | |
{ | |
"name": "Andy Dwyer", | |
"office": { | |
"name": "Parks and Recreation", | |
"url": "https://pawnee.gov/parks_and_rec" | |
} | |
}, | |
{ | |
"name": "April Ludgate", | |
"office": { | |
"name": "Parks and Recreation", | |
"url": "https://pawnee.gov/parks_and_rec" | |
} | |
}, | |
{ | |
"name": "Tom Haverford", | |
"office": { | |
"name": "Parks and Recreation", | |
"url": "https://pawnee.gov/parks_and_rec" | |
} | |
} | |
] | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@doc """ | |
Resolver for the Employee 'office' field. | |
""" | |
@spec office_for_employee(map(), map(), Absinthe.Resolution.t()) :: tuple() | |
def office_for_employee(employee, args, resolution) | |
def office_for_employee(%{office_id: office_id}, _, _) do | |
office = Core.office_by_id(office_id) | |
{:ok, office} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@doc """ | |
Resolver for the Employee 'office' field. | |
""" | |
@spec office_for_employee(map(), map(), Absinthe.Resolution.t()) :: tuple() | |
def office_for_employee(employee, args, resolution) | |
def office_for_employee(%{office_id: office_id}, _, %{context: %{loader: loader}}) do | |
loader | |
|> Dataloader.load(Core, :office_by_id, office_id) | |
|> on_load(fn loader -> | |
office = Dataloader.get(loader, Core, :office_by_id, office_id) | |
{:ok, office} | |
end) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment