Module: NulogyGraphqlApi::GraphqlHelpers

Defined in:
lib/nulogy_graphql_api/rspec/graphql_helpers.rb

Instance Method Summary collapse

Instance Method Details

#execute_graphql(query, schema, variables: {}, context: {}) ⇒ Object

Prefer the ‘request_graphql` method over this one because it exercises more of the stack but doesn’t run much slower.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/nulogy_graphql_api/rspec/graphql_helpers.rb', line 5

def execute_graphql(query, schema, variables: {}, context: {})
  camelized_variables = variables.deep_transform_keys! { |key| key.to_s.camelize(:lower) } || {}

  response = schema.execute(
    query,
    variables: camelized_variables,
    context: context,
    operation_name: nil
  )

  response.to_h.deep_symbolize_keys
end

#request_graphql(url, query, variables: {}, headers: {}, user: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/nulogy_graphql_api/rspec/graphql_helpers.rb', line 18

def request_graphql(url, query, variables: {}, headers: {}, user: nil)
  params = { query: query, variables: variables }.to_json
  default_headers = {
    CONTENT_TYPE: "application/json",
    HTTP_AUTHORIZATION: basic_auth_token((user || default_user).)
  }

  post url, params: params, headers: default_headers.merge(headers)

  JSON.parse(response.body, symbolize_names: true)
end