Skip to content

Commit aaa0083

Browse files
committed
Made the field definition lookup more optimised
1 parent 53408fd commit aaa0083

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

‎src/main/java/graphql/execution/ExecutionStrategy.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ protected GraphQLFieldDefinition getFieldDef(ExecutionContext executionContext,
923923
* @return a {@link GraphQLFieldDefinition}
924924
*/
925925
protected GraphQLFieldDefinition getFieldDef(GraphQLSchema schema, GraphQLObjectType parentType, Field field) {
926-
return Introspection.getFieldDef(schema, parentType, field.getName());
926+
return Introspection.getFieldDefinition(schema, parentType, field.getName());
927927
}
928928

929929
/**

‎src/main/java/graphql/introspection/Introspection.java‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,43 @@ public static boolean isIntrospectionTypes(GraphQLNamedType type) {
702702
*/
703703
public static GraphQLFieldDefinition getFieldDef(GraphQLSchema schema, GraphQLCompositeType parentType, String fieldName) {
704704

705+
GraphQLFieldDefinition fieldDefinition = getSystemFieldDef(schema, parentType, fieldName);
706+
if (fieldDefinition != null) {
707+
return fieldDefinition;
708+
}
709+
710+
assertTrue(parentType instanceof GraphQLFieldsContainer, () -> String.format("should not happen : parent type must be an object or interface %s", parentType));
711+
GraphQLFieldsContainer fieldsContainer = (GraphQLFieldsContainer) parentType;
712+
fieldDefinition = schema.getCodeRegistry().getFieldVisibility().getFieldDefinition(fieldsContainer, fieldName);
713+
assertTrue(fieldDefinition != null, () -> String.format("Unknown field '%s' for type %s", fieldName, fieldsContainer.getName()));
714+
return fieldDefinition;
715+
}
716+
717+
/**
718+
* This will look up a field definition by name, and understand that fields like __typename and __schema are special
719+
* and take precedence in field resolution
720+
*
721+
* @param schema the schema to use
722+
* @param parentType the type of the parent {@link GraphQLFieldsContainer}
723+
* @param fieldName the field to look up
724+
*
725+
* @return a field definition otherwise throws an assertion exception if it's null
726+
*/
727+
public static GraphQLFieldDefinition getFieldDefinition(GraphQLSchema schema, GraphQLFieldsContainer parentType, String fieldName) {
728+
// this method is optimized to look up the most common case first (type for field) and hence suits the hot path of the execution engine
729+
// and as a small benefit does not allocate any assertions unless it completely failed
730+
GraphQLFieldDefinition fieldDefinition = schema.getCodeRegistry().getFieldVisibility().getFieldDefinition(parentType, fieldName);
731+
if (fieldDefinition == null) {
732+
// we look up system fields second because they are less likely to be the field in question
733+
fieldDefinition = getSystemFieldDef(schema, parentType, fieldName);
734+
if (fieldDefinition == null) {
735+
Assert.assertShouldNeverHappen(String.format("Unknown field '%s' for type %s", fieldName, parentType.getName()));
736+
}
737+
}
738+
return fieldDefinition;
739+
}
740+
741+
private static GraphQLFieldDefinition getSystemFieldDef(GraphQLSchema schema, GraphQLCompositeType parentType, String fieldName) {
705742
if (schema.getQueryType() == parentType) {
706743
if (fieldName.equals(schema.getIntrospectionSchemaFieldDefinition().getName())) {
707744
return schema.getIntrospectionSchemaFieldDefinition();
@@ -713,11 +750,6 @@ public static GraphQLFieldDefinition getFieldDef(GraphQLSchema schema, GraphQLCo
713750
if (fieldName.equals(schema.getIntrospectionTypenameFieldDefinition().getName())) {
714751
return schema.getIntrospectionTypenameFieldDefinition();
715752
}
716-
717-
assertTrue(parentType instanceof GraphQLFieldsContainer, () -> String.format("should not happen : parent type must be an object or interface %s", parentType));
718-
GraphQLFieldsContainer fieldsContainer = (GraphQLFieldsContainer) parentType;
719-
GraphQLFieldDefinition fieldDefinition = schema.getCodeRegistry().getFieldVisibility().getFieldDefinition(fieldsContainer, fieldName);
720-
assertTrue(fieldDefinition != null, () -> String.format("Unknown field '%s' for type %s", fieldName, fieldsContainer.getName()));
721-
return fieldDefinition;
753+
return null;
722754
}
723-
}
755+
}

0 commit comments

Comments
 (0)