Skip to content

Commit 44c9de4

Browse files
committed
Use the type name for the connection as defined in the schema
1 parent eb1e9ed commit 44c9de4

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/main/kotlin/com/coxautodev/graphql/tools/relay/RelayConnectionFactory.kt

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.coxautodev.graphql.tools.relay
22

33
import com.coxautodev.graphql.tools.TypeDefinitionFactory
4+
import graphql.language.Argument
5+
import graphql.language.Comment
46
import graphql.language.Definition
57
import graphql.language.Directive
68
import graphql.language.FieldDefinition
79
import graphql.language.ListType
810
import graphql.language.NonNullType
911
import graphql.language.ObjectTypeDefinition
12+
import graphql.language.SourceLocation
1013
import graphql.language.StringValue
1114
import graphql.language.TypeDefinition
1215
import graphql.language.TypeName
@@ -35,32 +38,32 @@ class RelayConnectionFactory : TypeDefinitionFactory {
3538
return definitions
3639
}
3740

38-
private fun findConnectionDirectives(definitions: List<Definition<*>>): List<Directive> {
41+
private fun findConnectionDirectives(definitions: List<Definition<*>>): List<DirectiveWithField> {
3942
return definitions.filterIsInstance<ObjectTypeDefinition>()
4043
.flatMap { it.fieldDefinitions }
41-
.flatMap { it.directives }
44+
.flatMap { it.directivesWithField() }
4245
.filter { it.name == "connection" }
4346
}
4447

45-
private fun createDefinitions(directive: Directive): List<ObjectTypeDefinition> {
48+
private fun createDefinitions(directive: DirectiveWithField): List<ObjectTypeDefinition> {
4649
val definitions = mutableListOf<ObjectTypeDefinition>()
47-
definitions.add(createEdgeDefinition(directive.forTypeName()))
48-
definitions.add(createConnectionDefinition(directive.forTypeName()))
50+
definitions.add(createConnectionDefinition(directive.getTypeName()))
51+
definitions.add(createEdgeDefinition(directive.getTypeName(), directive.forTypeName()))
4952
return definitions.toList()
5053
}
5154

5255
private fun createConnectionDefinition(type: String): ObjectTypeDefinition =
5356
ObjectTypeDefinition.newObjectTypeDefinition()
54-
.name(type + "Connection")
57+
.name(type)
5558
.fieldDefinition(FieldDefinition("edges", ListType(TypeName(type + "Edge"))))
5659
.fieldDefinition(FieldDefinition("pageInfo", TypeName("PageInfo")))
5760
.build()
5861

59-
private fun createEdgeDefinition(type: String): ObjectTypeDefinition =
62+
private fun createEdgeDefinition(connectionType: String, nodeType: String): ObjectTypeDefinition =
6063
ObjectTypeDefinition.newObjectTypeDefinition()
61-
.name(type + "Edge")
64+
.name(connectionType + "Edge")
6265
.fieldDefinition(FieldDefinition("cursor", TypeName("String")))
63-
.fieldDefinition(FieldDefinition("node", TypeName(type)))
66+
.fieldDefinition(FieldDefinition("node", TypeName(nodeType)))
6467
.build()
6568

6669
private fun createPageInfo(): ObjectTypeDefinition =
@@ -74,4 +77,16 @@ class RelayConnectionFactory : TypeDefinitionFactory {
7477
return (this.getArgument("for").value as StringValue).value
7578
}
7679

80+
private fun Directive.withField(field: FieldDefinition): DirectiveWithField {
81+
return DirectiveWithField(field, this.name, this.arguments, this.sourceLocation, this.comments)
82+
}
83+
84+
private fun FieldDefinition.directivesWithField(): List<DirectiveWithField> {
85+
return this.directives.map { it.withField(this) }
86+
}
87+
88+
class DirectiveWithField(val field: FieldDefinition, name: String, arguments: List<Argument>, sourceLocation: SourceLocation, comments: List<Comment>) : Directive(name, arguments, sourceLocation, comments) {
89+
fun getTypeName() = (field.type as TypeName).name
90+
}
91+
7792
}

src/test/resources/RelayConnection.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
type Query {
2-
users(first: Int, after: String): UserConnection @connection(for: "User")
2+
users(first: Int, after: String): UserRelayConnection @connection(for: "User")
33
}
44

55
type User {

0 commit comments

Comments
 (0)