forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_known_directives_rule_test.go
165 lines (145 loc) · 5.23 KB
/
rules_known_directives_rule_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package graphql_test
import (
"testing"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/testutil"
)
func TestValidate_KnownDirectives_WithNoDirectives(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.KnownDirectivesRule, `
query Foo {
name
...Frag
}
fragment Frag on Dog {
name
}
`)
}
func TestValidate_KnownDirectives_WithKnownDirective(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.KnownDirectivesRule, `
{
dog @include(if: true) {
name
}
human @skip(if: false) {
name
}
}
`)
}
func TestValidate_KnownDirectives_WithUnknownDirective(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.KnownDirectivesRule, `
{
dog @unknown(directive: "value") {
name
}
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Unknown directive "unknown".`, 3, 13),
})
}
func TestValidate_KnownDirectives_WithManyUnknownDirectives(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.KnownDirectivesRule, `
{
dog @unknown(directive: "value") {
name
}
human @unknown(directive: "value") {
name
pets @unknown(directive: "value") {
name
}
}
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Unknown directive "unknown".`, 3, 13),
testutil.RuleError(`Unknown directive "unknown".`, 6, 15),
testutil.RuleError(`Unknown directive "unknown".`, 8, 16),
})
}
func TestValidate_KnownDirectives_WithWellPlacedDirectives(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.KnownDirectivesRule, `
query Foo @onQuery {
name @include(if: true)
...Frag @include(if: true)
skippedField @skip(if: true)
...SkippedFrag @skip(if: true)
}
mutation Bar @onMutation {
someField
}
`)
}
func TestValidate_KnownDirectives_WithMisplacedDirectives(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.KnownDirectivesRule, `
query Foo @include(if: true) {
name @onQuery
...Frag @onQuery
}
mutation Bar @onQuery {
someField
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Directive "include" may not be used on QUERY.`, 2, 17),
testutil.RuleError(`Directive "onQuery" may not be used on FIELD.`, 3, 14),
testutil.RuleError(`Directive "onQuery" may not be used on FRAGMENT_SPREAD.`, 4, 17),
testutil.RuleError(`Directive "onQuery" may not be used on MUTATION.`, 7, 20),
})
}
func TestValidate_KnownDirectives_WithinSchemaLanguage_WithWellPlacedDirectives(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.KnownDirectivesRule, `
type MyObj implements MyInterface @onObject {
myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition
}
scalar MyScalar @onScalar
interface MyInterface @onInterface {
myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition
}
union MyUnion @onUnion = MyObj | Other
enum MyEnum @onEnum {
MY_VALUE @onEnumValue
}
input MyInput @onInputObject {
myField: Int @onInputFieldDefinition
}
schema @onSchema {
query: MyQuery
}
`)
}
func TestValidate_KnownDirectives_WithinSchemaLanguage_WithMisplacedDirectives(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.KnownDirectivesRule, `
type MyObj implements MyInterface @onInterface {
myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition
}
scalar MyScalar @onEnum
interface MyInterface @onObject {
myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition
}
union MyUnion @onEnumValue = MyObj | Other
enum MyEnum @onScalar {
MY_VALUE @onUnion
}
input MyInput @onEnum {
myField: Int @onArgumentDefinition
}
schema @onObject {
query: MyQuery
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Directive "onInterface" may not be used on OBJECT.`, 2, 43),
testutil.RuleError(`Directive "onInputFieldDefinition" may not be used on ARGUMENT_DEFINITION.`, 3, 30),
testutil.RuleError(`Directive "onInputFieldDefinition" may not be used on FIELD_DEFINITION.`, 3, 63),
testutil.RuleError(`Directive "onEnum" may not be used on SCALAR.`, 6, 25),
testutil.RuleError(`Directive "onObject" may not be used on INTERFACE.`, 8, 31),
testutil.RuleError(`Directive "onInputFieldDefinition" may not be used on ARGUMENT_DEFINITION.`, 9, 30),
testutil.RuleError(`Directive "onInputFieldDefinition" may not be used on FIELD_DEFINITION.`, 9, 63),
testutil.RuleError(`Directive "onEnumValue" may not be used on UNION.`, 12, 23),
testutil.RuleError(`Directive "onScalar" may not be used on ENUM.`, 14, 21),
testutil.RuleError(`Directive "onUnion" may not be used on ENUM_VALUE.`, 15, 20),
testutil.RuleError(`Directive "onEnum" may not be used on INPUT_OBJECT.`, 18, 23),
testutil.RuleError(`Directive "onArgumentDefinition" may not be used on INPUT_FIELD_DEFINITION.`, 19, 24),
testutil.RuleError(`Directive "onObject" may not be used on SCHEMA.`, 22, 16),
})
}