Skip to content

Commit d63119f

Browse files
committed
Handle Object Return types from Muations
Handle string-only endpoint in .graphqlconfig
1 parent 550f567 commit d63119f

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/load.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {buildASTSchema,parse,GraphQLSchema,graphql,getNamedType,GraphQLNonNull} from 'graphql';
1+
import {buildASTSchema,parse,GraphQLSchema,GraphQLObjectType,graphql,getNamedType,GraphQLNonNull,GraphQLID} from 'graphql';
22
import { GraphQLClient, request } from 'graphql-request';
33

44
import * as papa from 'papaparse';
@@ -77,11 +77,14 @@ function getEndpoint(config, argv) {
7777
if (!key) {
7878
return console.log(chalk.red(`No endpoint found.`));
7979
}
80-
const endpoint = endpoints[key];
80+
var endpoint = endpoints[key];
8181
if (!endpoint) {
8282
return console.log(chalk.red(`No endpoint ${key} found.`));
8383
}
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)}`));
8588
return endpoint;
8689
}
8790

@@ -105,6 +108,16 @@ function getMutation(config, basePath, argv) {
105108
return mutationField;
106109
}
107110

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+
108121
function buildMutations(mutationField, args, data, mapping,delim) {
109122
const rMapping = {};
110123
const regexp = new RegExp(delim + "\s*");
@@ -141,7 +154,8 @@ function buildMutations(mutationField, args, data, mapping,delim) {
141154
}
142155
return `${arg.name}: ${value}`;
143156
}).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;
145159
}).filter((v) => v !== null).join("\n");
146160

147161
return "mutation { \n" + mutations +"\n}";

0 commit comments

Comments
 (0)