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

Integrate DGraph fork changes #179

Draft
wants to merge 38 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c92922d
Merge pull request #5 from vektah/master
pawanrawal Nov 3, 2020
2487809
Changed vektah to dgraph-io
Dec 11, 2019
11dafed
Fix import paths to have v2 suffix
pawanrawal Nov 3, 2020
8dcf919
added code and test for array coercing
JatinDev543 Nov 3, 2020
c23012d
Merge pull request #7 from dgraph-io/jatin/GraphQl-789
JatinDev543 Nov 3, 2020
b296ca2
changed slice type to interface
JatinDev543 Dec 14, 2020
af5c357
Merge pull request #8 from dgraph-io/jatin/GRAPHQL-745
JatinDev543 Dec 14, 2020
479ab4d
added input coercion to string for ID types
JatinDev543 Jan 18, 2021
1590604
clean code and added test for single ID case
JatinDev543 Jan 19, 2021
b48c15b
Merge pull request #9 from dgraph-io/jatin/GRAPHQL-959
JatinDev543 Jan 20, 2021
41867a0
removed extra check
JatinDev543 Jan 21, 2021
a39ba99
added test
JatinDev543 Jan 21, 2021
83d89f4
reformat code a bit
JatinDev543 Jan 21, 2021
d604941
Merge pull request #10 from dgraph-io/jatin/fixIdBug
JatinDev543 Jan 21, 2021
f73942d
added code for Int input coercion and tests
JatinDev543 Feb 7, 2021
3a9b1f5
added test for int64 out of range
JatinDev543 Feb 12, 2021
147d6c6
Merge pull request #11 from dgraph-io/jatin/GRAPHQL-1006
JatinDev543 Feb 12, 2021
3521f39
added validation for parameterized cascade arguments present in varia…
JatinDev543 Feb 24, 2021
a86723d
format imports
JatinDev543 Feb 24, 2021
45e53e4
support variables in validation hooks
JatinDev543 Feb 25, 2021
cb06f9b
fix tests
JatinDev543 Feb 25, 2021
d9375ee
clean code
JatinDev543 Feb 25, 2021
f103f8e
Merge pull request #12 from dgraph-io/jatin/GRAPHQL-1007
JatinDev543 Feb 25, 2021
4e8354a
fixed panic
JatinDev543 Feb 26, 2021
6c9552e
Merge pull request #13 from dgraph-io/jatin/Fix-stringPanic
JatinDev543 Feb 26, 2021
0488d0a
fix error
JatinDev543 Feb 26, 2021
6adf0a9
Merge pull request #14 from dgraph-io/jatin/Fix-stringError
JatinDev543 Feb 26, 2021
06de469
remove support of string -->int32 coercion
JatinDev543 Mar 15, 2021
eb159c9
Merge pull request #15 from dgraph-io/jatin/GRAPHQL-1081
JatinDev543 Mar 16, 2021
d24123f
fix(Parser): Add Directive argument validator
all-seeing-code Mar 19, 2021
f3f3532
Add tests
all-seeing-code Mar 19, 2021
7914f56
Merge pull request #16 from dgraph-io/anurags92/gwlparserfix
all-seeing-code Mar 19, 2021
97b20a5
chore: adding order to the validation rules
aman-bansal Aug 31, 2021
40ef820
fmt run
aman-bansal Aug 31, 2021
4b0b96a
Merge pull request #17 from dgraph-io/aman/add_order_to_rules
aman-bansal Aug 31, 2021
adfb85a
fix(validate): fix race condition in rules sort
NamanJain8 Dec 2, 2021
f1c512b
Merge pull request #18 from dgraph-io/naman/sort
NamanJain8 Dec 2, 2021
7a7d21b
Replace dgraph-io with vektah
StevenACoffman Jan 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"testing"
"unicode/utf8"

"github.com/stretchr/testify/assert"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/formatter"
"github.com/vektah/gqlparser/v2/parser"
"github.com/stretchr/testify/assert"
)

var update = flag.Bool("u", false, "update golden files")
Expand Down
2 changes: 1 addition & 1 deletion gqlerror/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package gqlerror
import (
"testing"

"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/v2/ast"
"github.com/stretchr/testify/require"
)

