Skip to content

Commit 18bf953

Browse files
committed
Test for changing schema
1 parent 33cea4a commit 18bf953

1 file changed

Lines changed: 44 additions & 3 deletions

File tree

src/test/groovy/graphql/schema/visitor/GraphQLSchemaVisitorTest.groovy

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ import graphql.schema.GraphQLInputObjectField
1212
import graphql.schema.GraphQLInputObjectType
1313
import graphql.schema.GraphQLInterfaceType
1414
import graphql.schema.GraphQLObjectType
15+
import graphql.schema.GraphQLOutputType
1516
import graphql.schema.GraphQLScalarType
17+
import graphql.schema.GraphQLSchema
1618
import graphql.schema.GraphQLSchemaElement
1719
import graphql.schema.GraphQLUnionType
20+
import graphql.schema.SchemaTransformer
1821
import graphql.schema.SchemaTraverser
1922
import graphql.util.TraversalControl
2023
import spock.lang.Specification
@@ -30,7 +33,7 @@ class GraphQLSchemaVisitorTest extends Specification {
3033

3134
@Override
3235
TraversalControl visitSchemaElement(GraphQLSchemaElement schemaElement, SchemaElementVisitorEnvironment environment) {
33-
schema = environment.getSchema()
36+
this.schema = environment.getSchema()
3437
return environment.ok()
3538
}
3639

@@ -161,11 +164,11 @@ class GraphQLSchemaVisitorTest extends Specification {
161164
def visitor = new CapturingSchemaVisitor()
162165

163166
when:
164-
new SchemaTraverser().depthFirstFullSchema(visitor.toTypeVisitor(), schema)
167+
new SchemaTraverser().depthFirstFullSchema(visitor.toTypeVisitor(), this.schema)
165168

166169
then:
167170

168-
visitor.schema == schema
171+
visitor.schema == this.schema
169172
visitor.types["Query"] instanceof GraphQLObjectType
170173

171174
visitor.leafs["directive"] instanceof GraphQLDirective
@@ -194,4 +197,42 @@ class GraphQLSchemaVisitorTest extends Specification {
194197
visitor.types["UnionTypeA"] instanceof GraphQLUnionType
195198

196199
}
200+
201+
def "can transform schemas via this pattern"() {
202+
def sdl = """
203+
type Query {
204+
f : xfoo
205+
}
206+
207+
type xfoo {
208+
bar : xbar
209+
}
210+
211+
type xbar {
212+
baz : String
213+
}
214+
215+
"""
216+
217+
def schema = TestUtil.schema(sdl)
218+
219+
def schemaVisitor = new GraphQLSchemaVisitor() {
220+
221+
@Override
222+
TraversalControl visitObjectType(GraphQLObjectType objectType, GraphQLSchemaVisitor.ObjectVisitorEnvironment environment) {
223+
if (objectType.name.startsWith("x")) {
224+
def newName = objectType.name.replaceFirst("x", "").capitalize()
225+
def newType = objectType.transform { it.name(newName) }
226+
return environment.changeNode(newType)
227+
}
228+
return environment.ok();
229+
}
230+
}
231+
232+
when:
233+
def newSchema = new SchemaTransformer().transform(schema, schemaVisitor.toTypeVisitor())
234+
then:
235+
newSchema.getType("Foo") instanceof GraphQLObjectType
236+
newSchema.getType("Bar") instanceof GraphQLObjectType
237+
}
197238
}

0 commit comments

Comments
 (0)