Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/graphql/schema/FieldCoordinates.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public int hashCode() {

@Override
public String toString() {
return typeName + ':' + fieldName + '\'';
return typeName + '.' + fieldName;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/graphql/schema/GraphQLSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,30 @@ public GraphQLObjectType getObjectType(String typeName) {
return (GraphQLObjectType) graphQLType;
}

public GraphQLFieldDefinition getFieldDefinition(FieldCoordinates fieldCoordinates) {
String fieldName = fieldCoordinates.getFieldName();
if (fieldCoordinates.isSystemCoordinates()) {
if (fieldName.equals(this.getIntrospectionSchemaFieldDefinition().getName())) {
return this.getIntrospectionSchemaFieldDefinition();
}
if (fieldName.equals(this.getIntrospectionTypeFieldDefinition().getName())) {
return this.getIntrospectionTypeFieldDefinition();
}
if (fieldName.equals(this.getIntrospectionTypenameFieldDefinition().getName())) {
return this.getIntrospectionTypenameFieldDefinition();
}
return Assert.assertShouldNeverHappen("The system field name %s is unknown", fieldName);
}
String typeName = fieldCoordinates.getTypeName();
GraphQLType graphQLType = getType(typeName);
if (graphQLType != null) {
assertTrue(graphQLType instanceof GraphQLFieldsContainer,
() -> String.format("You have asked for named type '%s' but its not GraphQLFieldsContainer but rather a '%s'", typeName, graphQLType.getClass().getName()));
return ((GraphQLFieldsContainer) graphQLType).getFieldDefinition(fieldName);
}
return null;
}

public Map<String, GraphQLNamedType> getTypeMap() {
return typeMap;
}
Expand Down
52 changes: 52 additions & 0 deletions src/test/groovy/graphql/schema/GraphQLSchemaTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,56 @@ class GraphQLSchemaTest extends Specification {
newDF !== nameDF
newDF instanceof PropertyDataFetcher // defaulted in
}

def "can get by field co-ordinate"() {
when:
def fieldDef = starWarsSchema.getFieldDefinition(FieldCoordinates.coordinates("QueryType", "hero"))

then:
fieldDef.name == "hero"
(fieldDef.type as GraphQLInterfaceType).getName() == "Character"

when:
fieldDef = starWarsSchema.getFieldDefinition(FieldCoordinates.coordinates("X", "hero"))

then:
fieldDef == null

when:
fieldDef = starWarsSchema.getFieldDefinition(FieldCoordinates.coordinates("QueryType", "X"))

then:
fieldDef == null

when:
starWarsSchema.getFieldDefinition(FieldCoordinates.coordinates("Episode", "JEDI"))

then:
thrown(AssertException)

when:
fieldDef = starWarsSchema.getFieldDefinition(FieldCoordinates.systemCoordinates("__typename"))

then:
fieldDef == starWarsSchema.getIntrospectionTypenameFieldDefinition()

when:
fieldDef = starWarsSchema.getFieldDefinition(FieldCoordinates.systemCoordinates("__type"))

then:
fieldDef == starWarsSchema.getIntrospectionTypeFieldDefinition()

when:
fieldDef = starWarsSchema.getFieldDefinition(FieldCoordinates.systemCoordinates("__schema"))

then:
fieldDef == starWarsSchema.getIntrospectionSchemaFieldDefinition()

when:
starWarsSchema.getFieldDefinition(FieldCoordinates.systemCoordinates("__junk"))

then:
thrown(AssertException)

}
}