# frozen_string_literal: true

require_relative 'abstract_github_service'

module GraphQLSchemaCompatibility
  class FileService < AbstractGithubService
    GetFileContentsQuery = parse <<~GRAPHQL
      query($repo: String!, $owner: String!, $expression: String!) {
        repository(name: $repo, owner: $owner) {
          object(expression: $expression) {
            ... on Blob {
              text
            }
          }
        }
      }
    GRAPHQL

    def file_contents(repo:, file:, sha:)
      owner, name = repo.split('/')
      result = execute(
        GetFileContentsQuery,
        owner: owner,
        repo: name,
        expression: "#{sha}:#{file}"
      )
      GraphQLSchemaCompatibility.logger.info("Downloaded #{file} @ #{sha}")
      result.data.repository.object.text
    end
  end
end
