|
| 1 | +import { gql } from 'graphql-tag'; |
| 2 | +import { ASTNode, print } from 'graphql'; |
| 3 | +import { isUndefined } from 'lodash'; |
| 4 | +import { reject } from 'ramda'; |
| 5 | + |
| 6 | +import { ConfigurationError } from './configurationError'; |
| 7 | +import { GraphQLQueryError } from './graphQLQueryError'; |
| 8 | +import { PactV3 } from '../pact'; |
| 9 | +import { GraphQLVariables } from '../../dsl/graphql'; |
| 10 | +import { V3Request, V3Response } from '../types'; |
| 11 | +import { OperationType } from './types'; |
| 12 | +import { JsonMap } from '../../common/jsonTypes'; |
| 13 | + |
| 14 | +import { regex } from '../matchers'; |
| 15 | + |
| 16 | +const escapeSpace = (s: string) => s.replace(/\s+/g, '\\s*'); |
| 17 | + |
| 18 | +const escapeRegexChars = (s: string) => |
| 19 | + s.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&'); |
| 20 | + |
| 21 | +const escapeGraphQlQuery = (s: string) => escapeSpace(escapeRegexChars(s)); |
| 22 | + |
| 23 | +/** |
| 24 | + * Accepts a raw or pre-parsed query, validating in the former case, and |
| 25 | + * returns a normalized raw query. |
| 26 | + * @param query {string|ASTNode} the query to validate |
| 27 | + * @param type the operation type |
| 28 | + */ |
| 29 | +function validateQuery(query: string | ASTNode, type: OperationType): string { |
| 30 | + if (!query) { |
| 31 | + throw new ConfigurationError(`You must provide a GraphQL ${type}.`); |
| 32 | + } |
| 33 | + |
| 34 | + if (typeof query !== 'string') { |
| 35 | + if (query?.kind === 'Document') { |
| 36 | + // Already parsed, store in string form |
| 37 | + return print(query); |
| 38 | + } |
| 39 | + throw new ConfigurationError( |
| 40 | + 'You must provide a either a string or parsed GraphQL.' |
| 41 | + ); |
| 42 | + } else { |
| 43 | + // String, so validate it |
| 44 | + try { |
| 45 | + gql(query); |
| 46 | + } catch (e) { |
| 47 | + throw new GraphQLQueryError(`GraphQL ${type} is invalid: ${e.message}`); |
| 48 | + } |
| 49 | + |
| 50 | + return query; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Expose a V3 compatible GraphQL interface |
| 56 | + * |
| 57 | + * Code borrowed/inspired from https://gist.github.com/wabrit/2d1e1f9520aa133908f0a3716338e5ff |
| 58 | + */ |
| 59 | +export class GraphQLPactV3 extends PactV3 { |
| 60 | + private operation?: string = undefined; |
| 61 | + |
| 62 | + private variables?: GraphQLVariables = undefined; |
| 63 | + |
| 64 | + private query: string; |
| 65 | + |
| 66 | + private req?: V3Request = undefined; |
| 67 | + |
| 68 | + public given(providerState: string, parameters?: JsonMap): GraphQLPactV3 { |
| 69 | + super.given(providerState, parameters); |
| 70 | + |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + public uponReceiving(description: string): GraphQLPactV3 { |
| 75 | + super.uponReceiving(description); |
| 76 | + |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * The GraphQL operation name, if used. |
| 82 | + * @param operation {string} the name of the operation |
| 83 | + * @return this object |
| 84 | + */ |
| 85 | + withOperation(operation: string): GraphQLPactV3 { |
| 86 | + this.operation = operation; |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Add variables used in the Query. |
| 92 | + * @param variables {GraphQLVariables} |
| 93 | + * @return this object |
| 94 | + */ |
| 95 | + withVariables(variables: GraphQLVariables): GraphQLPactV3 { |
| 96 | + this.variables = variables; |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * The actual GraphQL query as a string. |
| 102 | + * |
| 103 | + * NOTE: spaces are not important, Pact will auto-generate a space-insensitive matcher |
| 104 | + * |
| 105 | + * e.g. the value for the "query" field in the GraphQL HTTP payload: |
| 106 | + * '{ "query": "{ |
| 107 | + * Category(id:7) { |
| 108 | + * id, |
| 109 | + * name, |
| 110 | + * subcategories { |
| 111 | + * id, |
| 112 | + * name |
| 113 | + * } |
| 114 | + * } |
| 115 | + * }" |
| 116 | + * }' |
| 117 | + * @param query {string|ASTNode} parsed or unparsed query |
| 118 | + * @return this object |
| 119 | + */ |
| 120 | + withQuery(query: string | ASTNode): GraphQLPactV3 { |
| 121 | + this.query = validateQuery(query, OperationType.Query); |
| 122 | + |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * The actual GraphQL mutation as a string or parse tree. |
| 128 | + * |
| 129 | + * NOTE: spaces are not important, Pact will auto-generate a space-insensitive matcher |
| 130 | + * |
| 131 | + * e.g. the value for the "query" field in the GraphQL HTTP payload: |
| 132 | + * |
| 133 | + * mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) { |
| 134 | + * createReview(episode: $ep, review: $review) { |
| 135 | + * stars |
| 136 | + * commentary |
| 137 | + * } |
| 138 | + * } |
| 139 | + * @param mutation {string|ASTNode} parsed or unparsed mutation |
| 140 | + * @return this object |
| 141 | + */ |
| 142 | + withMutation(mutation: string | ASTNode): GraphQLPactV3 { |
| 143 | + this.query = validateQuery(mutation, OperationType.Mutation); |
| 144 | + |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Used to pass in the method, path and content-type; the body detail would |
| 150 | + * not typically be passed here as that will be internally constructed from |
| 151 | + * withQuery/withMutation/withVariables calls. |
| 152 | + * |
| 153 | + * @see {@link withQuery} |
| 154 | + * @see {@link withMutation} |
| 155 | + * @see {@link withVariables} |
| 156 | + * @param req {V3Request} request |
| 157 | + * @return this object |
| 158 | + */ |
| 159 | + withRequest(req: V3Request): GraphQLPactV3 { |
| 160 | + // Just take what we need from the request, as most of the detail will |
| 161 | + // come from withQuery/withMutation/withVariables |
| 162 | + this.req = req; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Overridden as this is the "trigger point" by which we should have received all |
| 168 | + * request information. |
| 169 | + * @param res {V3Response} the expected response |
| 170 | + * @returns this object |
| 171 | + */ |
| 172 | + willRespondWith(res: V3Response): GraphQLPactV3 { |
| 173 | + if (!this.query) { |
| 174 | + throw new ConfigurationError('You must provide a GraphQL query.'); |
| 175 | + } |
| 176 | + |
| 177 | + if (!this.req) { |
| 178 | + throw new ConfigurationError('You must provide a GraphQL request.'); |
| 179 | + } |
| 180 | + |
| 181 | + this.req = { |
| 182 | + ...this.req, |
| 183 | + body: reject(isUndefined, { |
| 184 | + operationName: this.operation, |
| 185 | + query: regex(escapeGraphQlQuery(this.query), this.query), |
| 186 | + variables: this.variables, |
| 187 | + }), |
| 188 | + headers: { |
| 189 | + 'Content-Type': (this.req.contentType ||= 'application/json'), |
| 190 | + }, |
| 191 | + method: (this.req.method ||= 'POST'), |
| 192 | + }; |
| 193 | + |
| 194 | + super.withRequest(this.req); |
| 195 | + super.willRespondWith(res); |
| 196 | + return this; |
| 197 | + } |
| 198 | + |
| 199 | + public addInteraction(): GraphQLPactV3 { |
| 200 | + throw new ConfigurationError('Only GraphQL Queries are allowed'); |
| 201 | + } |
| 202 | + |
| 203 | + public withRequestBinaryFile(): PactV3 { |
| 204 | + throw new ConfigurationError('Only GraphQL Queries are allowed'); |
| 205 | + } |
| 206 | + |
| 207 | + public withRequestMultipartFileUpload(): PactV3 { |
| 208 | + throw new ConfigurationError('Only GraphQL Queries are allowed'); |
| 209 | + } |
| 210 | +} |
0 commit comments