func TestErrorFormatting(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion gqlparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func LoadQuery(schema *ast.Schema, str string) (*ast.QueryDocument, gqlerror.Lis
if err != nil {
return nil, gqlerror.List{err}
}
errs := validator.Validate(schema, query)
errs := validator.Validate(schema, query, nil)
if errs != nil {
return nil, errs
}
Expand Down
2 changes: 1 addition & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package parser
import (
"testing"

"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/lexer"
"github.com/stretchr/testify/require"
)

func TestParserUtils(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
gqlparser [![CircleCI](https://badgen.net/circleci/github/vektah/gqlparser/master)](https://circleci.com/gh/vektah/gqlparser) [![Go Report Card](https://goreportcard.com/badge/github.com/vektah/gqlparser/v2)](https://goreportcard.com/report/github.com/vektah/gqlparser/v2) [![Coverage Status](https://badgen.net/coveralls/c/github/vektah/gqlparser)](https://coveralls.io/github/vektah/gqlparser?branch=master)
gqlparser [![CircleCI](https://badgen.net/circleci/github/vektah/gqlparser/v2/master)](https://circleci.com/gh/vektah/gqlparser) [![Go Report Card](https://goreportcard.com/badge/github.com/vektah/gqlparser)](https://goreportcard.com/report/github.com/vektah/gqlparser) [![Coverage Status](https://badgen.net/coveralls/c/github/vektah/gqlparser)](https://coveralls.io/github/vektah/gqlparser?branch=master)
===

This is a parser for graphql, written to mirror the graphql-js reference implementation as closely while remaining idiomatic and easy to use.
Expand Down
2 changes: 1 addition & 1 deletion validator/imported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

Expand Down
22 changes: 20 additions & 2 deletions validator/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func ValidateSchemaDocument(ast *SchemaDocument) (*Schema, *gqlerror.Error) {
def := schema.Types[ext.Name]
if def == nil {
schema.Types[ext.Name] = &Definition{
Kind: ext.Kind,
Name: ext.Name,
Kind: ext.Kind,
Name: ext.Name,
Position: ext.Position,
}
def = schema.Types[ext.Name]
Expand Down Expand Up @@ -289,6 +289,21 @@ func validateArgs(schema *Schema, args ArgumentDefinitionList, currentDirective
return nil
}

func validateDirectiveArgs(dir *Directive, schema *Schema) *gqlerror.Error {
allowedArgs := make(map[string]struct{})
for _, arg := range schema.Directives[dir.Name].Arguments {
allowedArgs[arg.Name] = struct{}{}
}

for _, arg := range dir.Arguments {
if _, ok := allowedArgs[arg.Name]; !ok {
return gqlerror.ErrorPosf(dir.Position, "%s is not supported as an argument for %s directive.", arg.Name, dir.Name)
}
}
return nil

}

func validateDirectives(schema *Schema, dirs DirectiveList, location DirectiveLocation, currentDirective *DirectiveDefinition) *gqlerror.Error {
for _, dir := range dirs {
if err := validateName(dir.Position, dir.Name); err != nil {
Expand All @@ -301,6 +316,9 @@ func validateDirectives(schema *Schema, dirs DirectiveList, location DirectiveLo
if schema.Directives[dir.Name] == nil {
return gqlerror.ErrorPosf(dir.Position, "Undefined directive %s.", dir.Name)
}
if err := validateDirectiveArgs(dir, schema); err != nil {
return err
}
validKind := false
for _, dirLocation := range schema.Directives[dir.Name].Locations {
if dirLocation == location {
Expand Down
2 changes: 1 addition & 1 deletion validator/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/parser/testrunner"
"github.com/stretchr/testify/require"
)

func TestLoadSchema(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions validator/schema_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,16 @@ directives:

directive @A(a: Input, b: Scalar, c: Enum) on FIELD_DEFINITION

- name: Valid arg for directive
input: |
type User @include(aggregate: false) {
name: String
}

error:
message: 'aggregate is not supported as an argument for include directive.'
locations: [{line: 1, column: 12}]

- name: Objects not allowed
input: |
type Object { id: ID }
Expand Down
11 changes: 10 additions & 1 deletion validator/testdata/vars.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type Query {
optionalIntArg(i: Int): Boolean!
intArg(i: Int!): Boolean!
int64Arg(i: Int64!): Boolean!
stringArg(i: String): Boolean!
boolArg(i: Boolean!): Boolean!
floatArg(i: Float!): Boolean!
Expand All @@ -10,10 +11,14 @@ type Query {
defaultStructArg(i: InputType! = {name: "foo"}): Boolean!
arrayArg(i: [InputType!]): Boolean!
intArrayArg(i: [Int]): Boolean!
idArrayArg(i: [ID]): Boolean!
stringArrayArg(i: [String]): Boolean!
boolArrayArg(i: [Boolean]): Boolean!
typeArrayArg(i: [CustomType]): Boolean!
}

scalar Int64

input InputType {
name: String!
nullName: String
Expand All @@ -26,8 +31,12 @@ input Embedded {
name: String!
}

input CustomType {
and: [Int!]
}

enum Enum {
A
}

scalar Custom
scalar Custom
19 changes: 16 additions & 3 deletions validator/validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package validator

import (
"sort"

. "github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
)
Expand All @@ -11,7 +13,9 @@ type ruleFunc func(observers *Events, addError AddErrFunc)

type rule struct {
name string
rule ruleFunc
// rules will be called in the ascending order
order int
rule ruleFunc
}

var rules []rule
Expand All @@ -22,7 +26,16 @@ func AddRule(name string, f ruleFunc) {
rules = append(rules, rule{name: name, rule: f})
}

func Validate(schema *Schema, doc *QueryDocument) gqlerror.List {
// AddRuleWithOrder to rule set with an order.
// f is called once each time `Validate` is executed.
func AddRuleWithOrder(name string, order int, f ruleFunc) {
rules = append(rules, rule{name: name, order: order, rule: f})
sort.Slice(rules, func(i, j int) bool {
return rules[i].order < rules[j].order
})
}

func Validate(schema *Schema, doc *QueryDocument, variables map[string]interface{}) gqlerror.List {
var errs gqlerror.List

observers := &Events{}
Expand All @@ -39,6 +52,6 @@ func Validate(schema *Schema, doc *QueryDocument) gqlerror.List {
})
}

Walk(schema, doc, observers)
Walk(schema, doc, observers, variables)
return errs
}
4 changes: 2 additions & 2 deletions validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package validator_test
import (
"testing"

"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/parser"
"github.com/vektah/gqlparser/v2/validator"
"github.com/stretchr/testify/require"
)

func TestExtendingNonExistantTypes(t *testing.T) {
Expand Down Expand Up @@ -37,5 +37,5 @@ extend type Query {
}
}`})
require.Nil(t, err)
require.Nil(t, validator.Validate(s, q))
require.Nil(t, validator.Validate(s, q, nil))
}
Loading