11package com.coxautodev.graphql.tools.relay
22
33import com.coxautodev.graphql.tools.TypeDefinitionFactory
4+ import graphql.language.Argument
5+ import graphql.language.Comment
46import graphql.language.Definition
57import graphql.language.Directive
68import graphql.language.FieldDefinition
79import graphql.language.ListType
810import graphql.language.NonNullType
911import graphql.language.ObjectTypeDefinition
12+ import graphql.language.SourceLocation
1013import graphql.language.StringValue
1114import graphql.language.TypeDefinition
1215import 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}
0 commit comments