graph.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2019-present Facebook Inc. All rights reserved.
  2. // This source code is licensed under the Apache 2.0 license found
  3. // in the LICENSE file in the root directory of this source tree.
  4. package main
  5. import (
  6. "context"
  7. "fmt"
  8. "log"
  9. "time"
  10. "code.osinet.fr/fgm/go__ent_demo/ent"
  11. "code.osinet.fr/fgm/go__ent_demo/ent/group"
  12. "code.osinet.fr/fgm/go__ent_demo/ent/user"
  13. )
  14. func CreateGraph(ctx context.Context, client *ent.Client) error {
  15. // First create the users
  16. a8m, err := client.User.
  17. Create().
  18. SetAge(30).
  19. SetName("Ariel").
  20. Save(ctx)
  21. if err != nil {
  22. return err
  23. }
  24. neta, err := client.User.
  25. Create().
  26. SetAge(28).
  27. SetName("Neta").
  28. SetSpouse(a8m).
  29. AddFollowing(a8m).
  30. AddFriends(a8m).
  31. Save(ctx)
  32. if err != nil {
  33. return err
  34. }
  35. // The, create the cars, and attach them to the users in the creation.
  36. if err = client.Car.Create().SetModel("Tesla").
  37. SetRegisteredAt(time.Now()). // Ignore the time in the graph
  38. SetOwner(a8m). // Attach this graph to Ariel
  39. Exec(ctx); err != nil {
  40. return err
  41. }
  42. if err = client.Car.Create().SetModel("Mazda").
  43. SetRegisteredAt(time.Now()). // Ignore the time in the graph
  44. SetOwner(a8m).
  45. Exec(ctx); err != nil {
  46. return err
  47. }
  48. if err = client.Car.Create().SetModel("Ford").
  49. SetRegisteredAt(time.Now()). // Ignore the time in the graph
  50. SetOwner(neta).
  51. Exec(ctx); err != nil {
  52. return err
  53. }
  54. // Create the groups, and add their users in the creation
  55. if err = client.Group.Create().SetName("GitLab").AddUsers(neta, a8m).Exec(ctx); err != nil {
  56. return err
  57. }
  58. if err = client.Group.Create().SetName("GitHub").AddUsers(a8m).Exec(ctx); err != nil {
  59. return err
  60. }
  61. log.Println("The graph was created successfully")
  62. return nil
  63. }
  64. // QueryGithub gets all user's cars within the group named "GitHub":
  65. func QueryGithub(ctx context.Context, client *ent.Client) error {
  66. cars, err := client.Group.
  67. Query().
  68. Where(group.Name("GitHub")). // (Group(Name=GitHub),)
  69. QueryUsers(). // (User(Name=Ariel, Age=30),)
  70. QueryCars(). // (Car(Model=Tesla, RegisteredAt=<Time>), Car(Model=Mazda, RegisteredAt=<Time>),
  71. All(ctx)
  72. if err != nil {
  73. return fmt.Errorf("failed getting cars for users in group GitHub: %w", err)
  74. }
  75. log.Println("cars in group GitHub: ", cars)
  76. return nil
  77. }
  78. func QueryArielCars(ctx context.Context, client *ent.Client) error {
  79. cars, err := client.User.
  80. Query().
  81. Where(
  82. user.HasCars(),
  83. user.Name("Ariel"),
  84. user.HasSpouse(),
  85. ).Only(ctx)
  86. if err != nil {
  87. return fmt.Errorf("failed getting cars for Ariel: %w", err)
  88. }
  89. log.Println("Ariel's cars: ", cars)
  90. a8m := client.User.Query().Where(user.HasSpouseWith(user.Name("Neta"))).OnlyX(ctx)
  91. log.Println("Query user who is the spouse of user named Neta: ", a8m)
  92. a8m = client.User.Query().Where(user.Name("Neta")).QuerySpouse().OnlyX(ctx)
  93. neta := client.User.Query().Where(user.Name("Neta")).OnlyX(ctx)
  94. log.Println("Query spouse of user named Neta: ", a8m)
  95. log.Printf("Followers for Ariel: %v, for Neta: %v",
  96. a8m.QueryFollowers().AllX(ctx),
  97. neta.QueryFollowers().AllX(ctx),
  98. )
  99. log.Printf("Following for Ariel: %v, for Neta: %v",
  100. a8m.QueryFollowing().AllX(ctx),
  101. neta.QueryFollowing().AllX(ctx),
  102. )
  103. log.Printf("Neta's friends: %v", neta.QueryFriends().AllX(ctx))
  104. return nil
  105. }
  106. func QueryGroupWithUsers(ctx context.Context, client *ent.Client) error {
  107. groups, err := client.Group.
  108. Query().
  109. Where(group.HasUsers()).
  110. All(ctx)
  111. if err != nil {
  112. return fmt.Errorf("failed getting groups with users: %w", err)
  113. }
  114. log.Println("Groups with users: ", groups)
  115. return nil
  116. }