Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fabiensebban/70bafbfc3e86574166d34c1a8578bfa8 to your computer and use it in GitHub Desktop.
Save fabiensebban/70bafbfc3e86574166d34c1a8578bfa8 to your computer and use it in GitHub Desktop.
@api_version = "2024-07"
@store_url = "TODO"
@api_key = "TODO"
@password = "TODO"
def graphql_request(query, variables = {})
query = { query: query, variables: variables }.to_json unless variables.empty?
url = URI.parse("https://#{@store_url}/admin/api/#{@api_version}/graphql.json")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request.content_type = variables.empty? ? 'application/graphql' : 'application/json'
request.basic_auth(@api_key, @password)
request.body = query
http_response = https.request(request)
response = JSON.parse(http_response.body)
raise response.inspect if response['errors'].present?
response
end
query = "{
discountNodes(last: 5){
edges{
node{
id
__typename
}
}
pageInfo{
hasNextPage
endCursor
}
}
}"
result = graphql_request(query)
loop do
break if result['data']['discountNodes']['pageInfo']['hasNextPage'] == false
result['data']['discountNodes']['edges'].each do |r|
next if r['node']['id'].include?("DiscountAutomaticNode")
puts "DOING #{r['node']['id']}"
mutation = "mutation discountCodeBasicUpdate($id: ID!, $basicCodeDiscount: DiscountCodeBasicInput!) {
discountCodeBasicUpdate(id: $id, basicCodeDiscount: $basicCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBasic {
combinesWith{
shippingDiscounts
orderDiscounts
productDiscounts
}
}
}
}
userErrors {
field
code
message
}
}
}"
variables = {
"id": "#{r['node']['id']}",
"basicCodeDiscount": {
"combinesWith": {
"shippingDiscounts": true,
"orderDiscounts":false,
"productDiscounts":false
}
}
}
graphql_request(mutation, variables)
end
query = "{
discountNodes(last: 250, before: \"#{result['data']['discountNodes']['pageInfo']['endCursor']}\"){
edges{
node{
id
__typename
}
}
pageInfo{
hasNextPage
endCursor
}
}
}"
puts query
result = graphql_request(query)
puts "NEXT PAGE, cursor is #{result['data']['discountNodes']['pageInfo']['endCursor']}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment