Skip to content

Commit

Permalink
fix(graphql): allow arbitrary operation names in GraphQL interface #235
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Oct 27, 2018
1 parent c40ce32 commit 16df628
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions src/dsl/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,25 @@
*
* @module GraphQL
*/
import { Interaction, ResponseOptions, RequestOptions, InteractionState, Query } from "../dsl/interaction";
import { HTTPMethod, methods } from "../common/request";
import { MatcherResult, regex } from "./matchers";
import { keys, isNil, extend } from "lodash";
import { Interaction, InteractionState } from "../dsl/interaction";
import { regex } from "./matchers";
import { isNil, extend } from "lodash";
import gql from "graphql-tag";

export type GraphQLOperation = "query" | "mutation" | null;

enum GraphQLOperations {
query = "query",
mutation = "mutation",
}

export interface GraphQLVariables { [name: string]: any; }

/**
* GraphQL interface
*/
export class GraphQLInteraction extends Interaction {
private operation: GraphQLOperation = null;
private operation: string;
private variables: GraphQLVariables = {};
private query: string;

/**
* The type of GraphQL operation. Generally not required.
*/
public withOperation(operation: GraphQLOperation) {
if (!operation || operation && keys(GraphQLOperations).indexOf(operation.toString()) < 0) {
throw new Error(`You must provide a valid HTTP method: ${keys(GraphQLOperations).join(", ")}.`);
}

public withOperation(operation: string) {
this.operation = operation;

return this;
Expand Down Expand Up @@ -93,9 +81,9 @@ export class GraphQLInteraction extends Interaction {
throw new Error("You must provide a description for the query.");
}

this.state.request = extend({
this.state.request = extend({
body: {
operationName: this.operation,
operationName: this.operation || null,
query: regex({ generate: this.query, matcher: escapeGraphQlQuery(this.query) }),
variables: this.variables,
},
Expand Down

0 comments on commit 16df628

Please sign in to comment.