Module: RSpec::GraphqlIntegration::Matchers::MatchGraphqlResponse

Extended by:
Matchers::DSL
Defined in:
lib/rspec/graphql_integration/matchers/match_graphql_response.rb

Overview

This module contains the matchers that are used to test GraphQL queries and mutations.

Defined Under Namespace

Classes: TestVariableMissing

Constant Summary collapse

REQUIRED_TEST_VARIABLES =

Defines the required variables as a hash for a test that uses the GraphQL response matcher.

Key is the variable name, value is an example value.

{ test_dir: "__FILE__" }.freeze

Instance Method Summary collapse

Instance Method Details

#load_query(dir, filename) ⇒ Object

Loads a query in a GraphQL file.

Example: load_query(__FILE__, ‘current_user/query.graphql’),



90
91
92
# File 'lib/rspec/graphql_integration/matchers/match_graphql_response.rb', line 90

def load_query(dir, filename)
  File.read(File.join(File.dirname(dir), filename))
end

#load_response(dir, filename, variables = {}) ⇒ Object

Loads a response in a JSON file and substitute the passed variables that are surrounded by {…} in the file.

Example: load_response(

__FILE__,
'current_user/response.json',
{
  user_id: user.id,
},

)



106
107
108
109
110
111
# File 'lib/rspec/graphql_integration/matchers/match_graphql_response.rb', line 106

def load_response(dir, filename, variables = {})
  json_file = File.read(File.join(File.dirname(dir), filename))
  variables.each { |key, value| json_file.gsub!("\"{{#{key}}}\"", JSON.dump(value)) }

  JSON.parse(json_file)
end