Skip to content

Commit e2939f7

Browse files
authored
Merge pull request #73 from brunnogrillo/fix-duplicated-fragments
Fragments validation
2 parents 9ef5085 + ad0b73a commit e2939f7

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

kgraphql/src/main/kotlin/com/apurebase/kgraphql/schema/structure/RequestInterpreter.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,16 @@ class RequestInterpreter(val schemaModel: SchemaModel) {
7272
OperationTypeNode.SUBSCRIPTION -> schemaModel.subscription ?: throw GraphQLError("Subscriptions are not supported on this schema")
7373
}
7474

75-
val fragmentDefinitions = test.filterIsInstance<FragmentDefinitionNode>().map { fragmentDef ->
75+
val fragmentDefinitionNode = test.filterIsInstance<FragmentDefinitionNode>()
76+
val fragmentDefinitions = fragmentDefinitionNode.map { fragmentDef ->
7677
val type = schemaModel.allTypesByName.getValue(fragmentDef.typeCondition.name.value)
78+
val name = fragmentDef.name!!.value
7779

78-
fragmentDef.name!!.value to (type to fragmentDef.selectionSet)
80+
if (fragmentDefinitionNode.count { it.name!!.value == name } > 1) {
81+
throw GraphQLError("There can be only one fragment named $name.", fragmentDef )
82+
}
83+
84+
name to (type to fragmentDef.selectionSet)
7985
}.toMap()
8086

8187
val ctx = InterpreterContext(fragmentDefinitions)

kgraphql/src/test/kotlin/com/apurebase/kgraphql/specification/language/FragmentsSpecificationTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,28 @@ class FragmentsSpecificationTest {
145145
""")
146146
} shouldThrow GraphQLError::class withMessage "Fragment spread circular references are not allowed"
147147
}
148+
149+
@Test
150+
fun `queries with duplicated fragments are denied`() {
151+
invoking {
152+
BaseTestSchema.execute("""
153+
{
154+
film {
155+
...film_title
156+
}
157+
}
158+
159+
fragment film_title on Film {
160+
title
161+
}
162+
163+
fragment film_title on Film {
164+
director {
165+
name
166+
age
167+
}
168+
}
169+
""")
170+
} shouldThrow GraphQLError::class withMessage "There can be only one fragment named film_title."
171+
}
148172
}

0 commit comments

Comments
 (0)