2121import org .osgi .service .component .annotations .Component ;
2222import org .osgi .service .component .annotations .Reference ;
2323
24- import javax .validation .constraints .NotNull ;
2524import java .lang .reflect .*;
2625import java .util .*;
2726import java .util .function .Function ;
@@ -470,12 +469,12 @@ protected GraphQLFieldDefinition getField(Field field) throws GraphQLAnnotations
470469 typeFunction = newInstance (annotation .value ());
471470 }
472471
473- GraphQLOutputType type = (GraphQLOutputType ) typeFunction .buildType (field .getType (), field .getAnnotatedType ());
472+ GraphQLOutputType outputType = (GraphQLOutputType ) typeFunction .buildType (field .getType (), field .getAnnotatedType ());
474473
475- GraphQLOutputType outputType = field . getAnnotation ( NotNull . class ) == null ? type : new GraphQLNonNull ( type );
476-
477- boolean isConnection = isConnection (field , field . getType (), type );
478- outputType = getGraphQLConnection ( isConnection , field , type , outputType , builder );
474+ boolean isConnection = isConnection ( field , outputType );
475+ if ( isConnection ) {
476+ outputType = getGraphQLConnection (field , outputType , builder );
477+ }
479478
480479 builder .type (outputType );
481480
@@ -575,24 +574,33 @@ private boolean checkIfPrefixGetterExists(Class c, String prefix, String propert
575574 return true ;
576575 }
577576
578- private GraphQLOutputType getGraphQLConnection (boolean isConnection , AccessibleObject field , GraphQLOutputType type , GraphQLOutputType outputType , GraphQLFieldDefinition .Builder builder ) {
579- if (isConnection ) {
580- if (type instanceof GraphQLList ) {
581- graphql .schema .GraphQLType wrappedType = ((GraphQLList ) type ).getWrappedType ();
582- assert wrappedType instanceof GraphQLObjectType ;
583- String annValue = field .getAnnotation (GraphQLConnection .class ).name ();
584- String connectionName = annValue .isEmpty () ? wrappedType .getName () : annValue ;
585- GraphQLObjectType edgeType = relay .edgeType (connectionName , (GraphQLOutputType ) wrappedType , null , Collections .<GraphQLFieldDefinition >emptyList ());
586- outputType = relay .connectionType (connectionName , edgeType , Collections .emptyList ());
587- builder .argument (relay .getConnectionFieldArguments ());
588- }
577+ private GraphQLOutputType getGraphQLConnection (AccessibleObject field , GraphQLOutputType type , GraphQLFieldDefinition .Builder builder ) {
578+ if (type instanceof GraphQLNonNull ) {
579+ type = (GraphQLOutputType ) ((GraphQLNonNull ) type ).getWrappedType ();
580+ return new GraphQLNonNull (internalGetGraphQLConnection (field , type , builder ));
581+ } else {
582+ return internalGetGraphQLConnection (field , type , builder );
589583 }
590- return outputType ;
591584 }
592585
593- private boolean isConnection (AccessibleObject obj , Class <?> klass , GraphQLOutputType type ) {
586+ private GraphQLOutputType internalGetGraphQLConnection (AccessibleObject field , GraphQLOutputType type , GraphQLFieldDefinition .Builder builder ) {
587+ graphql .schema .GraphQLType wrappedType = ((GraphQLList ) type ).getWrappedType ();
588+ assert wrappedType instanceof GraphQLObjectType ;
589+ String connectionName = field .getAnnotation (GraphQLConnection .class ).name ();
590+ connectionName = connectionName .isEmpty () ? wrappedType .getName () : connectionName ;
591+ GraphQLObjectType edgeType = relay .edgeType (connectionName , (GraphQLOutputType ) wrappedType , null , Collections .<GraphQLFieldDefinition >emptyList ());
592+ type = relay .connectionType (connectionName , edgeType , Collections .emptyList ());
593+ builder .argument (relay .getConnectionFieldArguments ());
594+ return type ;
595+ }
596+
597+ private boolean isConnection (AccessibleObject obj , GraphQLOutputType type ) {
598+ if (type instanceof GraphQLNonNull ) {
599+ type = (GraphQLOutputType ) ((GraphQLNonNull ) type ).getWrappedType ();
600+ }
601+ final GraphQLOutputType actualType = type ;
594602 return obj .isAnnotationPresent (GraphQLConnection .class ) &&
595- type instanceof GraphQLList && TYPES_FOR_CONNECTION .stream ().anyMatch (aClass -> aClass .isInstance (((GraphQLList ) type ).getWrappedType ()));
603+ actualType instanceof GraphQLList && TYPES_FOR_CONNECTION .stream ().anyMatch (aClass -> aClass .isInstance (((GraphQLList ) actualType ).getWrappedType ()));
596604 }
597605
598606 protected GraphQLFieldDefinition getField (Method method ) throws GraphQLAnnotationsException {
@@ -618,11 +626,12 @@ protected GraphQLFieldDefinition getField(Method method) throws GraphQLAnnotatio
618626 outputTypeFunction = typeFunction ;
619627 }
620628
621- GraphQLOutputType type = (GraphQLOutputType ) outputTypeFunction .buildType (method .getReturnType (), annotatedReturnType );
622- GraphQLOutputType outputType = method .getAnnotation (NotNull .class ) == null ? type : new GraphQLNonNull (type );
629+ GraphQLOutputType outputType = (GraphQLOutputType ) outputTypeFunction .buildType (method .getReturnType (), annotatedReturnType );
623630
624- boolean isConnection = isConnection (method , method .getReturnType (), type );
625- outputType = getGraphQLConnection (isConnection , method , type , outputType , builder );
631+ boolean isConnection = isConnection (method , outputType );
632+ if (isConnection ) {
633+ outputType = getGraphQLConnection (method , outputType , builder );
634+ }
626635
627636 builder .type (outputType );
628637
@@ -632,7 +641,7 @@ protected GraphQLFieldDefinition getField(Method method) throws GraphQLAnnotatio
632641 filter (p -> !DataFetchingEnvironment .class .isAssignableFrom (p .getType ())).
633642 map (parameter -> {
634643 Class <?> t = parameter .getType ();
635- graphql .schema .GraphQLType graphQLType = getInputObject (finalTypeFunction .buildType (t , parameter .getAnnotatedType ()), DEFAULT_INPUT_PREFIX );
644+ graphql .schema .GraphQLInputType graphQLType = getInputObject (finalTypeFunction .buildType (t , parameter .getAnnotatedType ()), DEFAULT_INPUT_PREFIX );
636645 return getArgument (parameter , graphQLType );
637646 }).collect (Collectors .toList ());
638647
@@ -743,10 +752,9 @@ public static GraphQLInputObjectType inputObject(GraphQLObjectType graphQLType,
743752 return (GraphQLInputObjectType ) getInstance ().getInputObject (graphQLType , newNamePrefix );
744753 }
745754
746- protected GraphQLArgument getArgument (Parameter parameter , graphql .schema .GraphQLType t ) throws
755+ protected GraphQLArgument getArgument (Parameter parameter , graphql .schema .GraphQLInputType t ) throws
747756 GraphQLAnnotationsException {
748- GraphQLArgument .Builder builder = newArgument ();
749- builder .type (parameter .getAnnotation (NotNull .class ) == null ? (GraphQLInputType ) t : new graphql .schema .GraphQLNonNull (t ));
757+ GraphQLArgument .Builder builder = newArgument ().type (t );
750758 GraphQLDescription description = parameter .getAnnotation (GraphQLDescription .class );
751759 if (description != null ) {
752760 builder .description (description .value ());
@@ -837,8 +845,12 @@ public Object get(DataFetchingEnvironment environment) {
837845 DataFetchingEnvironment env = new DataFetchingEnvironmentImpl (environment .getSource (), arguments , environment .getContext (),
838846 environment .getFields (), environment .getFieldType (), environment .getParentType (), environment .getGraphQLSchema (),
839847 environment .getFragmentsByName (), environment .getExecutionId (), environment .getSelectionSet ());
840- Connection conn = constructNewInstance (constructor , actualDataFetcher .get (env ));
841- return conn .get (environment );
848+ Object data = actualDataFetcher .get (env );
849+ if (data != null ) {
850+ Connection conn = constructNewInstance (constructor , data );
851+ return conn .get (environment );
852+ }
853+ return null ;
842854 }
843855 }
844856
0 commit comments