// Copyright 2019-present Facebook Inc. All rights reserved. // This source code is licensed under the Apache 2.0 license found // in the LICENSE file in the root directory of this source tree. package main import ( "context" "fmt" "log" "time" "code.osinet.fr/fgm/go__ent_demo/ent" "code.osinet.fr/fgm/go__ent_demo/ent/group" "code.osinet.fr/fgm/go__ent_demo/ent/user" ) func CreateGraph(ctx context.Context, client *ent.Client) error { // First create the users a8m, err := client.User. Create(). SetAge(30). SetName("Ariel"). Save(ctx) if err != nil { return err } neta, err := client.User. Create(). SetAge(28). SetName("Neta"). SetSpouse(a8m). AddFollowing(a8m). AddFriends(a8m). Save(ctx) if err != nil { return err } // The, create the cars, and attach them to the users in the creation. if err = client.Car.Create().SetModel("Tesla"). SetRegisteredAt(time.Now()). // Ignore the time in the graph SetOwner(a8m). // Attach this graph to Ariel Exec(ctx); err != nil { return err } if err = client.Car.Create().SetModel("Mazda"). SetRegisteredAt(time.Now()). // Ignore the time in the graph SetOwner(a8m). Exec(ctx); err != nil { return err } if err = client.Car.Create().SetModel("Ford"). SetRegisteredAt(time.Now()). // Ignore the time in the graph SetOwner(neta). Exec(ctx); err != nil { return err } // Create the groups, and add their users in the creation if err = client.Group.Create().SetName("GitLab").AddUsers(neta, a8m).Exec(ctx); err != nil { return err } if err = client.Group.Create().SetName("GitHub").AddUsers(a8m).Exec(ctx); err != nil { return err } log.Println("The graph was created successfully") return nil } // QueryGithub gets all user's cars within the group named "GitHub": func QueryGithub(ctx context.Context, client *ent.Client) error { cars, err := client.Group. Query(). Where(group.Name("GitHub")). // (Group(Name=GitHub),) QueryUsers(). // (User(Name=Ariel, Age=30),) QueryCars(). // (Car(Model=Tesla, RegisteredAt=