Sha256: 12ca413bedf600da1d5ea2821bc6d09fa0eecab0ad29a5ade92ff702eeab31f2

Contents?: true

Size: 982 Bytes

Versions: 4

Compression:

Stored size: 982 Bytes

Contents

module NulogyGraphqlApi
  module GraphqlHelpers
    # Prefer the `request_graphql` method over this one because it exercises more of the stack but doesn't run
    # much slower.
    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

    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).login)
      }

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nulogy_graphql_api-4.3.0 lib/nulogy_graphql_api/rspec/graphql_helpers.rb
nulogy_graphql_api-4.2.0 lib/nulogy_graphql_api/rspec/graphql_helpers.rb
nulogy_graphql_api-4.1.0 lib/nulogy_graphql_api/rspec/graphql_helpers.rb
nulogy_graphql_api-4.0.0 lib/nulogy_graphql_api/rspec/graphql_helpers.rb