diff --git a/ast/definition.go b/ast/definition.go index f5c8ea37..36076774 100644 --- a/ast/definition.go +++ b/ast/definition.go @@ -14,7 +14,7 @@ const ( // ObjectDefinition is the core type definition object, it includes all of the definable types // but does *not* cover schema or directives. // -// @vektah: Javascript implementation has different types for all of these, but they are +// @dgraph-io: Javascript implementation has different types for all of these, but they are // more similar than different and don't define any behaviour. I think this style of // "some hot" struct works better, at least for go. // diff --git a/ast/document_test.go b/ast/document_test.go index 529d7c33..74b85f4a 100644 --- a/ast/document_test.go +++ b/ast/document_test.go @@ -6,8 +6,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - . "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/parser" + . "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/parser" ) func TestQueryDocMethods(t *testing.T) { diff --git a/formatter/formatter.go b/formatter/formatter.go index b2465e6d..be85a68e 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -6,7 +6,7 @@ import ( "sort" "strings" - "github.com/vektah/gqlparser/ast" + "github.com/dgraph-io/gqlparser/ast" ) type Formatter interface { @@ -523,7 +523,7 @@ func (f *formatter) FormatVariableDefinition(def *ast.VariableDefinition) { f.FormatValue(def.DefaultValue) } - // TODO https://github.com/vektah/gqlparser/issues/102 + // TODO https://github.com/dgraph-io/gqlparser/issues/102 // VariableDefinition : Variable : Type DefaultValue? Directives[Const]? } diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index 1d7ecece..410f7a72 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -10,10 +10,10 @@ import ( "unicode/utf8" "github.com/stretchr/testify/assert" - "github.com/vektah/gqlparser" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/formatter" - "github.com/vektah/gqlparser/parser" + "github.com/dgraph-io/gqlparser" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/formatter" + "github.com/dgraph-io/gqlparser/parser" ) var update = flag.Bool("u", false, "update golden files") diff --git a/go.mod b/go.mod index cc038079..afee5e89 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/vektah/gqlparser +module github.com/dgraph-io/gqlparser go 1.12 diff --git a/gqlerror/error.go b/gqlerror/error.go index c4c0847a..607c9d07 100644 --- a/gqlerror/error.go +++ b/gqlerror/error.go @@ -5,7 +5,7 @@ import ( "fmt" "strconv" - "github.com/vektah/gqlparser/ast" + "github.com/dgraph-io/gqlparser/ast" ) // Error is the standard graphql error type described in https://facebook.github.io/graphql/draft/#sec-Errors diff --git a/gqlparser.go b/gqlparser.go index 71e46407..67ffc4f5 100644 --- a/gqlparser.go +++ b/gqlparser.go @@ -1,11 +1,11 @@ package gqlparser import ( - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/gqlerror" - "github.com/vektah/gqlparser/parser" - "github.com/vektah/gqlparser/validator" - _ "github.com/vektah/gqlparser/validator/rules" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/gqlerror" + "github.com/dgraph-io/gqlparser/parser" + "github.com/dgraph-io/gqlparser/validator" + _ "github.com/dgraph-io/gqlparser/validator/rules" ) func LoadSchema(str ...*ast.Source) (*ast.Schema, *gqlerror.Error) { @@ -40,3 +40,7 @@ func MustLoadQuery(schema *ast.Schema, str string) *ast.QueryDocument { } return q } + +func LoadVariables(schema *ast.Schema, op *ast.OperationDefinition, vars map[string]interface{}) (map[string]*ast.Value, *gqlerror.Error) { + return validator.Variables(schema, op, vars) +} diff --git a/lexer/lexer.go b/lexer/lexer.go index 89687857..499fab8a 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -4,8 +4,8 @@ import ( "bytes" "unicode/utf8" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/gqlerror" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/gqlerror" ) // Lexer turns graphql request and schema strings into tokens diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index ab16b162..fe2aa7d2 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -3,8 +3,8 @@ package lexer import ( "testing" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/parser/testrunner" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/parser/testrunner" ) func TestLexer(t *testing.T) { diff --git a/lexer/token.go b/lexer/token.go index aef8b729..d76649b8 100644 --- a/lexer/token.go +++ b/lexer/token.go @@ -3,7 +3,7 @@ package lexer import ( "strconv" - "github.com/vektah/gqlparser/ast" + "github.com/dgraph-io/gqlparser/ast" ) const ( diff --git a/parser/parser.go b/parser/parser.go index 96e98402..e96fa002 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -3,9 +3,9 @@ package parser import ( "strconv" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/gqlerror" - "github.com/vektah/gqlparser/lexer" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/gqlerror" + "github.com/dgraph-io/gqlparser/lexer" ) type parser struct { diff --git a/parser/parser_test.go b/parser/parser_test.go index ca06c2b2..0936d251 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -4,8 +4,8 @@ import ( "testing" "github.com/stretchr/testify/require" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/lexer" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/lexer" ) func TestParserUtils(t *testing.T) { diff --git a/parser/query.go b/parser/query.go index 89e1e2e3..4350fc69 100644 --- a/parser/query.go +++ b/parser/query.go @@ -1,10 +1,10 @@ package parser import ( - "github.com/vektah/gqlparser/gqlerror" - "github.com/vektah/gqlparser/lexer" + "github.com/dgraph-io/gqlparser/gqlerror" + "github.com/dgraph-io/gqlparser/lexer" - . "github.com/vektah/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/ast" ) func ParseQuery(source *Source) (*QueryDocument, *gqlerror.Error) { diff --git a/parser/query_test.go b/parser/query_test.go index 2ba9efee..a157f718 100644 --- a/parser/query_test.go +++ b/parser/query_test.go @@ -3,8 +3,8 @@ package parser import ( "testing" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/parser/testrunner" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/parser/testrunner" ) func TestQueryDocument(t *testing.T) { diff --git a/parser/schema.go b/parser/schema.go index 5689e433..ed8d47a3 100644 --- a/parser/schema.go +++ b/parser/schema.go @@ -1,9 +1,9 @@ package parser import ( - . "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/gqlerror" - "github.com/vektah/gqlparser/lexer" + . "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/gqlerror" + "github.com/dgraph-io/gqlparser/lexer" ) func ParseSchema(source *Source) (*SchemaDocument, *gqlerror.Error) { diff --git a/parser/schema_test.go b/parser/schema_test.go index fc77205a..674242bc 100644 --- a/parser/schema_test.go +++ b/parser/schema_test.go @@ -3,8 +3,8 @@ package parser import ( "testing" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/parser/testrunner" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/parser/testrunner" ) func TestSchemaDocument(t *testing.T) { diff --git a/parser/testrunner/runner.go b/parser/testrunner/runner.go index 068b74fa..7ca78a45 100644 --- a/parser/testrunner/runner.go +++ b/parser/testrunner/runner.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/andreyvit/diff" - "github.com/vektah/gqlparser/gqlerror" + "github.com/dgraph-io/gqlparser/gqlerror" "gopkg.in/yaml.v2" ) diff --git a/readme.md b/readme.md index 8d1362bb..4d90d1eb 100644 --- a/readme.md +++ b/readme.md @@ -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)](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) +gqlparser [![CircleCI](https://badgen.net/circleci/github/dgraph-io/gqlparser/master)](https://circleci.com/gh/dgraph-io/gqlparser) [![Go Report Card](https://goreportcard.com/badge/github.com/dgraph-io/gqlparser)](https://goreportcard.com/report/github.com/dgraph-io/gqlparser) [![Coverage Status](https://badgen.net/coveralls/c/github/dgraph-io/gqlparser)](https://coveralls.io/github/dgraph-io/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. diff --git a/validator/error.go b/validator/error.go index f354dee5..983e1b96 100644 --- a/validator/error.go +++ b/validator/error.go @@ -3,8 +3,8 @@ package validator import ( "fmt" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/gqlerror" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/gqlerror" ) type ErrorOption func(err *gqlerror.Error) diff --git a/validator/imported_test.go b/validator/imported_test.go index 8139304f..61de910e 100644 --- a/validator/imported_test.go +++ b/validator/imported_test.go @@ -12,9 +12,9 @@ import ( "testing" "github.com/stretchr/testify/require" - "github.com/vektah/gqlparser" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/gqlerror" + "github.com/dgraph-io/gqlparser" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/gqlerror" "gopkg.in/yaml.v2" ) diff --git a/validator/prelude.go b/validator/prelude.go index 00ae78f7..08ce53f5 100644 --- a/validator/prelude.go +++ b/validator/prelude.go @@ -1,6 +1,6 @@ package validator -import "github.com/vektah/gqlparser/ast" +import "github.com/dgraph-io/gqlparser/ast" var Prelude = &ast.Source{ Name: "prelude.graphql", diff --git a/validator/rules/fields_on_correct_type.go b/validator/rules/fields_on_correct_type.go index 69148d52..944b9e29 100644 --- a/validator/rules/fields_on_correct_type.go +++ b/validator/rules/fields_on_correct_type.go @@ -4,8 +4,8 @@ import ( "fmt" "sort" - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/fragments_on_composite_types.go b/validator/rules/fragments_on_composite_types.go index a4a48246..fddad16b 100644 --- a/validator/rules/fragments_on_composite_types.go +++ b/validator/rules/fragments_on_composite_types.go @@ -3,8 +3,8 @@ package validator import ( "fmt" - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/known_argument_names.go b/validator/rules/known_argument_names.go index 1a46431d..ef9e3ee7 100644 --- a/validator/rules/known_argument_names.go +++ b/validator/rules/known_argument_names.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/known_directives.go b/validator/rules/known_directives.go index dc4353ef..8cc73872 100644 --- a/validator/rules/known_directives.go +++ b/validator/rules/known_directives.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/known_fragment_names.go b/validator/rules/known_fragment_names.go index ec91588c..78b8d14e 100644 --- a/validator/rules/known_fragment_names.go +++ b/validator/rules/known_fragment_names.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/known_type_names.go b/validator/rules/known_type_names.go index 223086b3..0cc5cae6 100644 --- a/validator/rules/known_type_names.go +++ b/validator/rules/known_type_names.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/lone_anonymous_operation.go b/validator/rules/lone_anonymous_operation.go index dd232142..83f26b9c 100644 --- a/validator/rules/lone_anonymous_operation.go +++ b/validator/rules/lone_anonymous_operation.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/no_fragment_cycles.go b/validator/rules/no_fragment_cycles.go index 7511529b..d7c0bd0a 100644 --- a/validator/rules/no_fragment_cycles.go +++ b/validator/rules/no_fragment_cycles.go @@ -4,8 +4,8 @@ import ( "fmt" "strings" - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/no_undefined_variables.go b/validator/rules/no_undefined_variables.go index 505206be..9755cb06 100644 --- a/validator/rules/no_undefined_variables.go +++ b/validator/rules/no_undefined_variables.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/no_unused_fragments.go b/validator/rules/no_unused_fragments.go index 4aa835f5..5d8d9fd1 100644 --- a/validator/rules/no_unused_fragments.go +++ b/validator/rules/no_unused_fragments.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/no_unused_variables.go b/validator/rules/no_unused_variables.go index 28cf7736..cf19bbc6 100644 --- a/validator/rules/no_unused_variables.go +++ b/validator/rules/no_unused_variables.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/overlapping_fields_can_be_merged.go b/validator/rules/overlapping_fields_can_be_merged.go index bb2f1831..7c7264ff 100644 --- a/validator/rules/overlapping_fields_can_be_merged.go +++ b/validator/rules/overlapping_fields_can_be_merged.go @@ -5,8 +5,8 @@ import ( "fmt" "reflect" - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/possible_fragment_spreads.go b/validator/rules/possible_fragment_spreads.go index 04611834..b3cba7ee 100644 --- a/validator/rules/possible_fragment_spreads.go +++ b/validator/rules/possible_fragment_spreads.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/provided_required_arguments.go b/validator/rules/provided_required_arguments.go index 55791a6b..49d49f14 100644 --- a/validator/rules/provided_required_arguments.go +++ b/validator/rules/provided_required_arguments.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/scalar_leafs.go b/validator/rules/scalar_leafs.go index bb961f44..783b0577 100644 --- a/validator/rules/scalar_leafs.go +++ b/validator/rules/scalar_leafs.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/single_field_subscriptions.go b/validator/rules/single_field_subscriptions.go index 53003c11..a3c3b14e 100644 --- a/validator/rules/single_field_subscriptions.go +++ b/validator/rules/single_field_subscriptions.go @@ -3,8 +3,8 @@ package validator import ( "strconv" - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/unique_argument_names.go b/validator/rules/unique_argument_names.go index 0ddcde72..03b4f54d 100644 --- a/validator/rules/unique_argument_names.go +++ b/validator/rules/unique_argument_names.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/unique_directives_per_location.go b/validator/rules/unique_directives_per_location.go index 077c4687..bb7b8b13 100644 --- a/validator/rules/unique_directives_per_location.go +++ b/validator/rules/unique_directives_per_location.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/unique_fragment_names.go b/validator/rules/unique_fragment_names.go index 46a8b7c7..b742536b 100644 --- a/validator/rules/unique_fragment_names.go +++ b/validator/rules/unique_fragment_names.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/unique_input_field_names.go b/validator/rules/unique_input_field_names.go index f254d588..d7bf4824 100644 --- a/validator/rules/unique_input_field_names.go +++ b/validator/rules/unique_input_field_names.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/unique_operation_names.go b/validator/rules/unique_operation_names.go index c1ab56be..023882a6 100644 --- a/validator/rules/unique_operation_names.go +++ b/validator/rules/unique_operation_names.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/unique_variable_names.go b/validator/rules/unique_variable_names.go index 70590a88..ca003272 100644 --- a/validator/rules/unique_variable_names.go +++ b/validator/rules/unique_variable_names.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/values_of_correct_type.go b/validator/rules/values_of_correct_type.go index d64cc666..75fb7d2c 100644 --- a/validator/rules/values_of_correct_type.go +++ b/validator/rules/values_of_correct_type.go @@ -3,8 +3,8 @@ package validator import ( "fmt" - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/variables_are_input_types.go b/validator/rules/variables_are_input_types.go index 9d58ae1c..123d4ccf 100644 --- a/validator/rules/variables_are_input_types.go +++ b/validator/rules/variables_are_input_types.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/rules/variables_in_allowed_position.go b/validator/rules/variables_in_allowed_position.go index e6d97c9f..abce1c1c 100644 --- a/validator/rules/variables_in_allowed_position.go +++ b/validator/rules/variables_in_allowed_position.go @@ -1,8 +1,8 @@ package validator import ( - "github.com/vektah/gqlparser/ast" - . "github.com/vektah/gqlparser/validator" + "github.com/dgraph-io/gqlparser/ast" + . "github.com/dgraph-io/gqlparser/validator" ) func init() { diff --git a/validator/schema.go b/validator/schema.go index 3242822f..32cca3ef 100644 --- a/validator/schema.go +++ b/validator/schema.go @@ -6,9 +6,9 @@ import ( "strconv" "strings" - . "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/gqlerror" - "github.com/vektah/gqlparser/parser" + . "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/gqlerror" + "github.com/dgraph-io/gqlparser/parser" ) func LoadSchema(inputs ...*Source) (*Schema, *gqlerror.Error) { @@ -179,7 +179,7 @@ func validateDefinition(schema *Schema, def *Definition) *gqlerror.Error { if err := validateArgs(schema, field.Arguments, nil); err != nil { return err } - if err := validateDirectives(schema, field.Directives, nil); err != nil { + if err := validateDirectives(schema, field.Directives, LocationFieldDefinition, nil); err != nil { return err } } @@ -245,7 +245,7 @@ func validateDefinition(schema *Schema, def *Definition) *gqlerror.Error { } } - return validateDirectives(schema, def.Directives, nil) + return validateDirectives(schema, def.Directives, DirectiveLocation(def.Kind), nil) } func validateTypeRef(schema *Schema, typ *Type) *gqlerror.Error { @@ -274,14 +274,25 @@ func validateArgs(schema *Schema, args ArgumentDefinitionList, currentDirective def.Kind, ) } - if err := validateDirectives(schema, arg.Directives, currentDirective); err != nil { + if err := validateDirectives(schema, arg.Directives, LocationArgumentDefinition, currentDirective); err != nil { return err } } return nil } -func validateDirectives(schema *Schema, dirs DirectiveList, currentDirective *DirectiveDefinition) *gqlerror.Error { +func validateDirectivesArgs(schema *Schema, dir *Directive) *gqlerror.Error { + dirDefinition := schema.Directives[dir.Name] + possibleArgsList := dirDefinition.Arguments + for _, arg := range dir.Arguments { + if possibleArgsList.ForName(arg.Name) == nil { + return gqlerror.ErrorPosf(arg.Position, "Invalid argument %s with Directive %s", 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 { // now, GraphQL spec doesn't have reserved directive name @@ -293,6 +304,18 @@ func validateDirectives(schema *Schema, dirs DirectiveList, currentDirective *Di if schema.Directives[dir.Name] == nil { return gqlerror.ErrorPosf(dir.Position, "Undefined directive %s.", dir.Name) } + validKind := false + for _, dirLocation := range schema.Directives[dir.Name].Locations { + if dirLocation == location { + validKind = true + } + } + if !validKind { + return gqlerror.ErrorPosf(dir.Position, "Directive %s is not applicable on %s.", dir.Name, location) + } + if err := validateDirectivesArgs(schema, dir); err != nil { + return err + } dir.Definition = schema.Directives[dir.Name] } return nil diff --git a/validator/schema_test.go b/validator/schema_test.go index d8e03bbf..99ac0762 100644 --- a/validator/schema_test.go +++ b/validator/schema_test.go @@ -5,8 +5,8 @@ import ( "testing" "github.com/stretchr/testify/require" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/parser/testrunner" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/parser/testrunner" ) func TestLoadSchema(t *testing.T) { diff --git a/validator/schema_test.yml b/validator/schema_test.yml index b1d85c44..e456feac 100644 --- a/validator/schema_test.yml +++ b/validator/schema_test.yml @@ -505,6 +505,46 @@ directives: message: 'cannot use Interface as argument a because INTERFACE is not a valid input type' locations: [{line: 2, column: 14}] + - name: Invalid location usage not allowed + input: | + directive @test on FIELD_DEFINITION + input I1 @test { f: String } + + error: + message: 'Directive test is not applicable on INPUT_OBJECT.' + locations: [{line: 2, column: 11}] + + - name: Valid location usage + input: | + directive @test on FIELD_DEFINITION + directive @inp on INPUT_OBJECT + input I1 @inp { f: String } + type P { name: String @test } + + - name: Valid directive arg inside type defn + input: | + enum DgraphIndex { + int + int64 + float + bool + hash + exact + term + fulltext + trigram + regexp + year + month + day + hour + } + directive @search(by: [DgraphIndex!]) on FIELD_DEFINITION + type S { name: String! @search(bi: [hash]) } + error: + message: "Invalid argument bi with Directive search" + locations: [{line: 18, column: 32}] + entry points: - name: multiple schema entry points input: | diff --git a/validator/testdata/extensions.graphql b/validator/testdata/extensions.graphql index 525cb359..968c122e 100644 --- a/validator/testdata/extensions.graphql +++ b/validator/testdata/extensions.graphql @@ -26,4 +26,4 @@ extend type Dog { owner: Person! @permission(permission: "admin") } -directive @permission(permission: String!) on FIELD \ No newline at end of file +directive @permission(permission: String!) on FIELD_DEFINITION diff --git a/validator/testdata/post.graphql b/validator/testdata/post.graphql new file mode 100644 index 00000000..a6290fd0 --- /dev/null +++ b/validator/testdata/post.graphql @@ -0,0 +1,39 @@ +directive @fetch(fid: String!) on OBJECT + +type Author @fetch(fid: "dummy") { + id: Int + firstName: String + lastName: String + posts: [Post] +} + +type Post @fetch(fid: "dummy") { + id: Int! + title: String + text: String + views: Int +} + +type Query { + getPost(id: Int!): Post + getAuthor(id: Int!): Author +} + +input AuthorInput { + id: Int + firstName: String + lastName: String + posts: [PostInput] +} + +input PostInput { + id: Int! + title: String + text: String + views: Int +} + +type Mutation { + setAuthor(data: AuthorInput!): String + setPost(data: PostInput!): String +} diff --git a/validator/validator.go b/validator/validator.go index bbacec6f..cb0d0c09 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -1,8 +1,8 @@ package validator import ( - . "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/gqlerror" + . "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/gqlerror" ) type AddErrFunc func(options ...ErrorOption) diff --git a/validator/vars.go b/validator/vars.go index 40af5461..62d2d0f1 100644 --- a/validator/vars.go +++ b/validator/vars.go @@ -2,16 +2,73 @@ package validator import ( "reflect" + "strconv" "strings" "fmt" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/gqlerror" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/gqlerror" ) var UnexpectedType = fmt.Errorf("Unexpected Type") +func Variables(schema *ast.Schema, op *ast.OperationDefinition, variables map[string]interface{}) (map[string]*ast.Value, *gqlerror.Error) { + coercedVars := map[string]*ast.Value{} + + validator := varValidator{ + path: []interface{}{"variable"}, + schema: schema, + } + + for _, v := range op.VariableDefinitions { + validator.path = append(validator.path, v.Variable) + + if !v.Definition.IsInputType() { + return nil, gqlerror.ErrorPathf(validator.path, "must an input type") + } + + val, hasValue := variables[v.Variable] + if !hasValue { + if v.DefaultValue != nil { + var err error + val, err = v.DefaultValue.Value(nil) + if err != nil { + return nil, gqlerror.WrapPath(validator.path, err) + } + hasValue = true + } else if v.Type.NonNull { + return nil, gqlerror.ErrorPathf(validator.path, "must be defined") + } + } + + if hasValue { + if val == nil { + if v.Type.NonNull { + return nil, gqlerror.ErrorPathf(validator.path, "cannot be null") + } + coercedVars[v.Variable] = nil + } else { + rv := reflect.ValueOf(val) + if rv.Kind() == reflect.Ptr || rv.Kind() == reflect.Interface { + rv = rv.Elem() + } + + vars, err := validator.getVars(v.Type, rv) + if err != nil { + return nil, err + } + + coercedVars[v.Variable] = vars + } + } + + validator.path = validator.path[0 : len(validator.path)-1] + } + + return coercedVars, nil +} + // VariableValues coerces and validates variable values func VariableValues(schema *ast.Schema, op *ast.OperationDefinition, variables map[string]interface{}) (map[string]interface{}, *gqlerror.Error) { coercedVars := map[string]interface{}{} @@ -73,6 +130,211 @@ type varValidator struct { schema *ast.Schema } +func (v *varValidator) getVars(typ *ast.Type, val reflect.Value) (*ast.Value, *gqlerror.Error) { + currentPath := v.path + resetPath := func() { + v.path = currentPath + } + defer resetPath() + + value := &ast.Value{ + Raw: val.String(), + Children: make(ast.ChildValueList, 0, 0), + } + + if typ.Elem != nil { + if val.Kind() != reflect.Slice { + return nil, gqlerror.ErrorPathf(v.path, "must be an array") + } + + value.Kind = ast.ListValue + + for i := 0; i < val.Len(); i++ { + resetPath() + v.path = append(v.path, i) + field := val.Index(i) + + if field.Kind() == reflect.Ptr || field.Kind() == reflect.Interface { + if typ.Elem.NonNull && field.IsNil() { + return nil, gqlerror.ErrorPathf(v.path, "cannot be null") + } + field = field.Elem() + } + + vars, err := v.getVars(typ.Elem, field) + if err != nil { + return nil, err + } + + value.Children = append(value.Children, &ast.ChildValue{ + Name: strconv.Itoa(i), + Value: vars, + }) + + // typ.Elem is of type [typ], so it doesn't exist in v.schema.Types + //TODO find Definition from typ.Elem instead + value.Definition = vars.Definition + } + + return value, nil + } + + if !typ.NonNull && !val.IsValid() { + // If the type is not null and we got a invalid value namely null/nil, then it's valid + return nil, nil + } + def := v.schema.Types[typ.NamedType] + if def == nil { + panic(fmt.Errorf("missing def for %s", typ.NamedType)) + } + value.Definition = def + value.Raw = val.String() + + switch def.Kind { + case ast.Enum: + kind := val.Type().Kind() + value.Kind = ast.EnumValue + if kind != reflect.Int && kind != reflect.Int32 && kind != reflect.Int64 && kind != reflect.String { + return nil, gqlerror.ErrorPathf(v.path, "enums must be ints or strings") + } + isValidEnum := false + for _, enumVal := range def.EnumValues { + if strings.EqualFold(val.String(), enumVal.Name) { + isValidEnum = true + } + } + if !isValidEnum { + return nil, gqlerror.ErrorPathf(v.path, "%s is not a valid %s", val.String(), def.Name) + } + return value, nil + case ast.Scalar: + kind := val.Type().Kind() + switch typ.NamedType { + case "Int": + if kind == reflect.String { + res, err := strconv.ParseInt(val.String(), 10, 64) + if err != nil { + return nil, gqlerror.ErrorPathf(v.path, "Invalid %s provided : %s", kind.String(), val.String()) + } else { + value.Raw = strconv.FormatInt(res, 10) + value.Kind = ast.IntValue + return value, nil + } + } + if kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 { + value.Raw = strconv.FormatInt(val.Int(), 10) + value.Kind = ast.IntValue + return value, nil + } + case "Float": + if kind == reflect.String { + res, err := strconv.ParseFloat(val.String(), 10) + if err != nil { + return nil, gqlerror.ErrorPathf(v.path, "Invalid %s provided : %s", kind.String(), val.String()) + } else { + value.Raw = strconv.FormatFloat(res, 'E', -1, 64) + value.Kind = ast.FloatValue + return value, nil + } + } + if kind == reflect.Float32 || kind == reflect.Float64 || kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 { + value.Raw = strconv.FormatFloat(val.Float(), 'E', -1, 64) + value.Kind = ast.FloatValue + return value, nil + } + case "String": + if kind == reflect.String { + value.Kind = ast.StringValue + return value, nil + } + + case "Boolean": + if kind == reflect.Bool { + value.Kind = ast.BooleanValue + value.Raw = strconv.FormatBool(val.Bool()) + return value, nil + } + + case "ID": + if kind == reflect.String { + _, err := strconv.ParseInt(val.String(), 10, 64) + if err != nil { + return nil, gqlerror.ErrorPathf(v.path, "Invalid %s provided : %s", kind.String(), val.String()) + } else { + value.Raw = strconv.FormatInt(val.Int(), 10) + value.Kind = ast.IntValue + return value, nil + } + } + if kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 { + value.Kind = ast.IntValue + value.Raw = strconv.FormatInt(val.Int(), 10) + return value, nil + } + default: + // assume custom scalars are ok + value.Kind = ast.StringValue + return value, nil + } + return nil, gqlerror.ErrorPathf(v.path, "cannot use %s as %s", kind.String(), typ.NamedType) + case ast.InputObject: + if val.Kind() != reflect.Map { + return nil, gqlerror.ErrorPathf(v.path, "must be a %s", def.Name) + } + + value.Kind = ast.ObjectValue + + // check for unknown fields + for _, name := range val.MapKeys() { + val.MapIndex(name) + fieldDef := def.Fields.ForName(name.String()) + resetPath() + v.path = append(v.path, name.String()) + + if fieldDef == nil { + return nil, gqlerror.ErrorPathf(v.path, "unknown field") + } + } + + for _, fieldDef := range def.Fields { + resetPath() + v.path = append(v.path, fieldDef.Name) + + field := val.MapIndex(reflect.ValueOf(fieldDef.Name)) + if !field.IsValid() { + if fieldDef.Type.NonNull { + return nil, gqlerror.ErrorPathf(v.path, "must be defined") + } + continue + } + + if field.Kind() == reflect.Ptr || field.Kind() == reflect.Interface { + if fieldDef.Type.NonNull && field.IsNil() { + return nil, gqlerror.ErrorPathf(v.path, "cannot be null") + } + //allow null object field and skip it + if !fieldDef.Type.NonNull && field.IsNil() { + continue + } + field = field.Elem() + } + + val, err := v.getVars(fieldDef.Type, field) + if err != nil { + return nil, err + } + value.Children = append(value.Children, &ast.ChildValue{ + Name: fieldDef.Name, + Value: val, + }) + } + default: + panic(fmt.Errorf("unsupported type %s", def.Kind)) + } + + return value, nil +} + func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) *gqlerror.Error { currentPath := v.path resetPath := func() { diff --git a/validator/vars_test.go b/validator/vars_test.go index 09b0245c..37d178cd 100644 --- a/validator/vars_test.go +++ b/validator/vars_test.go @@ -6,12 +6,51 @@ import ( "encoding/json" + "github.com/dgraph-io/gqlparser" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/validator" "github.com/stretchr/testify/require" - "github.com/vektah/gqlparser" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/validator" ) +func TestGetVar(t *testing.T) { + schema := gqlparser.MustLoadSchema(&ast.Source{ + Name: "vars.graphql", + Input: mustReadFile("./testdata/post.graphql"), + }) + + t.Run("validation", func(t *testing.T) { + t.Run("int", func(t *testing.T) { + q := gqlparser.MustLoadQuery(schema, `mutation ($data: PostInput!) { + setPost(data: $data) +}`) + vars := map[string]interface{}{ + "data": map[string]interface{}{ + "id": 1, + }, + } + result, gerr := validator.Variables(schema, q.Operations.ForName(""), vars) + require.Nil(t, gerr) + + require.Equal(t, result["data"].Children[0].Value.Kind, ast.IntValue) + require.Equal(t, result["data"].Children[0].Value.Raw, "1") + }) + + t.Run("invalid int", func(t *testing.T) { + q := gqlparser.MustLoadQuery(schema, `mutation ($data: PostInput!) { + setPost(data: $data) +}`) + vars := map[string]interface{}{ + "data": map[string]interface{}{ + "id": "hi", + }, + } + _, gerr := validator.Variables(schema, q.Operations.ForName(""), vars) + require.NotNil(t, gerr) + }) + + }) +} + func TestValidateVars(t *testing.T) { schema := gqlparser.MustLoadSchema(&ast.Source{ Name: "vars.graphql", diff --git a/validator/walk.go b/validator/walk.go index 751ba1f1..ea4f0db4 100644 --- a/validator/walk.go +++ b/validator/walk.go @@ -4,7 +4,7 @@ import ( "context" "fmt" - "github.com/vektah/gqlparser/ast" + "github.com/dgraph-io/gqlparser/ast" ) type Events struct { diff --git a/validator/walk_test.go b/validator/walk_test.go index 1d010380..8fed399c 100644 --- a/validator/walk_test.go +++ b/validator/walk_test.go @@ -4,8 +4,8 @@ import ( "testing" "github.com/stretchr/testify/require" - "github.com/vektah/gqlparser/ast" - "github.com/vektah/gqlparser/parser" + "github.com/dgraph-io/gqlparser/ast" + "github.com/dgraph-io/gqlparser/parser" ) func TestWalker(t *testing.T) {