Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to query models? #40

Open
WolfDan opened this issue Oct 6, 2017 · 5 comments
Open

How to query models? #40

WolfDan opened this issue Oct 6, 2017 · 5 comments

Comments

@WolfDan
Copy link

WolfDan commented Oct 6, 2017

For example in the repository example are a vertex called "Land", it contains the model "Person".

How do I query the person data in adition to the Land fields?

@elbow-jason
Copy link
Owner

Here is the output of Land.land_example/0 from the modules in the examples folder:

iex> Land.land_example
%DgraphEx.Examples.Land{_uid_: nil, address: "123 Maple Rd Phoenix, AZ 88888",
 city: "Phoenix", construction_year: "1982", floors: 1, geo_border: nil,
 geo_center: nil, land_size: 12, living_space: 8,
 owner: %DgraphEx.Examples.Person{_uid_: nil,
  address: "456 Maple Rd Phoenix, AZ 55555", name: "Jason Beefcake"},
 zipcode: "88888", zoning: "FAKE"}

I mutate my schema to ensure Land is in there:

iex> {:ok, _} = Repo.request mutation(schema: Land)
{:ok, %{"code" => "Success", "message" => "Done", "uids" => %{}}}

Then I do a mutation set to the land_example output:

iex> mutation(set: Land.land_example) |> DgraphEx.Repo.request
{:ok,
 %{"code" => "Success", "message" => "Done",
   "uids" => %{"land" => "0x12", "owner" => "0x13"}}}

Then I query for :land with :zipcode "88888":

iex> query(get: :land, func: eq(:zipcode, "88888"))
|> select(%Land{owner: %Person{}})
|> DgraphEx.Repo.request
|> DgraphEx.into(%Land{owner: %Person{}}, :land)

Which outputs:

%{land: [
   %DgraphEx.Examples.Land{_uid_: "0x12",
    address: "123 Maple Rd Phoenix, AZ 88888", city: "Phoenix",
    construction_year: "1982", floors: 1, geo_border: nil, geo_center: nil,
    land_size: 12, living_space: 8,
    owner: [%DgraphEx.Examples.Person{_uid_: "0x13",
      address: "456 Maple Rd Phoenix, AZ 55555", name: "Jason Beefcake"}],
    zipcode: "88888", zoning: "FAKE"}
  ]}

@elbow-jason
Copy link
Owner

@WolfDan I hope this explanation helps.

@elbow-jason
Copy link
Owner

The keys to loading nested models are the select(%Land{owner: %Person{}}) to ask dgraph for nested object data and the DgraphEx.into(%Land{owner: %Person{}}, :land) for populating the nested query into the models.

@elbow-jason
Copy link
Owner

elbow-jason commented Oct 7, 2017

I'm wondering if I should extend Repo.get to incorporate this behavior? What do you think?

@WolfDan
Copy link
Author

WolfDan commented Oct 7, 2017

Totally helps @elbow-jason !

Hum I think there is a problem with Repo.get and the select example that is the deply nested models, suppose this

Land contains a owner that is a user -> User contains all the lands that owns -> This request other lands and so fort...

I think is mostly a user design problem it self but I think cases like those exist, maybe another argument to except always certain fields when request all the vertex?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants