package todo import ( "context" "fmt" "log" "entgo.io/ent/dialect" _ "github.com/mattn/go-sqlite3" "todo/ent" "todo/ent/todo" ) func setup() (context.Context, *ent.Client, func()) { client, err := ent.Open(dialect.SQLite, "file:ent?mode=memory&cache=shared&_fk=1") if err != nil { log.Fatalf("failed opening connection to sqlite: %v", err) } closer := func() { _ = client.Close() } ctx := context.Background() // Run the automatic migration tool to create all schema resources if err := client.Schema.Create(ctx); err != nil { log.Fatalf("failed creating schema resources: %v", err) } return ctx, client, closer } func addTwo(ctx context.Context, client *ent.Client) (*ent.Todo, *ent.Todo) { task1, err := client.Todo.Create().SetText("Add GraphQL Example").Save(ctx) if err != nil { log.Fatalf("failed creating a todo: %v", err) } task2, err := client.Todo.Create().SetText("Add Tracing Example").Save(ctx) if err != nil { log.Fatalf("failed creating a todo: %v", err) } return task1, task2 } func addTwoConnected(ctx context.Context, client *ent.Client) (*ent.Todo, *ent.Todo) { task1, task2 := addTwo(ctx, client) if err := task2.Update().SetParent(task1).Exec(ctx); err != nil { log.Fatalf("failed connecting todo2 to its parent: %v", err) } return task1, task2 } func Example_todo1() { _, _, closer := setup() defer closer() // Output: } func Example_todo2() { ctx, client, closer := setup() defer closer() task1, err := client.Todo.Create().SetText(" ").Save(ctx) if err != nil { log.Fatalf("failed creating a todo: %v", err) } fmt.Printf("Todo(id=%d)\n", task1.ID) // Output: // Todo(id=1) } func Example_todo3() { ctx, client, closer := setup() defer closer() task1, task2 := addTwo(ctx, client) fmt.Printf("%d: %q\n", task1.ID, task1.Text) fmt.Printf("%d: %q\n", task2.ID, task2.Text) // Output: // 1: "Add GraphQL Example" // 2: "Add Tracing Example" } func Example_todo4() { ctx, client, closer := setup() defer closer() _, _ = addTwoConnected(ctx, client) // Output: } func Example_todo5() { ctx, client, closer := setup() defer closer() _, _ = addTwoConnected(ctx, client) // Query all todo items. items, err := client.Todo.Query().All(ctx) if err != nil { log.Fatalf("failed querying all todos: %v", err) } for _, item := range items { fmt.Printf("%d: %q\n", item.ID, item.Text) } // Output: // 1: "Add GraphQL Example" // 2: "Add Tracing Example" } func Example_todo6() { ctx, client, closer := setup() defer closer() _, _ = addTwoConnected(ctx, client) // Query all todo items that depend on other items items, err := client.Todo.Query().Where(todo.HasParent()).All(ctx) if err != nil { log.Fatalf("failed querying all todos: %v", err) } for _, item := range items { fmt.Printf("%d: %q\n", item.ID, item.Text) } // Output: // 2: "Add Tracing Example" } func Example_todo7() { ctx, client, closer := setup() defer closer() _, _ = addTwoConnected(ctx, client) // Query all todo items that don't depend on other items and have items that depend on them items, err := client.Todo.Query().Where( todo.Not(todo.HasParent()), todo.HasChildren(), ).All(ctx) if err != nil { log.Fatalf("failed querying todos: %v", err) } for _, item := range items { fmt.Printf("%d: %q\n", item.ID, item.Text) } // Output: // 1: "Add GraphQL Example" } func Example_todo8() { ctx, client, closer := setup() defer closer() _, _ = addTwoConnected(ctx, client) // Query parent through its children parent, err := client.Todo.Query(). Where(todo.HasParent()). // Filter children QueryParent(). // Climb to their parent Only(ctx) // Expect exactly one result if err != nil { log.Fatalf("failed querying todos: %v", err) } fmt.Printf("%d: %q\n", parent.ID, parent.Text) // Output: // 1: "Add GraphQL Example" }