22
33import  java .io .ByteArrayInputStream ;
44import  java .io .IOException ;
5+ import  java .io .StringReader ;
56import  java .io .StringWriter ;
6- import  java .nio .charset .StandardCharsets ;
7+ import  java .io .UnsupportedEncodingException ;
8+ import  java .net .URLDecoder ;
79import  java .util .List ;
810
911import  javax .json .Json ;
12+ import  javax .json .JsonBuilderFactory ;
1013import  javax .json .JsonObject ;
14+ import  javax .json .JsonObjectBuilder ;
1115import  javax .json .JsonReader ;
16+ import  javax .json .JsonReaderFactory ;
1217import  javax .json .JsonWriter ;
18+ import  javax .json .JsonWriterFactory ;
1319
1420import  io .quarkus .arc .Arc ;
1521import  io .quarkus .arc .ManagedContext ;
2935public  class  SmallRyeGraphQLExecutionHandler  implements  Handler <RoutingContext > {
3036    private  static  boolean  allowGet  = false ;
3137    private  static  final  String  QUERY  = "query" ;
38+     private  static  final  String  VARIABLES  = "variables" ;
3239    private  static  final  String  OK  = "OK" ;
3340    private  volatile  ExecutionService  executionService ;
3441    private  final  CurrentIdentityAssociation  currentIdentityAssociation ;
42+     private  static  final  JsonBuilderFactory  jsonObjectFactory  = Json .createBuilderFactory (null );
43+     private  static  final  JsonReaderFactory  jsonReaderFactory  = Json .createReaderFactory (null );
44+     private  static  final  JsonWriterFactory  jsonWriterFactory  = Json .createWriterFactory (null );
3545
3646    public  SmallRyeGraphQLExecutionHandler (boolean  allowGet , CurrentIdentityAssociation  currentIdentityAssociation ) {
37-         this .allowGet  = allowGet ;
47+         SmallRyeGraphQLExecutionHandler .allowGet  = allowGet ;
3848        this .currentIdentityAssociation  = currentIdentityAssociation ;
3949    }
4050
@@ -96,13 +106,26 @@ private void handlePost(HttpServerResponse response, RoutingContext ctx) {
96106
97107    private  void  handleGet (HttpServerResponse  response , RoutingContext  ctx ) {
98108        if  (allowGet ) {
99-             List <String > queries  = ctx .queryParam (QUERY );
100-             if  (queries  != null  && !queries .isEmpty ()) {
101-                 String  graphqlGetRequest  = queries .get (0 );
102-                 String  getResponse  = doRequest (graphqlGetRequest .getBytes (StandardCharsets .UTF_8 ));
103-                 response .setStatusCode (200 )
104-                         .setStatusMessage (OK )
105-                         .end (Buffer .buffer (getResponse ));
109+             String  query  = getQueryParameter (ctx , QUERY );
110+             if  (query  != null  && !query .isEmpty ()) {
111+                 try  {
112+                     String  variables  = getQueryParameter (ctx , VARIABLES );
113+ 
114+                     JsonObjectBuilder  input  = jsonObjectFactory .createObjectBuilder ();
115+                     input .add (QUERY , URLDecoder .decode (query , "UTF8" ));
116+                     if  (variables  != null  && !variables .isEmpty ()) {
117+                         JsonObject  jsonObject  = toJsonObject (URLDecoder .decode (variables , "UTF8" ));
118+                         input .add (VARIABLES , jsonObject );
119+                     }
120+ 
121+                     String  getResponse  = doRequest (input .build ());
122+ 
123+                     response .setStatusCode (200 )
124+                             .setStatusMessage (OK )
125+                             .end (Buffer .buffer (getResponse ));
126+                 } catch  (UnsupportedEncodingException  ex ) {
127+                     throw  new  RuntimeException (ex );
128+                 }
106129            } else  {
107130                response .setStatusCode (204 ).end ();
108131            }
@@ -111,6 +134,14 @@ private void handleGet(HttpServerResponse response, RoutingContext ctx) {
111134        }
112135    }
113136
137+     private  String  getQueryParameter (RoutingContext  ctx , String  parameterName ) {
138+         List <String > all  = ctx .queryParam (parameterName );
139+         if  (all  != null  && !all .isEmpty ()) {
140+             return  all .get (0 );
141+         }
142+         return  null ;
143+     }
144+ 
114145    private  String  getAllowedMethods () {
115146        if  (allowGet ) {
116147            return  "GET, POST, OPTIONS" ;
@@ -121,23 +152,39 @@ private String getAllowedMethods() {
121152
122153    private  String  doRequest (final  byte [] body ) {
123154        try  (ByteArrayInputStream  input  = new  ByteArrayInputStream (body );
124-                 final  JsonReader  jsonReader  = Json .createReader (input )) {
155+                 final  JsonReader  jsonReader  = jsonReaderFactory .createReader (input )) {
125156            JsonObject  jsonInput  = jsonReader .readObject ();
126-             JsonObject  outputJson  = getExecutionService ().execute (jsonInput );
127-             if  (outputJson  != null ) {
128-                 try  (StringWriter  output  = new  StringWriter ();
129-                         final  JsonWriter  jsonWriter  = Json .createWriter (output )) {
130-                     jsonWriter .writeObject (outputJson );
131-                     output .flush ();
132-                     return  output .toString ();
133-                 }
134-             }
135-             return  null ;
157+             return  doRequest (jsonInput );
136158        } catch  (IOException  ex ) {
137159            throw  new  RuntimeException (ex );
138160        }
139161    }
140162
163+     private  String  doRequest (JsonObject  jsonInput ) {
164+         JsonObject  outputJson  = getExecutionService ().execute (jsonInput );
165+         if  (outputJson  != null ) {
166+             try  (StringWriter  output  = new  StringWriter ();
167+                     final  JsonWriter  jsonWriter  = jsonWriterFactory .createWriter (output )) {
168+                 jsonWriter .writeObject (outputJson );
169+                 output .flush ();
170+                 return  output .toString ();
171+             } catch  (IOException  ex ) {
172+                 throw  new  RuntimeException (ex );
173+             }
174+         }
175+         return  null ;
176+     }
177+ 
178+     private  static  JsonObject  toJsonObject (String  jsonString ) {
179+         if  (jsonString  == null  || jsonString .isEmpty ()) {
180+             return  null ;
181+         }
182+ 
183+         try  (JsonReader  jsonReader  = jsonReaderFactory .createReader (new  StringReader (jsonString ))) {
184+             return  jsonReader .readObject ();
185+         }
186+     }
187+ 
141188    private  ExecutionService  getExecutionService () {
142189        if  (this .executionService  == null ) {
143190            this .executionService  = Arc .container ().instance (ExecutionService .class ).get ();
0 commit comments