Action Controller Overview

How to follow the flow of a request through a controller.

Chapters

  1. 1. Actions
  2. 2. Filters

1. Actions

When the controller receives the request, the routing determines which controller and action to run, then creates a new context and executes the necessary method.

ProductController := actioncontroller.New(func(c *actioncontroller.C) {

    // Index action is rendered as a `products { }` query in the resulting
    // GraphQL scheama. Additionally, the input parameters could be overridden
    // with `Permit` call.
    c.Index(func(ctx *actioncontroller.Context) actioncontroller.Result {
        products := Product.WithContext(ctx).All()
        return actionview.NestedCollectionView(products)
    })
})

As an example, if a user calls query query { products { id, name } } in your application to list all available products, Active Graph will execute an action Index of the ProductController.

2. Filters

func RequireLogin(ctx *actioncontroller.Context) actioncontroller.Result {
    if !loggedIn(ctx) {
        return actionview.Error(errors.New("not logged in"))
    }
    return nil
}


AdminController := actioncontroller.New(func(c *actioncontroller.C) {
    c.BeforeAction(RequireLogin)
})