@pact-foundation/pact
Version:
Pact for all things Javascript
61 lines • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphQLInteractionWithRequest = void 0;
const lodash_1 = require("lodash");
const ramda_1 = require("ramda");
const graphQL_1 = require("../../common/graphQL/graphQL");
const matchers_1 = require("../../v3/matchers");
const interactionWithRequest_1 = require("../http/interactionWithRequest");
const types_1 = require("./types");
class GraphQLInteractionWithRequest {
pact;
interaction;
opts;
cleanupFn;
graphQLRequest;
// tslint:disable:no-empty-function
constructor(pact, interaction, opts, cleanupFn, graphQLRequest) {
this.pact = pact;
this.interaction = interaction;
this.opts = opts;
this.cleanupFn = cleanupFn;
this.graphQLRequest = graphQLRequest;
}
/**
* 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
* }
* }
* }"
* }'
* @param query {string|ASTNode} parsed or unparsed query
* @return this object
*/
withQuery(query) {
return this.setQueryDetails(query, types_1.OperationType.Query);
}
withMutation(mutation) {
return this.setQueryDetails(mutation, types_1.OperationType.Mutation);
}
setQueryDetails(query, type) {
const validatedQuery = (0, graphQL_1.validateQuery)(query, type);
this.interaction.withRequestBody(JSON.stringify((0, ramda_1.reject)(lodash_1.isUndefined, {
operationName: this.graphQLRequest.operation,
query: (0, matchers_1.regex)((0, graphQL_1.escapeGraphQlQuery)(validatedQuery), validatedQuery),
variables: this.graphQLRequest.variables,
})), 'application/json');
return new interactionWithRequest_1.InteractionWithRequest(this.pact, this.interaction, this.opts, this.cleanupFn);
}
}
exports.GraphQLInteractionWithRequest = GraphQLInteractionWithRequest;
//# sourceMappingURL=graphQLInteractionWithRequest.js.map