@graphql-hive/cli
Version:
A CLI util to manage and control your GraphQL Hive
89 lines • 5.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.graphqlRequest = graphqlRequest;
exports.cleanRequestId = cleanRequestId;
exports.isIntrospectionDisabledError = isIntrospectionDisabledError;
const graphql_1 = require("graphql");
const core_1 = require("@graphql-hive/core");
const errors_1 = require("./errors");
function graphqlRequest(config) {
const requestHeaders = Object.assign({ 'Content-Type': 'application/json', Accept: 'application/json', 'User-Agent': `hive-cli/${config.version}` }, config.additionalHeaders);
return {
request: async (args) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
let response;
try {
response = await core_1.http.post(config.endpoint, JSON.stringify({
query: typeof args.operation === 'string' ? args.operation : (0, graphql_1.print)(args.operation),
variables: args.variables,
}), {
logger: config.logger,
headers: requestHeaders,
timeout: args.timeout,
});
}
catch (e) {
const sourceError = (_a = e === null || e === void 0 ? void 0 : e.cause) !== null && _a !== void 0 ? _a : e;
if ((0, errors_1.isAggregateError)(sourceError)) {
throw new errors_1.NetworkError((_b = sourceError.errors[0]) === null || _b === void 0 ? void 0 : _b.message);
}
else {
throw new errors_1.NetworkError(sourceError);
}
}
if (!response.ok) {
throw new errors_1.HTTPError(config.endpoint, response.status, (_c = response.statusText) !== null && _c !== void 0 ? _c : 'Invalid status code for HTTP call');
}
let jsonData;
try {
jsonData = (await response.json());
}
catch (err) {
(_e = (_d = config.logger) === null || _d === void 0 ? void 0 : _d.debug) === null || _e === void 0 ? void 0 : _e.call(_d, String(err));
const contentType = (_f = response === null || response === void 0 ? void 0 : response.headers) === null || _f === void 0 ? void 0 : _f.get('content-type');
throw new errors_1.APIError(`Response from graphql was not valid JSON.${contentType ? ` Received "content-type": "${contentType}".` : ''}`, cleanRequestId((_g = response === null || response === void 0 ? void 0 : response.headers) === null || _g === void 0 ? void 0 : _g.get('x-request-id')));
}
if (jsonData.errors && jsonData.errors.length > 0) {
if (((_h = jsonData.errors[0].extensions) === null || _h === void 0 ? void 0 : _h.code) === 'ERR_MISSING_TARGET') {
throw new errors_1.MissingArgumentsError([
'target',
'The target on which the action is performed.' +
' This can either be a slug following the format "$organizationSlug/$projectSlug/$targetSlug" (e.g "the-guild/graphql-hive/staging")' +
' or an UUID (e.g. "a0f4c605-6541-4350-8cfe-b31f21a4bf80").',
]);
}
if (jsonData.errors[0].message === 'Invalid token provided') {
throw new errors_1.InvalidRegistryTokenError();
}
if (isIntrospectionDisabledError(jsonData.errors[0])) {
throw new errors_1.IntrospectionError();
}
(_k = (_j = config.logger) === null || _j === void 0 ? void 0 : _j.debug) === null || _k === void 0 ? void 0 : _k.call(_j, jsonData.errors.map(String).join('\n'));
throw new errors_1.APIError(jsonData.errors.map(e => e.message).join('\n'), cleanRequestId((_l = response === null || response === void 0 ? void 0 : response.headers) === null || _l === void 0 ? void 0 : _l.get('x-request-id')), jsonData.errors);
}
return jsonData.data;
},
};
}
function cleanRequestId(requestId) {
return requestId ? requestId.split(',')[0].trim() : undefined;
}
function isIntrospectionDisabledError(error) {
// Default implementation - this is the string used by graphql-js and thus the majority of all other implementations
// as they use graphql-js as a reference.
// https://github.com/graphql/graphql-js/blob/8eb6383ae7447514343457abb2063c40e5dc81bc/src/validation/rules/custom/NoSchemaIntrospectionCustomRule.ts#L30
if (error.message.includes('GraphQL introspection has been disabled')) {
return true;
}
// Apollo Server
// https://github.com/apollographql/apollo-server/blob/19d0ffca703f85f0184532393aa3fff687f30bca/packages/server/src/validationRules/NoIntrospection.ts#L20
if (error.extensions['validationErrorCode'] === 'INTROSPECTION_DISABLED') {
return true;
}
// https://github.com/apollographql/apollo-server/blob/19d0ffca703f85f0184532393aa3fff687f30bca/packages/server/src/validationRules/NoIntrospection.ts#L15
if (error.message.includes('GraphQL introspection is not allowed')) {
return true;
}
return false;
}
//# sourceMappingURL=graphql-request.js.map