Skip to content

Commit 3b84784

Browse files
authored
feature: add global headers support (#992)
1 parent 71fc40b commit 3b84784

File tree

8 files changed

+26
-7
lines changed

8 files changed

+26
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ interface ISettings {
148148
'prettier.tabWidth': number
149149
'prettier.useTabs': boolean
150150
'request.credentials': 'omit' | 'include' | 'same-origin'
151+
'request.globalHeaders': { [key: string]: string }
151152
'schema.polling.enable': boolean
152153
'schema.polling.endpointFilter': string
153154
'schema.polling.interval': number

packages/graphql-playground-html/src/render-playground-page.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface ISettings {
2727
'editor.fontSize': number
2828
'editor.fontFamily': string
2929
'request.credentials': string
30+
'request.globalHeaders': { [key: string]: string; }
3031
'schema.polling.enable': boolean
3132
'schema.polling.endpointFilter': string
3233
'schema.polling.interval': number

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,21 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
255255
const props = propsInput || this.props
256256
const endpoint = props.sessionEndpoint || props.endpoint
257257
const currentSchema = this.state.schema
258+
const globalHeaders = props.settings['request.globalHeaders']
259+
258260
try {
259261
const data = {
260262
endpoint,
261263
headers:
262264
props.sessionHeaders && props.sessionHeaders.length > 0
263-
? props.sessionHeaders
264-
: props.headers && Object.keys(props.headers).length > 0
265-
? JSON.stringify(props.headers)
266-
: undefined,
265+
? JSON.stringify({
266+
...globalHeaders,
267+
...JSON.parse(props.sessionHeaders),
268+
})
269+
: JSON.stringify({
270+
...globalHeaders,
271+
...props.headers,
272+
}),
267273
credentials: props.settings['request.credentials'],
268274
useTracingHeader:
269275
!this.initialSchemaFetch &&

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,15 @@ class TopBar extends React.Component<Props, {}> {
131131
} catch (e) {
132132
//
133133
}
134+
const globalHeaders = this.props.settings['request.globalHeaders']
134135
const headers = {
135136
'Accept-Encoding': 'gzip, deflate, br',
136137
'Content-Type': 'application/json',
137138
Accept: 'application/json',
138139
Connection: 'keep-alive',
139140
DNT: '1',
140141
Origin: location.origin || session.endpoint,
142+
...globalHeaders,
141143
...sessionHeaders,
142144
}
143145
const headersString = Object.keys(headers)

packages/graphql-playground-react/src/state/sessions/fetchingSagas.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ function* runQuerySaga(action) {
129129
}
130130
const lol = {
131131
endpoint: session.endpoint,
132-
headers,
132+
headers: {
133+
...settings['request.globalHeaders'],
134+
...headers,
135+
},
133136
credentials: settings['request.credentials'],
134137
}
135138

packages/graphql-playground-react/src/state/sessions/sagas.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ import { parse } from 'graphql'
3737
import { Session } from './reducers'
3838
import { safely, prettify } from '../../utils'
3939
import * as queryString from 'query-string'
40+
import { parseHeaders } from '../../components/Playground/util/parseHeaders'
4041

4142
function* setQueryFacts() {
4243
// debounce by 100 ms
4344
yield delay(100)
4445
const session: Session = yield select(getSelectedSession)
45-
4646
const { schema } = yield schemaFetcher.fetch(session)
4747
try {
4848
const ast = parse(session.query)
@@ -128,10 +128,14 @@ function* runQueryAtPosition(action) {
128128
function* getSessionWithCredentials() {
129129
const session = yield select(getSelectedSession)
130130
const settings = yield select(getSettings)
131+
const combinedHeaders = {
132+
...settings['request.globalHeaders'],
133+
...parseHeaders(session.headers),
134+
}
131135

132136
return {
133137
endpoint: session.endpoint,
134-
headers: session.headers,
138+
headers: JSON.stringify(combinedHeaders),
135139
credentials: settings['request.credentials'],
136140
}
137141
}

packages/graphql-playground-react/src/state/workspace/reducers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const defaultSettings: ISettings = {
5151
'prettier.tabWidth': 2,
5252
'prettier.useTabs': false,
5353
'request.credentials': 'omit',
54+
'request.globalHeaders': {},
5455
'schema.disableComments': true,
5556
'schema.polling.enable': true,
5657
'schema.polling.endpointFilter': '*localhost*',

packages/graphql-playground-react/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface ISettings {
2727
['prettier.tabWidth']: number
2828
['prettier.useTabs']: boolean
2929
['request.credentials']: 'omit' | 'include' | 'same-origin'
30+
['request.globalHeaders']: { [key: string]: string }
3031
['schema.disableComments']: boolean
3132
['schema.polling.enable']: boolean
3233
['schema.polling.endpointFilter']: string

0 commit comments

Comments
 (0)