Skip to content

Prevent to add PageInfo definition if not needed #476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import graphql.language.*
class RelayConnectionFactory : TypeDefinitionFactory {

override fun create(existing: MutableList<Definition<*>>): List<Definition<*>> {
val connectionDirectives = findConnectionDirectives(existing)
if (connectionDirectives.isEmpty()) {
// do not add Relay definitions unless needed
return emptyList()
}

val definitions = mutableListOf<Definition<*>>()
val definitionsByName = existing.filterIsInstance<TypeDefinition<*>>()
.associateBy { it.name }
.toMutableMap()

findConnectionDirectives(existing)
connectionDirectives
.flatMap { createDefinitions(it) }
.forEach {
if (!definitionsByName.containsKey(it.name)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package graphql.kickstart.tools.relay

import graphql.language.Definition
import org.junit.Assert.assertEquals
import org.junit.Test

class RelayConnectionFactoryTest {

@Test
fun `should not add new definition when no @connection directive`() {
// setup
val factory = RelayConnectionFactory()
val existing = mutableListOf<Definition<*>>()

val newDefinitions = factory.create(existing)

// expect
assertEquals(newDefinitions.size, 0)
}
}