Skip to content

Commit f210154

Browse files
authored
Adds the ability to rename via the TypeInfo object (graphql-java#1538)
1 parent 400d008 commit f210154

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

src/main/java/graphql/schema/idl/TypeInfo.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,31 @@ public boolean isPlain() {
6868
return !isList() && !isNonNull();
6969
}
7070

71+
/**
72+
* This will rename the type with the specified new name but will preserve the wrapping that was present
73+
*
74+
* @param newName the new name of the type
75+
*
76+
* @return a new type info rebuilt with the new name
77+
*/
78+
public TypeInfo renameAs(String newName) {
79+
80+
Type out = TypeName.newTypeName(newName).build();
81+
82+
Stack<Class<?>> wrappingStack = new Stack<>();
83+
wrappingStack.addAll(this.decoration);
84+
while (!wrappingStack.isEmpty()) {
85+
Class<?> clazz = wrappingStack.pop();
86+
if (clazz.equals(NonNullType.class)) {
87+
out = NonNullType.newNonNullType(out).build();
88+
}
89+
if (clazz.equals(ListType.class)) {
90+
out = ListType.newListType(out).build();
91+
}
92+
}
93+
return typeInfo(out);
94+
}
95+
7196
/**
7297
* This will decorate a graphql type with the original hierarchy of non null and list'ness
7398
* it originally contained in its definition type

src/test/groovy/graphql/TestUtil.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import graphql.execution.MergedSelectionSet
55
import graphql.introspection.Introspection.DirectiveLocation
66
import graphql.language.Document
77
import graphql.language.Field
8+
import graphql.language.ObjectTypeDefinition
89
import graphql.language.ScalarTypeDefinition
10+
import graphql.language.Type
911
import graphql.parser.Parser
1012
import graphql.schema.Coercing
1113
import graphql.schema.DataFetcher
@@ -209,6 +211,22 @@ class TestUtil {
209211
new Parser().parseDocument(query)
210212
}
211213

214+
static Type parseType(String typeAst) {
215+
String docStr = """
216+
type X {
217+
field : $typeAst
218+
}
219+
"""
220+
try {
221+
def document = toDocument(docStr)
222+
ObjectTypeDefinition objTypeDef = document.getDefinitionsOfType(ObjectTypeDefinition.class)[0]
223+
return objTypeDef.fieldDefinitions[0].getType()
224+
} catch (Exception ignored) {
225+
assert false, "Invalid type AST string : $typeAst"
226+
return null
227+
}
228+
}
229+
212230
static Document toDocument(String query) {
213231
parseQuery(query)
214232
}

src/test/groovy/graphql/schema/idl/TypeInfoTest.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package graphql.schema.idl
22

3+
import graphql.TestUtil
4+
import graphql.language.AstPrinter
35
import graphql.language.ListType
46
import graphql.language.NonNullType
57
import graphql.language.Type
@@ -9,6 +11,7 @@ import graphql.schema.GraphQLNonNull
911
import graphql.schema.GraphQLObjectType
1012
import graphql.schema.GraphQLType
1113
import spock.lang.Specification
14+
import spock.lang.Unroll
1215

1316
class TypeInfoTest extends Specification {
1417

@@ -112,4 +115,25 @@ class TypeInfoTest extends Specification {
112115
assertNotEqualsAndHashCode(new NonNullType(new ListType(new TypeName("A"))), new ListType(new TypeName("A")))
113116
assertNotEqualsAndHashCode(new NonNullType(new ListType(new TypeName("A"))), new NonNullType(new ListType(new TypeName("B"))))
114117
}
118+
119+
120+
@Unroll
121+
def "test rename works as expected"() {
122+
123+
expect:
124+
Type actualType = TestUtil.parseType(actual)
125+
def typeInfo = TypeInfo.typeInfo(actualType)
126+
TypeInfo newTypeInfo = typeInfo.renameAs("newName")
127+
def printed = AstPrinter.printAst(newTypeInfo.getRawType())
128+
printed == expected
129+
130+
where:
131+
actual | expected
132+
"named" | "newName"
133+
"named!" | "newName!"
134+
"[named]" | "[newName]"
135+
"[named!]" | "[newName!]"
136+
"[named!]!" | "[newName!]!"
137+
"[[named!]!]" | "[[newName!]!]"
138+
}
115139
}

0 commit comments

Comments
 (0)