2021-03-24 04:43:43 +00:00
|
|
|
package querycomplexity
|
|
|
|
|
|
|
|
import (
|
2021-03-24 05:15:48 +00:00
|
|
|
"github.com/99designs/gqlgen/graphql/handler/extension"
|
2021-05-02 11:16:32 +00:00
|
|
|
"github.com/Kichiyaki/goutil/safeptr"
|
2021-03-24 04:43:43 +00:00
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/internal/graphql/generated"
|
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/internal/models"
|
2021-03-24 05:15:48 +00:00
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/internal/profession"
|
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/internal/qualification"
|
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/internal/question"
|
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/internal/user"
|
2021-03-24 04:43:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-03-24 05:18:01 +00:00
|
|
|
complexityLimit = 10000
|
|
|
|
countComplexity = 1000
|
|
|
|
professionsTotalFieldComplexity = 100
|
|
|
|
qualificationsTotalFieldComplexity = 100
|
|
|
|
questionsTotalFieldComplexity = 300
|
|
|
|
usersTotalFieldComplexity = 50
|
2021-03-24 04:43:43 +00:00
|
|
|
)
|
|
|
|
|
2021-03-24 05:15:48 +00:00
|
|
|
func GetComplexityLimitExtension() *extension.ComplexityLimit {
|
|
|
|
return extension.FixedComplexityLimit(complexityLimit)
|
|
|
|
}
|
|
|
|
|
2021-03-24 04:43:43 +00:00
|
|
|
func GetComplexityRoot() generated.ComplexityRoot {
|
|
|
|
complexityRoot := generated.ComplexityRoot{}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
|
|
|
complexityRoot.Profession.Qualifications = func(childComplexity int) int {
|
|
|
|
return 10 + childComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
complexityRoot.ProfessionList.Total = getCountComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Query.Professions = func(
|
|
|
|
childComplexity int,
|
|
|
|
filter *models.ProfessionFilter,
|
|
|
|
limit *int,
|
|
|
|
offset *int,
|
|
|
|
sort []string,
|
|
|
|
) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return computeComplexity(
|
|
|
|
childComplexity,
|
2021-05-02 07:15:10 +00:00
|
|
|
safeptr.SafeIntPointer(limit, profession.FetchDefaultLimit),
|
2021-03-24 05:18:01 +00:00
|
|
|
professionsTotalFieldComplexity,
|
2021-03-24 05:15:48 +00:00
|
|
|
1,
|
|
|
|
)
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
|
|
|
complexityRoot.QualificationList.Total = getCountComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Query.Qualifications = func(
|
|
|
|
childComplexity int,
|
|
|
|
filter *models.QualificationFilter,
|
|
|
|
limit *int,
|
|
|
|
offset *int,
|
|
|
|
sort []string,
|
|
|
|
) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return computeComplexity(
|
|
|
|
childComplexity,
|
2021-05-02 07:15:10 +00:00
|
|
|
safeptr.SafeIntPointer(limit, qualification.FetchDefaultLimit),
|
2021-03-24 05:18:01 +00:00
|
|
|
qualificationsTotalFieldComplexity,
|
2021-03-24 05:15:48 +00:00
|
|
|
1,
|
|
|
|
)
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
|
|
|
complexityRoot.QuestionList.Total = getCountComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Query.Questions = func(
|
|
|
|
childComplexity int,
|
|
|
|
filter *models.QuestionFilter,
|
|
|
|
limit *int,
|
|
|
|
offset *int,
|
|
|
|
sort []string,
|
|
|
|
) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return computeComplexity(
|
|
|
|
childComplexity,
|
2021-05-02 07:15:10 +00:00
|
|
|
safeptr.SafeIntPointer(limit, question.FetchDefaultLimit),
|
2021-03-24 05:18:01 +00:00
|
|
|
questionsTotalFieldComplexity,
|
2021-03-24 05:15:48 +00:00
|
|
|
1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
complexityRoot.Query.GenerateTest = func(childComplexity int, qualificationIDs []int, limit *int) int {
|
|
|
|
return computeComplexity(
|
|
|
|
childComplexity,
|
2021-05-02 07:15:10 +00:00
|
|
|
safeptr.SafeIntPointer(limit, question.TestMaxLimit),
|
2021-03-24 05:15:48 +00:00
|
|
|
0,
|
|
|
|
3,
|
|
|
|
)
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
|
|
|
complexityRoot.UserList.Total = getCountComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Query.Users = func(
|
|
|
|
childComplexity int,
|
|
|
|
filter *models.UserFilter,
|
|
|
|
limit *int,
|
|
|
|
offset *int,
|
|
|
|
sort []string,
|
|
|
|
) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return computeComplexity(
|
|
|
|
childComplexity,
|
2021-05-02 07:15:10 +00:00
|
|
|
safeptr.SafeIntPointer(limit, user.FetchMaxLimit),
|
2021-03-24 05:18:01 +00:00
|
|
|
usersTotalFieldComplexity,
|
2021-03-24 05:15:48 +00:00
|
|
|
1,
|
|
|
|
)
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Mutation.CreateProfession = func(childComplexity int, input models.ProfessionInput) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return (complexityLimit / 5) + childComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Mutation.CreateQualification = func(
|
|
|
|
childComplexity int,
|
|
|
|
input models.QualificationInput,
|
|
|
|
) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return (complexityLimit / 5) + childComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Mutation.CreateQuestion = func(childComplexity int, input models.QuestionInput) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return (complexityLimit / 4) + childComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Mutation.CreateUser = func(childComplexity int, input models.UserInput) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return (complexityLimit / 5) + childComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Mutation.SignIn = func(
|
|
|
|
childComplexity int,
|
|
|
|
email string,
|
|
|
|
password string,
|
|
|
|
staySignedIn *bool,
|
|
|
|
) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return (complexityLimit / 2) + childComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Mutation.UpdateManyUsers = func(
|
|
|
|
childComplexity int,
|
|
|
|
ids []int,
|
|
|
|
input models.UserInput,
|
|
|
|
) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return (complexityLimit / 5) + childComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Mutation.UpdateProfession = func(
|
|
|
|
childComplexity int,
|
|
|
|
id int,
|
|
|
|
input models.ProfessionInput,
|
|
|
|
) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return (complexityLimit / 5) + childComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Mutation.UpdateQualification = func(
|
|
|
|
childComplexity int,
|
|
|
|
id int,
|
|
|
|
input models.QualificationInput,
|
|
|
|
) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return (complexityLimit / 5) + childComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Mutation.UpdateQuestion = func(
|
|
|
|
childComplexity int,
|
|
|
|
id int,
|
|
|
|
input models.QuestionInput,
|
|
|
|
) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return (complexityLimit / 4) + childComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
2021-03-24 04:43:43 +00:00
|
|
|
complexityRoot.Mutation.UpdateUser = func(childComplexity int, id int, input models.UserInput) int {
|
2021-03-24 05:15:48 +00:00
|
|
|
return (complexityLimit / 5) + childComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
|
|
|
complexityRoot.Mutation.DeleteProfessions = func(childComplexity int, ids []int) int {
|
|
|
|
return (complexityLimit / 5) + childComplexity
|
2021-03-24 04:43:43 +00:00
|
|
|
}
|
2021-03-24 05:15:48 +00:00
|
|
|
|
|
|
|
complexityRoot.Mutation.DeleteQualifications = func(childComplexity int, ids []int) int {
|
|
|
|
return (complexityLimit / 5) + childComplexity
|
|
|
|
}
|
|
|
|
|
|
|
|
complexityRoot.Mutation.DeleteQuestions = func(childComplexity int, ids []int) int {
|
|
|
|
return (complexityLimit / 4) + childComplexity
|
|
|
|
}
|
|
|
|
|
|
|
|
complexityRoot.Mutation.DeleteUsers = func(childComplexity int, ids []int) int {
|
|
|
|
return (complexityLimit / 5) + childComplexity
|
|
|
|
}
|
|
|
|
|
2021-03-24 04:43:43 +00:00
|
|
|
return complexityRoot
|
|
|
|
}
|
|
|
|
|
|
|
|
func computeComplexity(childComplexity, limit, totalFieldComplexity, multiplyBy int) int {
|
|
|
|
complexity := 0
|
|
|
|
if childComplexity >= countComplexity {
|
|
|
|
childComplexity -= countComplexity
|
|
|
|
complexity += totalFieldComplexity
|
|
|
|
}
|
|
|
|
return limit*childComplexity*multiplyBy + complexity
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCountComplexity(childComplexity int) int {
|
|
|
|
return countComplexity + childComplexity
|
|
|
|
}
|