@pact-foundation/pact
Version:
Pact for all things Javascript
120 lines • 3.96 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphQLInteraction = void 0;
/**
* Pact GraphQL module.
*
* @module GraphQL
*/
const lodash_1 = require("lodash");
const ramda_1 = require("ramda");
const graphQL_1 = require("../common/graphQL/graphQL");
const types_1 = require("../common/graphQL/types");
const configurationError_1 = __importDefault(require("../errors/configurationError"));
const graphQLQueryError_1 = __importDefault(require("../errors/graphQLQueryError"));
const interaction_1 = require("./interaction");
const matchers_1 = require("./matchers");
/**
* GraphQL interface
*/
class GraphQLInteraction extends interaction_1.Interaction {
operation = undefined;
variables = undefined;
query;
/**
* The type of GraphQL operation. Generally not required.
*/
withOperation(operation) {
this.operation = operation;
return this;
}
/**
* Any variables used in the Query
*/
withVariables(variables) {
this.variables = variables;
return this;
}
/**
* The actual GraphQL query as a string.
*
* NOTE: spaces are not important, Pact will auto-generate a space-insensitive matcher
*
* e.g. the value for the "query" field in the GraphQL HTTP payload:
* '{ "query": "{
* Category(id:7) {
* id,
* name,
* subcategories {
* id,
* name
* }
* }
* }"
* }'
*/
withQuery(query) {
return this.queryOrMutation(query, 'query');
}
/**
* The actual GraphQL mutation as a string.
*
* NOTE: spaces are not important, Pact will auto-generate a space-insensitive matcher
*
* e.g. the value for the "query" field in the GraphQL HTTP payload:
*
* mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
* createReview(episode: $ep, review: $review) {
* stars
* commentary
* }
* }
*/
withMutation(mutation) {
return this.queryOrMutation(mutation, 'mutation');
}
/**
* Returns the interaction object created.
*/
json() {
super.json();
if ((0, lodash_1.isNil)(this.query)) {
throw new configurationError_1.default('You must provide a GraphQL query.');
}
if ((0, lodash_1.isNil)(this.state.description)) {
throw new graphQLQueryError_1.default('You must provide a description for the query.');
}
this.state.request = (0, lodash_1.extend)({
body: (0, ramda_1.reject)(lodash_1.isUndefined, {
operationName: this.operation,
query: (0, matchers_1.regex)({
generate: this.query,
matcher: (0, graphQL_1.escapeGraphQlQuery)(this.query),
}),
variables: this.variables,
}),
headers: { 'Content-Type': 'application/json' },
method: 'POST',
}, this.state.request);
return this.state;
}
queryOrMutation(query, type) {
if ((0, lodash_1.isNil)(query)) {
throw new configurationError_1.default(`You must provide a GraphQL ${type}.`);
}
try {
(0, graphQL_1.validateQuery)(query, type === 'query' ? types_1.OperationType.Query : types_1.OperationType.Mutation);
}
catch (e) {
const error = e instanceof Error ? e : new Error(String(e));
throw new graphQLQueryError_1.default(`GraphQL ${type} is invalid: ${error.message}`);
}
this.query = query;
return this;
}
}
exports.GraphQLInteraction = GraphQLInteraction;
//# sourceMappingURL=graphql.js.map