Skip to content

Commit 0eb2492

Browse files
committed
fix(curl): Fixed curl command generation
Handle empty variables, take headers into account Closes #333
1 parent 0ef92d0 commit 0eb2492

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

packages/graphql-playground-react/src/components/Playground/GraphQLEditor.tsx

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,16 +610,43 @@ export class GraphQLEditor extends React.PureComponent<
610610
}
611611

612612
getCurl = () => {
613+
let variables
614+
try {
615+
variables = JSON.parse(this.state.variables)
616+
} catch (e) {
617+
//
618+
}
613619
const data = JSON.stringify({
614620
query: this.state.query,
615-
variables: this.state.variables,
621+
variables: variables,
616622
operationName: this.state.operationName,
617623
})
624+
let sessionHeaders
625+
626+
try {
627+
sessionHeaders = JSON.parse(this.props.session.headers!)
628+
} catch (e) {
629+
//
630+
}
631+
632+
const headers = {
633+
'Accept-Encoding': 'gzip, deflate, br',
634+
'Content-Type': 'application/json',
635+
'Accept': '*/*',
636+
'Connection': 'keep-alive',
637+
'DNT': '1',
638+
'Origin': location.origin ||
639+
this.props.session
640+
.endpoint,
641+
...sessionHeaders,
642+
}
643+
const headersString = Object.keys(headers).map(key => {
644+
const value = headers[key]
645+
return `-H '${key}: ${value}'`
646+
}).join(' ')
618647
return `curl '${
619648
this.props.session.endpoint
620-
}' -H 'Origin: ${location.origin ||
621-
this.props.session
622-
.endpoint}' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: */*' -H 'Connection: keep-alive' -H 'DNT: 1' --data-binary '${data}' --compressed`
649+
}' ${headersString} --data-binary '${data}' --compressed`
623650
}
624651

625652
setQueryVariablesRef = ref => {

0 commit comments

Comments
 (0)