~edwargix/git.sr.ht

816b41bcc825563adb99c6f634c7d0f02236f7a8 — Drew DeVault 5 years ago be80de0
api: flesh out query complexity configuration
2 files changed, 48 insertions(+), 1 deletions(-)

A api/graph/complexity.go
M api/server.go
A api/graph/complexity.go => api/graph/complexity.go +45 -0
@@ 0,0 1,45 @@
package graph

import (
	"git.sr.ht/~sircmpwn/git.sr.ht/api/graph/api"
	"git.sr.ht/~sircmpwn/git.sr.ht/api/graph/model"
)

func cursorComplexity(c int, cursor *model.Cursor) int {
	if cursor != nil {
		return c * cursor.Count
	}
	return c
}

func ApplyComplexity(conf *api.Config) {
	conf.Complexity.Query.Repositories = func(c int, cursor *model.Cursor, filter *model.Filter) int {
		c = cursorComplexity(c, cursor)
		if filter != nil && filter.Count != nil {
			c *= *filter.Count
		}
		return c
	}
	conf.Complexity.Repository.AccessControlList = func(c int, cursor *model.Cursor) int {
		return cursorComplexity(c, cursor)
	}
	conf.Complexity.Repository.Log = func(c int, cursor *model.Cursor, from *string) int {
		return cursorComplexity(c, cursor)
	}
	conf.Complexity.Repository.Objects = func(c int, ids []string) int {
		return c * len(ids)
	}
	conf.Complexity.Repository.References = func(c int, cursor *model.Cursor) int {
		return cursorComplexity(c, cursor)
	}
	conf.Complexity.Tree.Entries = func(c int, cursor *model.Cursor) int {
		return cursorComplexity(c, cursor)
	}
	conf.Complexity.User.Repositories = func(c int, cursor *model.Cursor, filter *model.Filter) int {
		c = cursorComplexity(c, cursor)
		if filter != nil && filter.Count != nil {
			c *= *filter.Count
		}
		return c
	}
}

M api/server.go => api/server.go +3 -1
@@ 73,9 73,11 @@ func main() {
	gqlConfig := api.Config{
		Resolvers: &graph.Resolver{DB: db},
	}
	graph.ApplyComplexity(&gqlConfig)

	srv := handler.GraphQL(
		api.NewExecutableSchema(gqlConfig),
		handler.ComplexityLimit(50))
		handler.ComplexityLimit(100))

	router.Handle("/query", srv)