33import static graphql .kickstart .execution .GraphQLRequest .createQueryOnlyRequest ;
44
55import graphql .ExecutionResult ;
6- import graphql .kickstart .execution .GraphQLObjectMapper ;
7- import graphql .kickstart .execution .GraphQLQueryInvoker ;
86import graphql .kickstart .execution .GraphQLRequest ;
97import graphql .kickstart .execution .input .GraphQLSingleInvocationInput ;
108import graphql .kickstart .servlet .core .GraphQLMBean ;
119import graphql .kickstart .servlet .core .GraphQLServletListener ;
12- import graphql .kickstart .servlet .input .GraphQLInvocationInputFactory ;
1310import graphql .schema .GraphQLFieldDefinition ;
14- import java .util .ArrayList ;
1511import java .util .List ;
1612import java .util .Objects ;
1713import java .util .function .Consumer ;
3026public abstract class AbstractGraphQLHttpServlet extends HttpServlet implements Servlet ,
3127 GraphQLMBean {
3228
33- /**
34- * @deprecated use {@link #getConfiguration()} instead
35- */
36- @ Deprecated
37- private final List <GraphQLServletListener > listeners ;
38- private GraphQLConfiguration configuration ;
39- private HttpRequestHandler requestHandler ;
40-
41- public AbstractGraphQLHttpServlet () {
42- this (null );
43- }
44-
45- public AbstractGraphQLHttpServlet (List <GraphQLServletListener > listeners ) {
46- this .listeners = listeners != null ? new ArrayList <>(listeners ) : new ArrayList <>();
47- }
48-
49- /**
50- * @deprecated override {@link #getConfiguration()} instead
51- */
52- @ Deprecated
53- protected abstract GraphQLQueryInvoker getQueryInvoker ();
54-
55- /**
56- * @deprecated override {@link #getConfiguration()} instead
57- */
58- @ Deprecated
59- protected abstract GraphQLInvocationInputFactory getInvocationInputFactory ();
60-
61- /**
62- * @deprecated override {@link #getConfiguration()} instead
63- */
64- @ Deprecated
65- protected abstract GraphQLObjectMapper getGraphQLObjectMapper ();
66-
67- protected GraphQLConfiguration getConfiguration () {
68- return GraphQLConfiguration .with (getInvocationInputFactory ())
69- .with (getQueryInvoker ())
70- .with (getGraphQLObjectMapper ())
71- .with (listeners )
72- .build ();
73- }
74-
75- @ Override
76- public void init () {
77- if (configuration == null ) {
78- this .configuration = getConfiguration ();
79- this .requestHandler = configuration .createHttpRequestHandler ();
80- }
81- }
29+ protected abstract GraphQLConfiguration getConfiguration ();
8230
8331 public void addListener (GraphQLServletListener servletListener ) {
84- if (configuration != null ) {
85- configuration .add (servletListener );
86- } else {
87- listeners .add (servletListener );
88- }
32+ getConfiguration ().add (servletListener );
8933 }
9034
9135 public void removeListener (GraphQLServletListener servletListener ) {
92- if (configuration != null ) {
93- configuration .remove (servletListener );
94- } else {
95- listeners .remove (servletListener );
96- }
36+ getConfiguration ().remove (servletListener );
9737 }
9838
9939 @ Override
10040 public String [] getQueries () {
101- return configuration .getInvocationInputFactory ().getSchemaProvider ().getSchema ().getQueryType ()
41+ return getConfiguration ().getInvocationInputFactory ().getSchemaProvider ().getSchema ()
42+ .getQueryType ()
10243 .getFieldDefinitions ().stream ().map (GraphQLFieldDefinition ::getName ).toArray (String []::new );
10344 }
10445
10546 @ Override
10647 public String [] getMutations () {
107- return configuration .getInvocationInputFactory ().getSchemaProvider ().getSchema ()
48+ return getConfiguration () .getInvocationInputFactory ().getSchemaProvider ().getSchema ()
10849 .getMutationType ()
10950 .getFieldDefinitions ().stream ().map (GraphQLFieldDefinition ::getName ).toArray (String []::new );
11051 }
@@ -113,10 +54,11 @@ public String[] getMutations() {
11354 public String executeQuery (String query ) {
11455 try {
11556 GraphQLRequest graphQLRequest = createQueryOnlyRequest (query );
116- GraphQLSingleInvocationInput invocationInput = configuration .getInvocationInputFactory ()
57+ GraphQLSingleInvocationInput invocationInput = getConfiguration () .getInvocationInputFactory ()
11758 .create (graphQLRequest );
118- ExecutionResult result = configuration .getGraphQLInvoker ().query (invocationInput ).getResult ();
119- return configuration .getObjectMapper ().serializeResultAsJson (result );
59+ ExecutionResult result = getConfiguration ().getGraphQLInvoker ().query (invocationInput )
60+ .getResult ();
61+ return getConfiguration ().getObjectMapper ().serializeResultAsJson (result );
12062 } catch (Exception e ) {
12163 return e .getMessage ();
12264 }
@@ -133,12 +75,11 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
13375 }
13476
13577 private void doRequest (HttpServletRequest request , HttpServletResponse response ) {
136- init ();
13778 List <GraphQLServletListener .RequestCallback > requestCallbacks = runListeners (
13879 l -> l .onRequest (request , response ));
13980
14081 try {
141- requestHandler .handle (request , response );
82+ getConfiguration (). getHttpRequestHandler () .handle (request , response );
14283 runCallbacks (requestCallbacks , c -> c .onSuccess (request , response ));
14384 } catch (Exception t ) {
14485 log .error ("Error executing GraphQL request!" , t );
@@ -149,7 +90,7 @@ private void doRequest(HttpServletRequest request, HttpServletResponse response)
14990 }
15091
15192 private <R > List <R > runListeners (Function <? super GraphQLServletListener , R > action ) {
152- return configuration .getListeners ().stream ()
93+ return getConfiguration () .getListeners ().stream ()
15394 .map (listener -> {
15495 try {
15596 return action .apply (listener );
0 commit comments