Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make writing validation rules easy #552

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

koron
Copy link
Contributor

@koron koron commented Jun 27, 2020

This PR eases writing and using custom validation rules. It includes two changes:

  1. make graphql.Do() be able to change validation rules
    by adding ValidationRule field to graphql.Params and use it for graphql.ValidateDocument()
  2. make graphql.getArgumentValues() be accessible from other packages
    by adding graphql.GetArgumentValues() as a wrapper of it.

Background

I wrote a cost analysis package:koron-go/gqlcost for graphql-go/graphql as a validation rule.
(It is a port of pa-bru/graphql-cost-analysis)
I use two problematic hacky methods to write it:

  1. modify graphql.SpecifiedRules (refer: koron-go/gqlcost:gqlcost.go#L19-L28)

    There are no ways to use custom validation rules with graphql.Do() currently.
    It is highly problematic to modify graphql.SpecifiedRules because other packages use graphql are affected by this.

  2. Copy pile of codes from graphql-go/graphql:values.go to just access field's argument values (refer: koron-go/gqlcost:values.go#L3-L5)

    I found that there are some cases to want access field arguments value when writing validation rules.
    cost-analysis package uses those to calculate cost with multipliers.
    graphql-go/graphql has a function to do it, but it is private and not exported.
    It wil be great, if those functions are public for other packages which provides custom validation rules.

I want to resolve these problems by this PR.

@koron koron marked this pull request as ready for review June 27, 2020 03:15
@koron koron marked this pull request as draft June 27, 2020 03:16
@coveralls
Copy link

coveralls commented Jun 27, 2020

Coverage Status

Coverage increased (+0.004%) to 92.43% when pulling 3e0d192 on koron:make-validationrule-extensible into d6b7434 on graphql-go:master.

koron added 2 commits June 27, 2020 12:18
add "ValidationRules" field to graphql.Params, to be able to customize
validation rules per query.
to write custom validation rules, sometimes it want to access field's
argument values, but getArgumentValues() is private.  this creates
public version of getArgumentValues() to access from those validation
rules.
@koron koron force-pushed the make-validationrule-extensible branch from c67a102 to 64d8171 Compare June 27, 2020 03:18
@koron koron marked this pull request as ready for review June 27, 2020 03:19
@koron koron changed the title make writing validation rules ease make writing validation rules easy Jun 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants