1717
1818import graphql .annotations .GraphQLFieldDefinitionWrapper ;
1919import graphql .annotations .annotationTypes .GraphQLRelayMutation ;
20- import graphql .annotations .annotationTypes .GraphQLType ;
2120import graphql .annotations .connection .GraphQLConnection ;
2221import graphql .annotations .processor .ProcessingElementsContainer ;
2322import graphql .annotations .processor .exceptions .CannotCastMemberException ;
4746import java .util .List ;
4847import java .util .Map ;
4948import java .util .stream .Collectors ;
50-
49+ import graphql . schema . GraphQLType ;
5150import static graphql .annotations .processor .util .ObjectUtil .getAllFields ;
5251import static graphql .annotations .processor .util .ReflectionKit .newInstance ;
5352import static graphql .schema .GraphQLFieldDefinition .newFieldDefinition ;
@@ -59,23 +58,21 @@ public class GraphQLFieldRetriever {
5958 private GraphQLObjectInfoRetriever graphQLObjectInfoRetriever ;
6059 private BreadthFirstSearch breadthFirstSearch ;
6160 private ParentalSearch parentalSearch ;
62- private GraphQLInputObjectRetriever graphQLInputObjectRetriever ;
6361 private DataFetcherConstructor dataFetcherConstructor ;
6462
6563 public GraphQLFieldRetriever (GraphQLObjectInfoRetriever graphQLObjectInfoRetriever , BreadthFirstSearch breadthFirstSearch , ParentalSearch parentalSearch ,
66- GraphQLInputObjectRetriever graphQLInputObjectRetriever , DataFetcherConstructor dataFetcherConstructor ) {
64+ DataFetcherConstructor dataFetcherConstructor ) {
6765 this .graphQLObjectInfoRetriever = graphQLObjectInfoRetriever ;
6866 this .breadthFirstSearch = breadthFirstSearch ;
6967 this .parentalSearch = parentalSearch ;
70- this .graphQLInputObjectRetriever = graphQLInputObjectRetriever ;
7168 this .dataFetcherConstructor = dataFetcherConstructor ;
7269 }
7370
7471 public GraphQLFieldRetriever () {
75- this (new GraphQLObjectInfoRetriever (), new BreadthFirstSearch (new GraphQLObjectInfoRetriever ()), new ParentalSearch (new GraphQLObjectInfoRetriever ()), new GraphQLInputObjectRetriever (), new DataFetcherConstructor ());
72+ this (new GraphQLObjectInfoRetriever (), new BreadthFirstSearch (new GraphQLObjectInfoRetriever ()), new ParentalSearch (new GraphQLObjectInfoRetriever ()), new DataFetcherConstructor ());
7673 }
7774
78- public List <GraphQLFieldDefinition > getExtensionFields (Class <?> object , List <String > fieldsDefined , ProcessingElementsContainer container ) throws CannotCastMemberException {
75+ public List <GraphQLFieldDefinition > getExtensionFields (Class <?> object , List <String > definedFields , ProcessingElementsContainer container ) throws CannotCastMemberException {
7976 List <GraphQLFieldDefinition > fields = new ArrayList <>();
8077 if (container .getExtensionsTypeRegistry ().containsKey (object )) {
8178 for (Class <?> aClass : container .getExtensionsTypeRegistry ().get (object )) {
@@ -84,15 +81,15 @@ public List<GraphQLFieldDefinition> getExtensionFields(Class<?> object, List<Str
8481 continue ;
8582 }
8683 if (breadthFirstSearch .isFound (method )) {
87- addExtensionField (getField (method , container ), fields , fieldsDefined );
84+ addExtensionField (getField (method , container ), fields , definedFields );
8885 }
8986 }
9087 for (Field field : getAllFields (aClass ).values ()) {
9188 if (Modifier .isStatic (field .getModifiers ())) {
9289 continue ;
9390 }
9491 if (parentalSearch .isFound (field )) {
95- addExtensionField (getField (field , container ), fields , fieldsDefined );
92+ addExtensionField (getField (field , container ), fields , definedFields );
9693 }
9794 }
9895 }
@@ -104,28 +101,62 @@ public GraphQLFieldDefinition getField(Method method, ProcessingElementsContaine
104101 GraphQLFieldDefinition .Builder builder = newFieldDefinition ();
105102 TypeFunction typeFunction = getTypeFunction (method , container );
106103 builder .name (new MethodNameBuilder (method ).build ());
107- GraphQLOutputType outputType = new MethodTypeBuilder (method , typeFunction , container ).build ();
104+ GraphQLOutputType outputType = ( GraphQLOutputType ) new MethodTypeBuilder (method , typeFunction , container , false ).build ();
108105
109106 boolean isConnection = ConnectionUtil .isConnection (method , outputType );
110107 if (isConnection ) {
111108 outputType = getGraphQLConnection (method , outputType , container .getRelay (), container .getTypeRegistry ());
112109 }
113110 builder .type (outputType );
114111 handleConnectionArgument (container , builder , isConnection );
115- List <GraphQLArgument > args = new ArgumentBuilder (method , typeFunction , graphQLInputObjectRetriever , builder , container , outputType ).build ();
112+ List <GraphQLArgument > args = new ArgumentBuilder (method , typeFunction , builder , container , outputType ).build ();
116113 GraphQLFieldDefinition relayFieldDefinition = handleRelayArguments (method , container , builder , outputType , args );
117114 builder .description (new DescriptionBuilder (method ).build ())
118115 .deprecate (new DeprecateBuilder (method ).build ())
119116 .dataFetcher (new MethodDataFetcherBuilder (method , outputType , typeFunction , container , relayFieldDefinition , args , dataFetcherConstructor , isConnection ).build ());
120117 return new GraphQLFieldDefinitionWrapper (builder .build ());
121118 }
122119
120+ public GraphQLFieldDefinition getField (Field field , ProcessingElementsContainer container ) throws GraphQLAnnotationsException {
121+ GraphQLFieldDefinition .Builder builder = newFieldDefinition ();
122+ builder .name (new FieldNameBuilder (field ).build ());
123+ TypeFunction typeFunction = getTypeFunction (field , container );
124+
125+ GraphQLType outputType = typeFunction .buildType (field .getType (), field .getAnnotatedType (), container );
126+ boolean isConnection = ConnectionUtil .isConnection (field , outputType );
127+ if (isConnection ) {
128+ outputType = getGraphQLConnection (field , outputType , container .getRelay (), container .getTypeRegistry ());
129+ builder .argument (container .getRelay ().getConnectionFieldArguments ());
130+ }
131+
132+ builder .type ((GraphQLOutputType ) outputType ).description (new DescriptionBuilder (field ).build ())
133+ .deprecate (new DeprecateBuilder (field ).build ())
134+ .dataFetcher (new FieldDataFetcherBuilder (field , dataFetcherConstructor , outputType , typeFunction , container , isConnection ).build ());
135+
136+ return new GraphQLFieldDefinitionWrapper (builder .build ());
137+ }
138+
139+ public GraphQLInputObjectField getInputField (Method method , ProcessingElementsContainer container ) throws GraphQLAnnotationsException {
140+ GraphQLInputObjectField .Builder builder = newInputObjectField ();
141+ builder .name (new MethodNameBuilder (method ).build ());
142+ TypeFunction typeFunction = getTypeFunction (method , container );
143+ GraphQLInputType inputType = (GraphQLInputType ) new MethodTypeBuilder (method , typeFunction , container , true ).build ();
144+ return builder .type (inputType ).description (new DescriptionBuilder (method ).build ()).build ();
145+ }
146+
147+ public GraphQLInputObjectField getInputField (Field field , ProcessingElementsContainer container ) throws GraphQLAnnotationsException {
148+ GraphQLInputObjectField .Builder builder = newInputObjectField ();
149+ builder .name (new FieldNameBuilder (field ).build ());
150+ TypeFunction typeFunction = getTypeFunction (field , container );
151+ GraphQLType graphQLType = typeFunction .buildType (true ,field .getType (), field .getAnnotatedType (), container );
152+ return builder .type ((GraphQLInputType ) graphQLType ).description (new DescriptionBuilder (field ).build ()).build ();
153+ }
154+
123155 private GraphQLFieldDefinition handleRelayArguments (Method method , ProcessingElementsContainer container , GraphQLFieldDefinition .Builder builder , GraphQLOutputType outputType , List <GraphQLArgument > args ) {
124156 GraphQLFieldDefinition relayFieldDefinition = null ;
125157 if (method .isAnnotationPresent (GraphQLRelayMutation .class )) {
126158 relayFieldDefinition = buildRelayMutation (method , container , builder , outputType , args );
127- }
128- else {
159+ } else {
129160 builder .argument (args );
130161 }
131162 return relayFieldDefinition ;
@@ -138,7 +169,7 @@ private void handleConnectionArgument(ProcessingElementsContainer container, Gra
138169 }
139170
140171 private TypeFunction getTypeFunction (Method method , ProcessingElementsContainer container ) {
141- GraphQLType annotation = method .getAnnotation (GraphQLType .class );
172+ graphql . annotations . annotationTypes . GraphQLType annotation = method .getAnnotation (graphql . annotations . annotationTypes . GraphQLType .class );
142173 TypeFunction typeFunction = container .getDefaultTypeFunction ();
143174
144175 if (annotation != null ) {
@@ -166,27 +197,9 @@ private GraphQLFieldDefinition buildRelayMutation(Method method, ProcessingEleme
166197 return relayFieldDefinition ;
167198 }
168199
169- public GraphQLFieldDefinition getField (Field field , ProcessingElementsContainer container ) throws GraphQLAnnotationsException {
170- GraphQLFieldDefinition .Builder builder = newFieldDefinition ();
171- builder .name (new FieldNameBuilder (field ).build ());
172- TypeFunction typeFunction = getTypeFunction (field , container );
173-
174- GraphQLOutputType outputType = (GraphQLOutputType ) typeFunction .buildType (field .getType (), field .getAnnotatedType (), container );
175- boolean isConnection = ConnectionUtil .isConnection (field , outputType );
176- if (isConnection ) {
177- outputType = getGraphQLConnection (field , outputType , container .getRelay (), container .getTypeRegistry ());
178- builder .argument (container .getRelay ().getConnectionFieldArguments ());
179- }
180-
181- builder .type (outputType ).description (new DescriptionBuilder (field ).build ())
182- .deprecate (new DeprecateBuilder (field ).build ())
183- .dataFetcher (new FieldDataFetcherBuilder (field , dataFetcherConstructor , outputType , typeFunction , container , isConnection ).build ());
184-
185- return new GraphQLFieldDefinitionWrapper (builder .build ());
186- }
187200
188201 private TypeFunction getTypeFunction (Field field , ProcessingElementsContainer container ) {
189- GraphQLType annotation = field .getAnnotation (GraphQLType .class );
202+ graphql . annotations . annotationTypes . GraphQLType annotation = field .getAnnotation (graphql . annotations . annotationTypes . GraphQLType .class );
190203
191204 TypeFunction typeFunction = container .getDefaultTypeFunction ();
192205
@@ -196,7 +209,7 @@ private TypeFunction getTypeFunction(Field field, ProcessingElementsContainer co
196209 return typeFunction ;
197210 }
198211
199- private GraphQLOutputType getGraphQLConnection (AccessibleObject field , GraphQLOutputType type , Relay relay , Map <String , graphql .schema .GraphQLType > typeRegistry ) {
212+ private GraphQLOutputType getGraphQLConnection (AccessibleObject field , graphql . schema . GraphQLType type , Relay relay , Map <String , graphql .schema .GraphQLType > typeRegistry ) {
200213 if (type instanceof GraphQLNonNull ) {
201214 GraphQLList listType = (GraphQLList ) ((GraphQLNonNull ) type ).getWrappedType ();
202215 return new GraphQLNonNull (internalGetGraphQLConnection (field , listType , relay , typeRegistry ));
@@ -213,9 +226,9 @@ private GraphQLOutputType internalGetGraphQLConnection(AccessibleObject field, G
213226 return getActualType (relay .connectionType (connectionName , edgeType , Collections .emptyList ()), typeRegistry );
214227 }
215228
216- private void addExtensionField (GraphQLFieldDefinition gqlField , List <GraphQLFieldDefinition > fields , List <String > fieldsDefined ) {
217- if (!fieldsDefined .contains (gqlField .getName ())) {
218- fieldsDefined .add (gqlField .getName ());
229+ private void addExtensionField (GraphQLFieldDefinition gqlField , List <GraphQLFieldDefinition > fields , List <String > definedFields ) {
230+ if (!definedFields .contains (gqlField .getName ())) {
231+ definedFields .add (gqlField .getName ());
219232 fields .add (gqlField );
220233 } else {
221234 throw new GraphQLAnnotationsException ("Duplicate field found in extension : " + gqlField .getName (), null );
@@ -231,5 +244,4 @@ private GraphQLObjectType getActualType(GraphQLObjectType type, Map<String, grap
231244 return type ;
232245 }
233246
234-
235247}
0 commit comments