UNPKG

@pact-foundation/pact

Version:
150 lines 5.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GraphQLPactV3 = void 0; const lodash_1 = require("lodash"); const ramda_1 = require("ramda"); const configurationError_1 = require("../../common/graphQL/configurationError"); const graphQL_1 = require("../../common/graphQL/graphQL"); const types_1 = require("../../common/graphQL/types"); const matchers_1 = require("../matchers"); const pact_1 = require("../pact"); /** * Expose a V3 compatible GraphQL interface * * Code borrowed/inspired from https://gist.github.com/wabrit/2d1e1f9520aa133908f0a3716338e5ff */ class GraphQLPactV3 extends pact_1.PactV3 { operation = undefined; variables = undefined; query; req = undefined; given(providerState, parameters) { super.given(providerState, parameters); return this; } uponReceiving(description) { super.uponReceiving(description); return this; } /** * The GraphQL operation name, if used. * @param operation {string} the name of the operation * @return this object */ withOperation(operation) { this.operation = operation; return this; } /** * Add variables used in the Query. * @param variables {GraphQLVariables} * @return this object */ 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 * } * } * }" * }' * @param query {string|ASTNode} parsed or unparsed query * @return this object */ withQuery(query) { this.query = (0, graphQL_1.validateQuery)(query, types_1.OperationType.Query); return this; } /** * The actual GraphQL mutation as a string or parse tree. * * 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 * } * } * @param mutation {string|ASTNode} parsed or unparsed mutation * @return this object */ withMutation(mutation) { this.query = (0, graphQL_1.validateQuery)(mutation, types_1.OperationType.Mutation); return this; } /** * Used to pass in the method, path and content-type; the body detail would * not typically be passed here as that will be internally constructed from * withQuery/withMutation/withVariables calls. * * @see {@link withQuery} * @see {@link withMutation} * @see {@link withVariables} * @param req {V3Request} request * @return this object */ withRequest(req) { // Just take what we need from the request, as most of the detail will // come from withQuery/withMutation/withVariables this.req = req; return this; } /** * Overridden as this is the "trigger point" by which we should have received all * request information. * @param res {V3Response} the expected response * @returns this object */ willRespondWith(res) { if (!this.query) { throw new configurationError_1.ConfigurationError('You must provide a GraphQL query.'); } if (!this.req) { throw new configurationError_1.ConfigurationError('You must provide a GraphQL request.'); } this.req.contentType ||= 'application/json'; this.req.method ||= 'POST'; this.req = { ...this.req, body: (0, ramda_1.reject)(lodash_1.isUndefined, { operationName: this.operation, query: (0, matchers_1.regex)((0, graphQL_1.escapeGraphQlQuery)(this.query), this.query), variables: this.variables, }), headers: { 'Content-Type': this.req.contentType, }, method: this.req.method, }; super.withRequest(this.req); super.willRespondWith(res); return this; } addInteraction() { throw new configurationError_1.ConfigurationError('Only GraphQL Queries are allowed'); } withRequestBinaryFile() { throw new configurationError_1.ConfigurationError('Only GraphQL Queries are allowed'); } withRequestMultipartFileUpload() { throw new configurationError_1.ConfigurationError('Only GraphQL Queries are allowed'); } } exports.GraphQLPactV3 = GraphQLPactV3; //# sourceMappingURL=graphQL.js.map