1- use constants:: * ;
21use failure;
32use fragments:: GqlFragment ;
43use graphql_parser:: query;
4+ use operations:: Operation ;
55use proc_macro2:: TokenStream ;
66use query:: QueryContext ;
77use schema;
@@ -10,84 +10,16 @@ use selection::Selection;
1010pub ( crate ) fn response_for_query (
1111 schema : schema:: Schema ,
1212 query : query:: Document ,
13- selected_operation : Option < String > ,
13+ selected_operation : String ,
1414) -> Result < TokenStream , failure:: Error > {
1515 let mut context = QueryContext :: new ( schema) ;
1616 let mut definitions = Vec :: new ( ) ;
17+ let mut operations: Vec < Operation > = Vec :: new ( ) ;
1718
1819 for definition in query. definitions {
1920 match definition {
20- query:: Definition :: Operation ( query:: OperationDefinition :: Query ( q) ) => {
21- context. root = {
22- let definition = context
23- . schema
24- . query_type
25- . clone ( )
26- . and_then ( |query_type| context. schema . objects . get ( & query_type) )
27- . expect ( "query type is defined" ) ;
28- let prefix = & q. name . expect ( "unnamed operation" ) ;
29- let prefix = format ! ( "RUST_{}" , prefix) ;
30- let selection = Selection :: from ( & q. selection_set ) ;
31-
32- definitions. extend (
33- definition. field_impls_for_selection ( & context, & selection, & prefix) ?,
34- ) ;
35- Some ( definition. response_fields_for_selection ( & context, & selection, & prefix) ?)
36- } ;
37-
38- context. register_variables ( & q. variable_definitions ) ;
39- }
40- query:: Definition :: Operation ( query:: OperationDefinition :: Mutation ( q) ) => {
41- context. root = {
42- let definition = context
43- . schema
44- . mutation_type
45- . clone ( )
46- . and_then ( |mutation_type| context. schema . objects . get ( & mutation_type) )
47- . expect ( "mutation type is defined" ) ;
48- let prefix = & q. name . expect ( "unnamed operation" ) ;
49- let prefix = format ! ( "RUST_{}" , prefix) ;
50- let selection = Selection :: from ( & q. selection_set ) ;
51-
52- definitions. extend (
53- definition. field_impls_for_selection ( & context, & selection, & prefix) ?,
54- ) ;
55- Some ( definition. response_fields_for_selection ( & context, & selection, & prefix) ?)
56- } ;
57-
58- context. register_variables ( & q. variable_definitions ) ;
59- }
60- query:: Definition :: Operation ( query:: OperationDefinition :: Subscription ( q) ) => {
61- context. root = {
62- let definition = context
63- . schema
64- . subscription_type
65- . clone ( )
66- . and_then ( |subscription_type| {
67- context. schema . objects . get ( & subscription_type)
68- } )
69- . expect ( "subscription type is defined" ) ;
70- let prefix = & q. name . expect ( "unnamed operation" ) ;
71- let prefix = format ! ( "RUST_{}" , prefix) ;
72- let selection = Selection :: from ( & q. selection_set ) ;
73-
74- if selection. 0 . len ( ) > 1 {
75- Err ( format_err ! (
76- "{}" ,
77- :: constants:: MULTIPLE_SUBSCRIPTION_FIELDS_ERROR
78- ) ) ?
79- }
80-
81- definitions. extend (
82- definition. field_impls_for_selection ( & context, & selection, & prefix) ?,
83- ) ;
84- Some ( definition. response_fields_for_selection ( & context, & selection, & prefix) ?)
85- } ;
86-
87- context. register_variables ( & q. variable_definitions ) ;
88- }
89- query:: Definition :: Operation ( query:: OperationDefinition :: SelectionSet ( _) ) => {
90- panic ! ( SELECTION_SET_AT_ROOT )
21+ query:: Definition :: Operation ( op) => {
22+ operations. push ( op. into ( ) ) ;
9123 }
9224 query:: Definition :: Fragment ( fragment) => {
9325 let query:: TypeCondition :: On ( on) = fragment. type_condition ;
@@ -103,15 +35,56 @@ pub(crate) fn response_for_query(
10335 }
10436 }
10537
38+ context. selected_operation = operations
39+ . iter ( )
40+ . find ( |op| op. name == selected_operation)
41+ . map ( |i| i. to_owned ( ) ) ;
42+
43+ let operation = context. selected_operation . clone ( ) . unwrap_or_else ( || {
44+ operations
45+ . iter ( )
46+ . next ( )
47+ . map ( |i| i. to_owned ( ) )
48+ . expect ( "no operation in query document" )
49+ } ) ;
50+
51+ let response_data_fields = {
52+ let root_name: String = operation
53+ . root_name ( & context. schema )
54+ . expect ( "operation type not in schema" ) ;
55+ let definition = context
56+ . schema
57+ . objects
58+ . get ( & root_name)
59+ . expect ( "schema declaration is invalid" ) ;
60+ let prefix = format ! ( "RUST_{}" , operation. name) ;
61+ let selection = & operation. selection ;
62+
63+ if operation. is_subscription ( ) && selection. 0 . len ( ) > 1 {
64+ Err ( format_err ! (
65+ "{}" ,
66+ :: constants:: MULTIPLE_SUBSCRIPTION_FIELDS_ERROR
67+ ) ) ?
68+ }
69+
70+ definitions. extend (
71+ definition
72+ . field_impls_for_selection ( & context, & selection, & prefix)
73+ . unwrap ( ) ,
74+ ) ;
75+ definition
76+ . response_fields_for_selection ( & context, & selection, & prefix)
77+ . unwrap ( )
78+ } ;
79+
10680 let enum_definitions = context. schema . enums . values ( ) . map ( |enm| enm. to_rust ( ) ) ;
10781 let fragment_definitions: Result < Vec < TokenStream > , _ > = context
10882 . fragments
10983 . values ( )
11084 . map ( |fragment| fragment. to_rust ( & context) )
11185 . collect ( ) ;
11286 let fragment_definitions = fragment_definitions?;
113- let variables_struct = context. expand_variables ( ) ;
114- let response_data_fields = context. root . as_ref ( ) . expect ( "no selection defined" ) ;
87+ let variables_struct = operation. expand_variables ( & context) ;
11588
11689 let input_object_definitions: Result < Vec < TokenStream > , _ > = context
11790 . schema
@@ -149,7 +122,7 @@ pub(crate) fn response_for_query(
149122 #[ derive( Debug , Serialize , Deserialize ) ]
150123 #[ serde( rename_all = "camelCase" ) ]
151124 pub struct ResponseData {
152- #( #response_data_fields) * ,
125+ #( #response_data_fields, ) *
153126 }
154127
155128 } )
0 commit comments