Skip to content

Instantly share code, notes, and snippets.

@amkisko
Last active November 24, 2022 15:01
Show Gist options
  • Select an option

  • Save amkisko/a58d878f3acabb7557f0bb4ad838b99d to your computer and use it in GitHub Desktop.

Select an option

Save amkisko/a58d878f3acabb7557f0bb4ad838b99d to your computer and use it in GitHub Desktop.
Apollo Studio graph variants cleanup script (uses git branches for matching existing variants)
#!/usr/bin/env ruby
# USAGE:
# APOLLO_SCHEMA_KEY=service:... APOLLO_SCHEMA_NAME=Project-Name ./cleanup_apollo_schema_variants.rb
require "pry"
require "uri"
require "json"
require "net/http"
def call_api(query:, variables: nil)
url = URI("https://graphql.api.apollographql.com/api/graphql")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request["X-API-Key"] = ENV["APOLLO_SCHEMA_KEY"]
request["Content-Type"] = "application/json"
variables = { serviceId: ENV["APOLLO_SCHEMA_NAME"] }.merge(variables || {})
request.body = { query:, variables: }.to_json
response = https.request(request)
Struct.new(:code, :data).new(
response.code,
JSON.parse(response.body)
)
end
def list_all_variants
call_api(
query: %|
query Graph__ListServiceVartiansQuery($serviceId: ID!) {
service(id: $serviceId) {
variants { name }
}
}
|
).data.dig("data", "service", "variants").map{_1["name"]}
end
def delete_variant(schemaTag)
call_api(
query: %|
mutation Graph__DeleteGraphVariantMutation($serviceId: ID!, $schemaTag: String!) {
service(id: $serviceId) {
deleteSchemaTag(tag: $schemaTag) {
deleted
}
}
}
|,
variables: {
schemaTag:
}
).code
end
def list_branches
%x{git ls-remote --heads}.scan(/refs\/heads\/(.+)/).flatten.compact
end
branches = list_branches
list_all_variants.reject{branches.include?(_1)}.each do |schemaTag|
delete_variant(schemaTag)
puts %{Deleted "#{schemaTag}" variant}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment