Created
March 16, 2022 09:08
-
-
Save jcamblan/2ee86fb3cd1d963cc7cdfa3537c2ad1a to your computer and use it in GitHub Desktop.
Format GraphQL curl for smoothest GraphQL/Postman/Playground/Thunder usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
def prompt(label, value) | |
puts "" | |
puts label | |
puts "" | |
puts value | |
puts "" | |
puts "" | |
end | |
url = ARGV.find { |arg| arg.match(/^http(.*)/) } | |
raw_data = ARGV.find { |arg| arg.match(/^{\"operationName\":(.*)/) } | |
if url.nil? || raw_data.nil? | |
prompt("USAGE:", "ruby graphql-from-curl.rb RAW_CURL") | |
else | |
data = JSON.parse(raw_data) | |
variables = data['variables'] | |
query = data['query'].gsub('\\n', "\n") | |
raw_token = ARGV.find { |arg| arg.match(/^authorization/)} | |
token = raw_token&.gsub('authorization: Bearer ', '') | |
prompt('URL:', url) | |
prompt('TOKEN:', token) if token | |
prompt('VARIABLES:', variables) if variables | |
prompt('QUERY:', query) | |
end | |
# Example usage: | |
# ruby graphqurl.rb curl 'https://kidem-api-dev.osc-fr1.scalingo.io/graphql' -H 'accept: */*' -H 'Origin: file://' -H 'authorization: Bearer 1RO1yX7Mf_BOQW2AVy_WfPToNCa6-EOJCU_Xn_fVM2g' -H 'Sec-Fetch-Dest: empty' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_16_0) AppleWebKit/537.36 (KHTML, like Gecko) ReactNativeDebugger/0.11.5 Chrome/80.0.3987.165 Electron/8.5.1 Safari/537.36' -H 'content-type: application/json' --data-binary $'{"operationName":"getPatientHobbyAssessments","variables":{"uuid":"UGF0aWVudC0yMjA=","assessment":"like"},"query":"query getPatientHobbyAssessments($uuid: ID\u0021, $assessment: String) {\\n patient(patientId: $uuid) {\\n uuid\\n hobbyAssessments(filter: {assessment: {eq: $assessment}}) {\\n edges {\\n node {\\n assessment\\n hobby {\\n uuid\\n title\\n attachment {\\n uuid\\n url\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n}"}' --compressed | |
# Prompts the following: | |
# URL: | |
# https://kidem-api-dev.osc-fr1.scalingo.io/graphql | |
# TOKEN: | |
# 1RO1yX7Mf_BOQW2AVy_WfPToNCa6-EOJCU_Xn_fVM2g | |
# VARIABLES: | |
# {"uuid"=>"UGF0aWVudC0yMjA=", "assessment"=>"like"} | |
# QUERY: | |
# query getPatientHobbyAssessments($uuid: ID!, $assessment: String) { | |
# patient(patientId: $uuid) { | |
# uuid | |
# hobbyAssessments(filter: {assessment: {eq: $assessment}}) { | |
# edges { | |
# node { | |
# assessment | |
# hobby { | |
# uuid | |
# title | |
# attachment { | |
# uuid | |
# url | |
# __typename | |
# } | |
# __typename | |
# } | |
# __typename | |
# } | |
# __typename | |
# } | |
# __typename | |
# } | |
# __typename | |
# } | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment