Graphql: Pagination

Created on 9 Sep 2019  路  3Comments  路  Source: graphql-go/graphql

Is there any plan to add pagination support? https://graphql.org/learn/pagination/

Most helpful comment

I created a library that helps in creating pagination in graphql-go: https://github.com/carlosstrand/graphql-pagination-go

All 3 comments

Connections are part of the relay specification. https://github.com/graphql-go/relay may be what you are looking for

You can do pagination yourself by build a pageType from origin Type,
then use the pageType in your field, do the pagination logic in your database query,
I think it鈥榮 better than the relay solution锛宐ecause we cache no data and do not rely on react
When you do query, add index and size args such as:

{
    userPage(index: 1, size: 100){
         list{
             # fields of userType
         }
         index
         size
         total
    }
}
func addPageParamsToArgs(args graphql.FieldConfigArgument) graphql.FieldConfigArgument {
    args["index"] = &graphql.ArgumentConfig{
        Type:         graphql.Int,
        Description:  "page index",
        DefaultValue: 0,
    }
    args["size"] = &graphql.ArgumentConfig{
        Type:         graphql.Int,
        Description:  "page size, 0 means do not page",
        DefaultValue: 0,
    }
    return args
}


func buildPageType(t graphql.Type) *graphql.Object {
    fields := graphql.Fields{}

    fields["list"] = &graphql.Field{
        Name: t.Name() + "List",
        Type: graphql.NewList(t),
    }

    fields["index"] = &graphql.Field{
        Name:        "number",
        Type:        graphql.Int,
        Description: "page index",
    }

    fields["size"] = &graphql.Field{
        Name:        "size",
        Type:        graphql.Int,
        Description: "page size",
    }

    fields["total"] = &graphql.Field{
        Name:        "total",
        Type:        graphql.Int,
        Description: "total count",
    }

    pageType := graphql.NewObject(
        graphql.ObjectConfig{
            Name:        t.Name() + "Page",
            Description: fmt.Sprintf("%s page data", t.Name()),
            Fields:      fields,
        },
    )

    return pageType
}

I created a library that helps in creating pagination in graphql-go: https://github.com/carlosstrand/graphql-pagination-go

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jiharal picture jiharal  路  5Comments

SDkie picture SDkie  路  6Comments

yhc44 picture yhc44  路  5Comments

sogko picture sogko  路  6Comments

januszm picture januszm  路  6Comments