-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql-playground.js
131 lines (119 loc) · 3.82 KB
/
graphql-playground.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const DEFAULT_ENDPOINT = 'https://demo.rgf.app/graphql'
function getBaseURL() {
return window.location.href.substring(0, window.location.href.lastIndexOf('/'))
}
function createGraphQLPlaygroundInstance(container, tabs, endpoint) {
if (typeof GraphQLPlayground !== 'undefined') {
delete GraphQLPlayground
}
const script = document.createElement('script')
script.src = getBaseURL() + '/graphql-playground-react-middleware.js'
script.onload = function () {
const GraphQLPlaygroundInstance = GraphQLPlayground
GraphQLPlaygroundInstance.init(
container,
{
endpoint,
tabs,
settings: {
"schema.polling.interval": 30000,
}
}
)
};
document.documentElement.firstChild.appendChild(script);
}
function createHeader(parent, title, description, container) {
const header = document.createElement('div')
header.classList.add('graphql-playground-header')
const heading = document.createElement('h3')
heading.classList.add('graphql-playground-heading', 'admonition', 'info')
heading.innerHTML = title
heading.title = `GraphQL Example "${title}": Click to open in fullscreen!`
heading.addEventListener('click', function () {
if (document.fullscreenElement) {
document.exitFullscreen()
} else {
container.requestFullscreen()
}
})
const descriptionNode = document.createElement('div')
descriptionNode.classList.add('graphql-playground-description', 'admonition', 'info')
const descriptionInner = document.createElement('div')
descriptionInner.innerHTML = `<p>${description}</p>`
descriptionNode.append(descriptionInner)
header.append(heading, descriptionNode)
return header
}
function createInstanceContainer(id) {
const container = document.createElement('graphql-playground-container')
container.id = `${id}-playground`
return container
}
function createInstance(container, config) {
container.innerHTML = ''
const instanceContainer = createInstanceContainer(container.id)
const header = createHeader(container, config.title, config.description, container)
container.append(header, instanceContainer)
return instanceContainer
}
function fetchConfig(url) {
url = getBaseURL() + url
console.log(`Fetching config from ${url}`)
return fetch(url).then((response) => response.json())
}
function fetchQuery(url) {
url = getBaseURL() + url
console.log(`Fetching query from ${url}`)
return fetch(url).then((response) => response.text())
}
function getTabConfig(endpoint, name, url, variables, headers) {
return fetchQuery(url).then(query => {
return {
endpoint,
name,
query,
variables,
headers
}
})
}
function getTabConfigs(config, endpoint) {
return Promise.all(
config.tabs.map(tab => getTabConfig(endpoint, tab.name, tab.url, tab.variables, tab.headers))
)
}
function getContainer(id) {
return document.getElementById(id)
}
window.GraphQLPlaygroundInstance = function (id, configUrl, endpoint) {
if (!id) {
console.error(`Missing id`)
return
}
if (!configUrl) {
console.error(`Missing src`)
return
}
endpoint = endpoint || DEFAULT_ENDPOINT
const container = getContainer(id)
fetchConfig(configUrl).then(config => {
endpoint = config.endpoint || endpoint
console.log(`Initializing GraphQLPlaygroundInstance ${id} ${endpoint}`, config)
const instanceContainer = createInstance(container, config)
getTabConfigs(config, endpoint).then(tabs => {
console.log(tabs)
createGraphQLPlaygroundInstance(
instanceContainer,
tabs,
endpoint
)
})
})
}
window.addEventListener('load', function () {
const playgrounds = Array.from(document.getElementsByTagName('graphql-playground'))
for (let playground of playgrounds) {
window.GraphQLPlaygroundInstance(playground.id, playground.getAttribute('src'))
}
})