1
- import { buildASTSchema , parse , GraphQLSchema , graphql , getNamedType , GraphQLNonNull } from 'graphql' ;
1
+ import { buildASTSchema , parse , GraphQLSchema , GraphQLObjectType , graphql , getNamedType , GraphQLNonNull , GraphQLID } from 'graphql' ;
2
2
import { GraphQLClient , request } from 'graphql-request' ;
3
3
4
4
import * as papa from 'papaparse' ;
@@ -77,11 +77,14 @@ function getEndpoint(config, argv) {
77
77
if ( ! key ) {
78
78
return console . log ( chalk . red ( `No endpoint found.` ) ) ;
79
79
}
80
- const endpoint = endpoints [ key ] ;
80
+ var endpoint = endpoints [ key ] ;
81
81
if ( ! endpoint ) {
82
82
return console . log ( chalk . red ( `No endpoint ${ key } found.` ) ) ;
83
83
}
84
- console . log ( chalk . green ( `Using endpoint ${ key } : ${ endpoint . url } ` ) ) ;
84
+ if ( typeof ( endpoint ) === "string" ) {
85
+ endpoint = { url : endpoint } ;
86
+ }
87
+ console . log ( chalk . green ( `Using endpoint ${ key } : ${ JSON . stringify ( endpoint ) } ` ) ) ;
85
88
return endpoint ;
86
89
}
87
90
@@ -105,6 +108,16 @@ function getMutation(config, basePath, argv) {
105
108
return mutationField ;
106
109
}
107
110
111
+ function findReturnExpression ( mutationField ) {
112
+ const returnType = getNamedType ( mutationField . type )
113
+ if ( returnType instanceof GraphQLObjectType ) {
114
+ const fields = ( returnType as GraphQLObjectType ) . getFields ( ) ;
115
+ const field = Object . keys ( fields ) . find ( ( x ) => ( getNamedType ( fields [ x ] . type ) === GraphQLID ) ) || Object . keys ( fields ) [ 0 ]
116
+ return `{ ${ field } }`
117
+ }
118
+ return "" ;
119
+ }
120
+
108
121
function buildMutations ( mutationField , args , data , mapping , delim ) {
109
122
const rMapping = { } ;
110
123
const regexp = new RegExp ( delim + "\s*" ) ;
@@ -141,7 +154,8 @@ function buildMutations(mutationField, args, data, mapping,delim) {
141
154
}
142
155
return `${ arg . name } : ${ value } ` ;
143
156
} ) . filter ( ( v ) => v !== null ) . join ( "," ) ;
144
- return fullfilled ? `_${ idx } : ${ mutationField . name } ( ${ params } )` : null ;
157
+ const returnExpression = findReturnExpression ( mutationField ) ;
158
+ return fullfilled ? `_${ idx } : ${ mutationField . name } ( ${ params } ) ${ returnExpression } ` : null ;
145
159
} ) . filter ( ( v ) => v !== null ) . join ( "\n" ) ;
146
160
147
161
return "mutation { \n" + mutations + "\n}" ;
0 commit comments