Skip to content

Commit fd86181

Browse files
authored
Allow getting a field in the schema by FieldCoordinates (graphql-java#2490)
1 parent a75b54a commit fd86181

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎src/main/java/graphql/schema/FieldCoordinates.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public int hashCode() {
7878

7979
@Override
8080
public String toString() {
81-
return typeName + ':' + fieldName + '\'';
81+
return typeName + '.' + fieldName;
8282
}
8383

8484
/**

‎src/main/java/graphql/schema/GraphQLSchema.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,30 @@ public GraphQLObjectType getObjectType(String typeName) {
293293
return (GraphQLObjectType) graphQLType;
294294
}
295295

296+
public GraphQLFieldDefinition getFieldDefinition(FieldCoordinates fieldCoordinates) {
297+
String fieldName = fieldCoordinates.getFieldName();
298+
if (fieldCoordinates.isSystemCoordinates()) {
299+
if (fieldName.equals(this.getIntrospectionSchemaFieldDefinition().getName())) {
300+
return this.getIntrospectionSchemaFieldDefinition();
301+
}
302+
if (fieldName.equals(this.getIntrospectionTypeFieldDefinition().getName())) {
303+
return this.getIntrospectionTypeFieldDefinition();
304+
}
305+
if (fieldName.equals(this.getIntrospectionTypenameFieldDefinition().getName())) {
306+
return this.getIntrospectionTypenameFieldDefinition();
307+
}
308+
return Assert.assertShouldNeverHappen("The system field name %s is unknown", fieldName);
309+
}
310+
String typeName = fieldCoordinates.getTypeName();
311+
GraphQLType graphQLType = getType(typeName);
312+
if (graphQLType != null) {
313+
assertTrue(graphQLType instanceof GraphQLFieldsContainer,
314+
() -> String.format("You have asked for named type '%s' but its not GraphQLFieldsContainer but rather a '%s'", typeName, graphQLType.getClass().getName()));
315+
return ((GraphQLFieldsContainer) graphQLType).getFieldDefinition(fieldName);
316+
}
317+
return null;
318+
}
319+
296320
public Map<String, GraphQLNamedType> getTypeMap() {
297321
return typeMap;
298322
}

‎src/test/groovy/graphql/schema/GraphQLSchemaTest.groovy‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,56 @@ class GraphQLSchemaTest extends Specification {
272272
newDF !== nameDF
273273
newDF instanceof PropertyDataFetcher // defaulted in
274274
}
275+
276+
def "can get by field co-ordinate"() {
277+
when:
278+
def fieldDef = starWarsSchema.getFieldDefinition(FieldCoordinates.coordinates("QueryType", "hero"))
279+
280+
then:
281+
fieldDef.name == "hero"
282+
(fieldDef.type as GraphQLInterfaceType).getName() == "Character"
283+
284+
when:
285+
fieldDef = starWarsSchema.getFieldDefinition(FieldCoordinates.coordinates("X", "hero"))
286+
287+
then:
288+
fieldDef == null
289+
290+
when:
291+
fieldDef = starWarsSchema.getFieldDefinition(FieldCoordinates.coordinates("QueryType", "X"))
292+
293+
then:
294+
fieldDef == null
295+
296+
when:
297+
starWarsSchema.getFieldDefinition(FieldCoordinates.coordinates("Episode", "JEDI"))
298+
299+
then:
300+
thrown(AssertException)
301+
302+
when:
303+
fieldDef = starWarsSchema.getFieldDefinition(FieldCoordinates.systemCoordinates("__typename"))
304+
305+
then:
306+
fieldDef == starWarsSchema.getIntrospectionTypenameFieldDefinition()
307+
308+
when:
309+
fieldDef = starWarsSchema.getFieldDefinition(FieldCoordinates.systemCoordinates("__type"))
310+
311+
then:
312+
fieldDef == starWarsSchema.getIntrospectionTypeFieldDefinition()
313+
314+
when:
315+
fieldDef = starWarsSchema.getFieldDefinition(FieldCoordinates.systemCoordinates("__schema"))
316+
317+
then:
318+
fieldDef == starWarsSchema.getIntrospectionSchemaFieldDefinition()
319+
320+
when:
321+
starWarsSchema.getFieldDefinition(FieldCoordinates.systemCoordinates("__junk"))
322+
323+
then:
324+
thrown(AssertException)
325+
326+
}
275327
}

0 commit comments

Comments
 (0)