Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 1c62506

Browse files
committed
Fixed required arrays when parsing operation variables
1 parent 42fef4a commit 1c62506

File tree

5 files changed

+171
-17
lines changed

5 files changed

+171
-17
lines changed

src/main/kotlin/com/github/pgutkowski/kgraphql/request/RequestPreProcessing.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ fun tokenizeRequest(input : String) : List<String> {
1717
when(input[i]){
1818
in IGNORED_CHARACTERS -> { i++ }
1919
in OPERANDS -> {
20-
tokens.add(input[i].toString())
21-
i++
20+
if (input.length > i+1 && input.substring(i, i+2) == "]!") {
21+
tokens.add("]!")
22+
i += 2
23+
} else {
24+
tokens.add(input[i].toString())
25+
i++
26+
}
2227
}
2328
'\"' -> {
2429
val substring = input.substring(i + 1)
@@ -167,8 +172,8 @@ private fun parseOperationVariables(variablesTokens: List<String>): MutableList<
167172
}
168173
variableName == null -> variableName = variableToken
169174
variableType == null -> variableType = variableToken
170-
variableType.startsWith("[") && variableType == "]" -> variableType += variableToken
171-
variableType.startsWith("[") -> variableType += variableToken
175+
variableType.startsWith(("[")) && variableToken.startsWith("]") -> variableType += variableToken
176+
variableType.startsWith("[") && !variableType.contains("]") -> variableType += variableToken
172177
defaultTypeStarted && variableDefaultValue == null -> variableDefaultValue = variableToken
173178
else -> {
174179
//if variableName of variableType would be null, it would already be matched

src/test/kotlin/com/github/pgutkowski/kgraphql/TestClasses.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ abstract class Person(val name: String, val age: Int)
99

1010
class Director(name : String, age: Int, val favActors: List<Actor>) : Person(name, age)
1111

12+
class ActorInput(val name: String, val age: Int)
13+
class ActorCalculateAgeInput(val name: String, val ages: List<Int>)
14+
1215
class Actor(name : String, age: Int) : Person(name, age)
1316

1417
class Id(val literal: String, val numeric: Int)

src/test/kotlin/com/github/pgutkowski/kgraphql/integration/BaseSchemaTest.kt

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
package com.github.pgutkowski.kgraphql.integration
22

3-
import com.github.pgutkowski.kgraphql.Actor
4-
import com.github.pgutkowski.kgraphql.Director
5-
import com.github.pgutkowski.kgraphql.Film
6-
import com.github.pgutkowski.kgraphql.FilmType
7-
import com.github.pgutkowski.kgraphql.Id
8-
import com.github.pgutkowski.kgraphql.IdScalarSupport
9-
import com.github.pgutkowski.kgraphql.Person
10-
import com.github.pgutkowski.kgraphql.Scenario
11-
import com.github.pgutkowski.kgraphql.defaultSchema
12-
import com.github.pgutkowski.kgraphql.deserialize
13-
import com.github.pgutkowski.kgraphql.schema.DefaultSchema
14-
import com.github.pgutkowski.kgraphql.schema.dsl.SchemaBuilder
3+
import com.github.pgutkowski.kgraphql.*
154
import org.junit.After
165

176

@@ -184,6 +173,30 @@ abstract class BaseSchemaTest {
184173
actor
185174
}
186175
}
176+
mutation("createActorWithInput") {
177+
description = "create new actor"
178+
resolver { input: ActorInput ->
179+
val actor = Actor(input.name, input.age)
180+
createdActors.add(actor)
181+
actor
182+
}
183+
}
184+
mutation("createActorWithAges") {
185+
description = "create new actor"
186+
resolver { name: String, ages: List<Int> ->
187+
val actor = Actor(name, ages.reduce { sum, age -> sum + age })
188+
createdActors.add(actor)
189+
actor
190+
}
191+
}
192+
mutation("createActorWithAgesInput") {
193+
description = "create new actor"
194+
resolver { input: ActorCalculateAgeInput ->
195+
val actor = Actor(input.name, input.ages.reduce { sum, age -> sum + age })
196+
createdActors.add(actor)
197+
actor
198+
}
199+
}
187200
query("scenario") {
188201
resolver { -> Scenario(Id("GKalus", 234234), "Gamil Kalus", "Very long scenario") }
189202
}

src/test/kotlin/com/github/pgutkowski/kgraphql/request/RequestTokenizationTest.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,43 @@ class RequestTokenizationTest {
5151
)
5252
}
5353

54+
@Test
55+
fun `Tokenize required list argument`() {
56+
val d = "$"
57+
testTokenization(
58+
input = "mutation create(${d}agesName1: String!, ${d}ages: [String!]!){ createActorWithAges(name: ${d}agesName1, ages: ${d}ages1) { name, age } }",
59+
expected = listOf(
60+
"mutation",
61+
"create",
62+
"(",
63+
"${d}agesName1",
64+
":",
65+
"String!",
66+
"${d}ages",
67+
":",
68+
"[",
69+
"String!",
70+
"]!",
71+
")",
72+
"{",
73+
"createActorWithAges",
74+
"(",
75+
"name",
76+
":",
77+
"${d}agesName1",
78+
"ages",
79+
":",
80+
"${d}ages1",
81+
")",
82+
"{",
83+
"name",
84+
"age",
85+
"}",
86+
"}"
87+
)
88+
)
89+
}
90+
5491
@Test
5592
fun `tokenize input with quotes`(){
5693
testTokenization(

src/test/kotlin/com/github/pgutkowski/kgraphql/specification/language/VariablesSpecificationTest.kt

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,100 @@ class VariablesSpecificationTest : BaseSchemaTest() {
8989
)
9090
}
9191
}
92-
}
92+
93+
@Test
94+
fun `Advanced variables`() {
95+
val d = "$"
96+
val request = """
97+
mutation MultipleCreate(
98+
${d}name1: String!,
99+
${d}age1: Int!,
100+
${d}name2: String!,
101+
${d}age2: Int!,
102+
103+
${d}input1: ActorInput!,
104+
${d}input2: ActorInput!,
105+
106+
${d}agesName1: String!,
107+
${d}ages1: [Int!]!,
108+
${d}agesName2: String!,
109+
${d}ages2: [Int!]!,
110+
111+
${d}agesInput1: ActorCalculateAgeInput!,
112+
${d}agesInput2: ActorCalculateAgeInput!
113+
) {
114+
createFirst: createActor(name: ${d}name1, age: ${d}age1) { name, age }
115+
createSecond: createActor(name: ${d}name2, age: ${d}age2) { name, age }
116+
inputFirst: createActorWithInput(input: ${d}input1) { name, age }
117+
inputSecond: createActorWithInput(input: ${d}input2) { name, age }
118+
agesFirst: createActorWithAges(name: ${d}agesName1, ages: ${d}ages1) { name, age }
119+
agesSecond: createActorWithAges(name: ${d}agesName2, ages: ${d}ages2) { name, age }
120+
inputAgesFirst: createActorWithAgesInput(input: ${d}agesInput1) { name, age }
121+
inputAgesSecond: createActorWithAgesInput(input: ${d}agesInput2) { name, age }
122+
}
123+
""".trimEnd()
124+
val variables = """
125+
{
126+
"name1": "Jógvan",
127+
"age1": 1,
128+
"name2": "Paweł",
129+
"age2": 2,
130+
131+
"input1": {"name": "Olsen", "age": 3},
132+
"input2": {"name": "Gutkowski", "age": 4},
133+
134+
"agesName1": "Someone",
135+
"ages1": [10,50],
136+
"agesName2": "Some other",
137+
"ages2": [5, 10],
138+
139+
"agesInput1": {"name": "Jógvan Olsen", "ages": [3]},
140+
"agesInput2": {"name": "Paweł Gutkowski", "ages": [4,5,6,7]}
141+
}
142+
""".trimIndent()
143+
144+
val result = execute(request, variables)
145+
146+
147+
assertNoErrors(result)
148+
assertThat(result.extract("data/createFirst/name"), equalTo("Jógvan"))
149+
assertThat(result.extract("data/createSecond/age"), equalTo(2))
150+
assertThat(result.extract("data/inputFirst/name"), equalTo("Olsen"))
151+
assertThat(result.extract("data/inputSecond/age"), equalTo(4))
152+
assertThat(result.extract("data/agesFirst/name"), equalTo("Someone"))
153+
assertThat(result.extract("data/agesSecond/age"), equalTo(15))
154+
assertThat(result.extract("data/inputAgesFirst/name"), equalTo("Jógvan Olsen"))
155+
assertThat(result.extract("data/inputAgesSecond/age"), equalTo(22))
156+
}
157+
158+
@Test
159+
fun `required variable arrays`() {
160+
val d = "$"
161+
val request = """
162+
mutation MultipleCreate(
163+
${d}agesName1: String!,
164+
${d}ages1: [Int!]!,
165+
${d}agesName2: String!,
166+
${d}ages2: [Int!]!
167+
) {
168+
agesFirst: createActorWithAges(name: ${d}agesName1, ages: ${d}ages1) { name, age }
169+
agesSecond: createActorWithAges(name: ${d}agesName2, ages: ${d}ages2) { name, age }
170+
}
171+
""".trimIndent()
172+
val variables = """
173+
{
174+
"agesName1": "Someone",
175+
"ages1": [10,50],
176+
"agesName2": "Some other",
177+
"ages2": [5, 10]
178+
}
179+
""".trimIndent()
180+
181+
val result = execute(request, variables)
182+
183+
184+
assertNoErrors(result)
185+
assertThat(result.extract("data/agesFirst/name"), equalTo("Someone"))
186+
assertThat(result.extract("data/agesSecond/age"), equalTo(15))
187+
}
188+
}

0 commit comments

Comments
 (0)