1
- use constants:: * ;
2
1
use failure;
3
2
use fragments:: GqlFragment ;
4
3
use graphql_parser:: query;
4
+ use operations:: Operation ;
5
5
use proc_macro2:: TokenStream ;
6
6
use query:: QueryContext ;
7
7
use schema;
@@ -10,84 +10,16 @@ use selection::Selection;
10
10
pub ( crate ) fn response_for_query (
11
11
schema : schema:: Schema ,
12
12
query : query:: Document ,
13
- selected_operation : Option < String > ,
13
+ selected_operation : String ,
14
14
) -> Result < TokenStream , failure:: Error > {
15
15
let mut context = QueryContext :: new ( schema) ;
16
16
let mut definitions = Vec :: new ( ) ;
17
+ let mut operations: Vec < Operation > = Vec :: new ( ) ;
17
18
18
19
for definition in query. definitions {
19
20
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 ( ) ) ;
91
23
}
92
24
query:: Definition :: Fragment ( fragment) => {
93
25
let query:: TypeCondition :: On ( on) = fragment. type_condition ;
@@ -103,15 +35,56 @@ pub(crate) fn response_for_query(
103
35
}
104
36
}
105
37
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
+
106
80
let enum_definitions = context. schema . enums . values ( ) . map ( |enm| enm. to_rust ( ) ) ;
107
81
let fragment_definitions: Result < Vec < TokenStream > , _ > = context
108
82
. fragments
109
83
. values ( )
110
84
. map ( |fragment| fragment. to_rust ( & context) )
111
85
. collect ( ) ;
112
86
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) ;
115
88
116
89
let input_object_definitions: Result < Vec < TokenStream > , _ > = context
117
90
. schema
@@ -149,7 +122,7 @@ pub(crate) fn response_for_query(
149
122
#[ derive( Debug , Serialize , Deserialize ) ]
150
123
#[ serde( rename_all = "camelCase" ) ]
151
124
pub struct ResponseData {
152
- #( #response_data_fields) * ,
125
+ #( #response_data_fields, ) *
153
126
}
154
127
155
128
} )
0 commit comments