1515package graphql .annotations .processor ;
1616
1717import graphql .annotations .annotationTypes .GraphQLName ;
18- import graphql .annotations .annotationTypes .GraphQLTypeExtension ;
1918import graphql .annotations .processor .exceptions .GraphQLAnnotationsException ;
2019import graphql .annotations .processor .graphQLProcessors .GraphQLAnnotationsProcessor ;
2120import graphql .annotations .processor .graphQLProcessors .GraphQLInputProcessor ;
2221import graphql .annotations .processor .graphQLProcessors .GraphQLOutputProcessor ;
23- import graphql .annotations .processor .retrievers .GraphQLObjectHandler ;
22+ import graphql .annotations .processor .retrievers .*;
23+ import graphql .annotations .processor .searchAlgorithms .BreadthFirstSearch ;
24+ import graphql .annotations .processor .searchAlgorithms .ParentalSearch ;
2425import graphql .annotations .processor .typeFunctions .DefaultTypeFunction ;
2526import graphql .annotations .processor .typeFunctions .TypeFunction ;
27+ import graphql .annotations .processor .util .DataFetcherConstructor ;
2628import graphql .relay .Relay ;
2729import graphql .schema .GraphQLObjectType ;
28- import org .osgi .service .component .annotations .Component ;
29- import org .osgi .service .component .annotations .Reference ;
3030
31- import java .util .HashSet ;
3231import java .util .Map ;
3332
3433import static graphql .annotations .processor .util .NamingKit .toGraphqlName ;
3736 * A utility class for extracting GraphQL data structures from annotated
3837 * elements.
3938 */
40- @ Component
4139public class GraphQLAnnotations implements GraphQLAnnotationsProcessor {
4240
4341 private GraphQLObjectHandler graphQLObjectHandler ;
42+ private GraphQLExtensionsHandler graphQLExtensionsHandler ;
43+
4444 private ProcessingElementsContainer container ;
4545
4646 public GraphQLAnnotations () {
47- this (new DefaultTypeFunction (new GraphQLInputProcessor (), new GraphQLOutputProcessor ()), new GraphQLObjectHandler ());
47+ GraphQLObjectHandler objectHandler = new GraphQLObjectHandler ();
48+ GraphQLTypeRetriever typeRetriever = new GraphQLTypeRetriever ();
49+ GraphQLObjectInfoRetriever objectInfoRetriever = new GraphQLObjectInfoRetriever ();
50+ GraphQLInterfaceRetriever interfaceRetriever = new GraphQLInterfaceRetriever ();
51+ GraphQLFieldRetriever fieldRetriever = new GraphQLFieldRetriever ();
52+ GraphQLInputProcessor inputProcessor = new GraphQLInputProcessor ();
53+ GraphQLOutputProcessor outputProcessor = new GraphQLOutputProcessor ();
54+ BreadthFirstSearch methodSearchAlgorithm = new BreadthFirstSearch (objectInfoRetriever );
55+ ParentalSearch fieldSearchAlgorithm = new ParentalSearch (objectInfoRetriever );
56+ DataFetcherConstructor dataFetcherConstructor = new DataFetcherConstructor ();
57+ GraphQLExtensionsHandler extensionsHandler = new GraphQLExtensionsHandler ();
58+ DefaultTypeFunction defaultTypeFunction = new DefaultTypeFunction (inputProcessor , outputProcessor );
59+
60+ objectHandler .setTypeRetriever (typeRetriever );
61+ typeRetriever .setGraphQLObjectInfoRetriever (objectInfoRetriever );
62+ typeRetriever .setGraphQLInterfaceRetriever (interfaceRetriever );
63+ typeRetriever .setMethodSearchAlgorithm (methodSearchAlgorithm );
64+ typeRetriever .setFieldSearchAlgorithm (fieldSearchAlgorithm );
65+ typeRetriever .setExtensionsHandler (extensionsHandler );
66+ typeRetriever .setGraphQLFieldRetriever (fieldRetriever );
67+ interfaceRetriever .setGraphQLTypeRetriever (typeRetriever );
68+ fieldRetriever .setDataFetcherConstructor (dataFetcherConstructor );
69+ inputProcessor .setGraphQLTypeRetriever (typeRetriever );
70+ outputProcessor .setGraphQLTypeRetriever (typeRetriever );
71+ extensionsHandler .setGraphQLObjectInfoRetriever (objectInfoRetriever );
72+ extensionsHandler .setFieldSearchAlgorithm (fieldSearchAlgorithm );
73+ extensionsHandler .setMethodSearchAlgorithm (methodSearchAlgorithm );
74+ extensionsHandler .setFieldRetriever (fieldRetriever );
75+
76+ this .graphQLObjectHandler = objectHandler ;
77+ this .graphQLExtensionsHandler = extensionsHandler ;
78+ this .container = new ProcessingElementsContainer (defaultTypeFunction );
4879 }
4980
50- public GraphQLAnnotations (TypeFunction defaultTypeFunction , GraphQLObjectHandler graphQLObjectHandler ) {
81+ public GraphQLAnnotations (TypeFunction defaultTypeFunction , GraphQLObjectHandler graphQLObjectHandler , GraphQLExtensionsHandler graphQLExtensionsHandler ) {
5182 this .graphQLObjectHandler = graphQLObjectHandler ;
83+ this .graphQLExtensionsHandler = graphQLExtensionsHandler ;
5284 this .container = new ProcessingElementsContainer (defaultTypeFunction );
5385 }
5486
@@ -73,28 +105,7 @@ public static GraphQLObjectType object(Class<?> object) throws GraphQLAnnotation
73105 }
74106
75107 public void registerTypeExtension (Class <?> objectClass ) {
76- GraphQLTypeExtension typeExtension = objectClass .getAnnotation (GraphQLTypeExtension .class );
77- if (typeExtension == null ) {
78- throw new GraphQLAnnotationsException ("Class is not annotated with GraphQLTypeExtension" , null );
79- } else {
80- Class <?> aClass = typeExtension .value ();
81- if (!container .getExtensionsTypeRegistry ().containsKey (aClass )) {
82- container .getExtensionsTypeRegistry ().put (aClass , new HashSet <>());
83- }
84- container .getExtensionsTypeRegistry ().get (aClass ).add (objectClass );
85- }
86- }
87-
88- public void unregisterTypeExtension (Class <?> objectClass ) {
89- GraphQLTypeExtension typeExtension = objectClass .getAnnotation (GraphQLTypeExtension .class );
90- if (typeExtension == null ) {
91- throw new GraphQLAnnotationsException ("Class is not annotated with GraphQLTypeExtension" , null );
92- } else {
93- Class <?> aClass = typeExtension .value ();
94- if (container .getExtensionsTypeRegistry ().containsKey (aClass )) {
95- container .getExtensionsTypeRegistry ().get (aClass ).remove (objectClass );
96- }
97- }
108+ graphQLExtensionsHandler .registerTypeExtension (objectClass , container );
98109 }
99110
100111 public void registerType (TypeFunction typeFunction ) {
@@ -109,6 +120,14 @@ public Map<String, graphql.schema.GraphQLType> getTypeRegistry() {
109120 return container .getTypeRegistry ();
110121 }
111122
123+ public GraphQLObjectHandler getObjectHandler () {
124+ return graphQLObjectHandler ;
125+ }
126+
127+ public GraphQLExtensionsHandler getExtensionsHandler () {
128+ return graphQLExtensionsHandler ;
129+ }
130+
112131 public ProcessingElementsContainer getContainer () {
113132 return container ;
114133 }
@@ -117,7 +136,6 @@ public void setContainer(ProcessingElementsContainer container) {
117136 this .container = container ;
118137 }
119138
120- @ Reference (target = "(type=default)" )
121139 public void setDefaultTypeFunction (TypeFunction function ) {
122140 this .container .setDefaultTypeFunction (function );
123141 }
0 commit comments