Skip to content

Commit

Permalink
validation: VariablesInAllowedPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed May 24, 2017
1 parent 83e2f31 commit 0933d24
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 16 deletions.
4 changes: 3 additions & 1 deletion internal/common/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
type InputValue struct {
Name Ident
Type Type
TypeLoc errors.Location
Default Literal
Desc string
Loc errors.Location
TypeLoc errors.Location
}

type InputValueList []*InputValue
Expand All @@ -25,6 +26,7 @@ func (l InputValueList) Get(name string) *InputValue {

func ParseInputValue(l *Lexer) *InputValue {
p := &InputValue{}
p.Loc = l.Location()
p.Desc = l.DescComment()
p.Name = l.ConsumeIdentWithLoc()
l.ConsumeToken(':')
Expand Down
5 changes: 4 additions & 1 deletion internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ func parseOperation(l *common.Lexer, opType OperationType) *Operation {
if l.Peek() == '(' {
l.ConsumeToken('(')
for l.Peek() != ')' {
loc := l.Location()
l.ConsumeToken('$')
op.Vars = append(op.Vars, common.ParseInputValue(l))
iv := common.ParseInputValue(l)
iv.Loc = loc
op.Vars = append(op.Vars, iv)
}
l.ConsumeToken(')')
}
Expand Down
2 changes: 1 addition & 1 deletion internal/tests/testdata/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ require('./src/validation/__tests__/UniqueInputFieldNames-test');
require('./src/validation/__tests__/UniqueOperationNames-test');
require('./src/validation/__tests__/UniqueVariableNames-test');
require('./src/validation/__tests__/VariablesAreInputTypes-test');
// require('./src/validation/__tests__/VariablesInAllowedPosition-test');
require('./src/validation/__tests__/VariablesInAllowedPosition-test');

let output = JSON.stringify(tests, null, 2)
output = output.replace(' Did you mean to use an inline fragment on \\"Dog\\" or \\"Cat\\"?', '');
Expand Down
224 changes: 224 additions & 0 deletions internal/tests/testdata/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -3588,5 +3588,229 @@
"message": "Variable \"$c\" cannot be non-input type \"Pet\"."
}
]
},
{
"name": "Validate: Variables are in allowed positions/Boolean => Boolean",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($booleanArg: Boolean)\n {\n complicatedArgs {\n booleanArgField(booleanArg: $booleanArg)\n }\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/Boolean => Boolean within fragment",
"rule": "VariablesInAllowedPosition",
"query": "\n fragment booleanArgFrag on ComplicatedArgs {\n booleanArgField(booleanArg: $booleanArg)\n }\n query Query($booleanArg: Boolean)\n {\n complicatedArgs {\n ...booleanArgFrag\n }\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/Boolean => Boolean within fragment",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($booleanArg: Boolean)\n {\n complicatedArgs {\n ...booleanArgFrag\n }\n }\n fragment booleanArgFrag on ComplicatedArgs {\n booleanArgField(booleanArg: $booleanArg)\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/Boolean! => Boolean",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($nonNullBooleanArg: Boolean!)\n {\n complicatedArgs {\n booleanArgField(booleanArg: $nonNullBooleanArg)\n }\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/Boolean! => Boolean within fragment",
"rule": "VariablesInAllowedPosition",
"query": "\n fragment booleanArgFrag on ComplicatedArgs {\n booleanArgField(booleanArg: $nonNullBooleanArg)\n }\n\n query Query($nonNullBooleanArg: Boolean!)\n {\n complicatedArgs {\n ...booleanArgFrag\n }\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/Int => Int! with default",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($intArg: Int = 1)\n {\n complicatedArgs {\n nonNullIntArgField(nonNullIntArg: $intArg)\n }\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/[String] => [String]",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($stringListVar: [String])\n {\n complicatedArgs {\n stringListArgField(stringListArg: $stringListVar)\n }\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/[String!] => [String]",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($stringListVar: [String!])\n {\n complicatedArgs {\n stringListArgField(stringListArg: $stringListVar)\n }\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/String => [String] in item position",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($stringVar: String)\n {\n complicatedArgs {\n stringListArgField(stringListArg: [$stringVar])\n }\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/String! => [String] in item position",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($stringVar: String!)\n {\n complicatedArgs {\n stringListArgField(stringListArg: [$stringVar])\n }\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/ComplexInput => ComplexInput",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($complexVar: ComplexInput)\n {\n complicatedArgs {\n complexArgField(complexArg: $complexVar)\n }\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/ComplexInput => ComplexInput in field position",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($boolVar: Boolean = false)\n {\n complicatedArgs {\n complexArgField(complexArg: {requiredArg: $boolVar})\n }\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/Boolean! => Boolean! in directive",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($boolVar: Boolean!)\n {\n dog @include(if: $boolVar)\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/Boolean => Boolean! in directive with default",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($boolVar: Boolean = false)\n {\n dog @include(if: $boolVar)\n }\n ",
"errors": []
},
{
"name": "Validate: Variables are in allowed positions/Int => Int!",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($intArg: Int) {\n complicatedArgs {\n nonNullIntArgField(nonNullIntArg: $intArg)\n }\n }\n ",
"errors": [
{
"message": "Variable \"$intArg\" of type \"Int\" used in position expecting type \"Int!\".",
"locations": [
{
"line": 2,
"column": 19
},
{
"line": 4,
"column": 45
}
]
}
]
},
{
"name": "Validate: Variables are in allowed positions/Int => Int! within fragment",
"rule": "VariablesInAllowedPosition",
"query": "\n fragment nonNullIntArgFieldFrag on ComplicatedArgs {\n nonNullIntArgField(nonNullIntArg: $intArg)\n }\n\n query Query($intArg: Int) {\n complicatedArgs {\n ...nonNullIntArgFieldFrag\n }\n }\n ",
"errors": [
{
"message": "Variable \"$intArg\" of type \"Int\" used in position expecting type \"Int!\".",
"locations": [
{
"line": 6,
"column": 19
},
{
"line": 3,
"column": 43
}
]
}
]
},
{
"name": "Validate: Variables are in allowed positions/Int => Int! within nested fragment",
"rule": "VariablesInAllowedPosition",
"query": "\n fragment outerFrag on ComplicatedArgs {\n ...nonNullIntArgFieldFrag\n }\n\n fragment nonNullIntArgFieldFrag on ComplicatedArgs {\n nonNullIntArgField(nonNullIntArg: $intArg)\n }\n\n query Query($intArg: Int) {\n complicatedArgs {\n ...outerFrag\n }\n }\n ",
"errors": [
{
"message": "Variable \"$intArg\" of type \"Int\" used in position expecting type \"Int!\".",
"locations": [
{
"line": 10,
"column": 19
},
{
"line": 7,
"column": 43
}
]
}
]
},
{
"name": "Validate: Variables are in allowed positions/String over Boolean",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($stringVar: String) {\n complicatedArgs {\n booleanArgField(booleanArg: $stringVar)\n }\n }\n ",
"errors": [
{
"message": "Variable \"$stringVar\" of type \"String\" used in position expecting type \"Boolean\".",
"locations": [
{
"line": 2,
"column": 19
},
{
"line": 4,
"column": 39
}
]
}
]
},
{
"name": "Validate: Variables are in allowed positions/String => [String]",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($stringVar: String) {\n complicatedArgs {\n stringListArgField(stringListArg: $stringVar)\n }\n }\n ",
"errors": [
{
"message": "Variable \"$stringVar\" of type \"String\" used in position expecting type \"[String]\".",
"locations": [
{
"line": 2,
"column": 19
},
{
"line": 4,
"column": 45
}
]
}
]
},
{
"name": "Validate: Variables are in allowed positions/Boolean => Boolean! in directive",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($boolVar: Boolean) {\n dog @include(if: $boolVar)\n }\n ",
"errors": [
{
"message": "Variable \"$boolVar\" of type \"Boolean\" used in position expecting type \"Boolean!\".",
"locations": [
{
"line": 2,
"column": 19
},
{
"line": 3,
"column": 26
}
]
}
]
},
{
"name": "Validate: Variables are in allowed positions/String => Boolean! in directive",
"rule": "VariablesInAllowedPosition",
"query": "\n query Query($stringVar: String) {\n dog @include(if: $stringVar)\n }\n ",
"errors": [
{
"message": "Variable \"$stringVar\" of type \"String\" used in position expecting type \"Boolean!\".",
"locations": [
{
"line": 2,
"column": 19
},
{
"line": 3,
"column": 26
}
]
}
]
}
]
Loading

0 comments on commit 0933d24

Please sign in to comment.