2016-06-14 17:31:36 -07:00
|
|
|
/*
|
|
|
|
* Mostly taken straight from express-graphql, so see their licence
|
|
|
|
* (https://github.com/graphql/express-graphql/blob/master/LICENSE)
|
|
|
|
*/
|
|
|
|
|
2016-06-15 20:35:48 -07:00
|
|
|
// TODO: in the future, build the GraphiQL app on the server, so it does not
|
|
|
|
// depend on any CDN and can be run offline.
|
|
|
|
|
2016-06-29 15:30:47 -04:00
|
|
|
/*
|
|
|
|
* Arguments:
|
|
|
|
*
|
|
|
|
* - endpointURL: the relative or absolute URL for the endpoint which GraphiQL will make queries to
|
|
|
|
* - (optional) query: the GraphQL query to pre-fill in the GraphiQL UI
|
|
|
|
* - (optional) variables: a JS object of variables to pre-fill in the GraphiQL UI
|
|
|
|
* - (optional) operationName: the operationName to pre-fill in the GraphiQL UI
|
|
|
|
* - (optional) result: the result of the query to pre-fill in the GraphiQL UI
|
2016-09-12 18:07:35 -04:00
|
|
|
* - (optional) passHeader: a string that will be added to the header object.
|
|
|
|
* For example "'Authorization': localStorage['Meteor.loginToken']" for meteor
|
2016-06-29 15:30:47 -04:00
|
|
|
*/
|
|
|
|
|
2016-06-14 17:31:36 -07:00
|
|
|
export type GraphiQLData = {
|
2016-06-29 13:57:21 -04:00
|
|
|
endpointURL: string,
|
2016-06-14 17:31:36 -07:00
|
|
|
query?: string,
|
|
|
|
variables?: Object,
|
|
|
|
operationName?: string,
|
2016-06-15 20:35:48 -07:00
|
|
|
result?: Object,
|
2017-01-23 13:10:42 +02:00
|
|
|
passHeader?: string,
|
2016-06-14 17:31:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Current latest version of GraphiQL.
|
2017-01-31 15:40:57 +01:00
|
|
|
const GRAPHIQL_VERSION = '0.9.1';
|
2016-06-14 17:31:36 -07:00
|
|
|
|
2016-06-26 15:59:15 -04:00
|
|
|
// Ensures string values are safe to be used within a <script> tag.
|
2016-06-26 17:55:58 -04:00
|
|
|
// TODO: I don't think that's the right escape function
|
2016-06-14 17:31:36 -07:00
|
|
|
function safeSerialize(data) {
|
|
|
|
return data ? JSON.stringify(data).replace(/\//g, '\\/') : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function renderGraphiQL(data: GraphiQLData): string {
|
2016-06-29 13:57:21 -04:00
|
|
|
const endpointURL = data.endpointURL;
|
2016-06-14 17:31:36 -07:00
|
|
|
const queryString = data.query;
|
|
|
|
const variablesString =
|
|
|
|
data.variables ? JSON.stringify(data.variables, null, 2) : null;
|
2016-06-26 15:59:15 -04:00
|
|
|
const resultString = null;
|
2016-06-14 17:31:36 -07:00
|
|
|
const operationName = data.operationName;
|
2016-09-12 18:07:35 -04:00
|
|
|
const passHeader = data.passHeader ? data.passHeader : '';
|
2016-06-14 17:31:36 -07:00
|
|
|
|
|
|
|
/* eslint-disable max-len */
|
2016-06-26 15:59:15 -04:00
|
|
|
return `
|
2016-06-14 17:31:36 -07:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
<title>GraphiQL</title>
|
|
|
|
<meta name="robots" content="noindex" />
|
|
|
|
<style>
|
|
|
|
html, body {
|
|
|
|
height: 100%;
|
|
|
|
margin: 0;
|
|
|
|
overflow: hidden;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<link href="//cdn.jsdelivr.net/graphiql/${GRAPHIQL_VERSION}/graphiql.css" rel="stylesheet" />
|
|
|
|
<script src="//cdn.jsdelivr.net/fetch/0.9.0/fetch.min.js"></script>
|
|
|
|
<script src="//cdn.jsdelivr.net/react/15.0.0/react.min.js"></script>
|
|
|
|
<script src="//cdn.jsdelivr.net/react/15.0.0/react-dom.min.js"></script>
|
|
|
|
<script src="//cdn.jsdelivr.net/graphiql/${GRAPHIQL_VERSION}/graphiql.min.js"></script>
|
2017-02-28 20:05:45 +02:00
|
|
|
<script src="//unpkg.com/subscriptions-transport-ws@0.5.3/browser/client.js"></script>
|
2016-06-14 17:31:36 -07:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<script>
|
|
|
|
// Collect the URL parameters
|
|
|
|
var parameters = {};
|
|
|
|
window.location.search.substr(1).split('&').forEach(function (entry) {
|
|
|
|
var eq = entry.indexOf('=');
|
|
|
|
if (eq >= 0) {
|
|
|
|
parameters[decodeURIComponent(entry.slice(0, eq))] =
|
|
|
|
decodeURIComponent(entry.slice(eq + 1));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Produce a Location query string from a parameter object.
|
2016-06-26 20:25:23 -04:00
|
|
|
function locationQuery(params, location) {
|
|
|
|
return (location ? location: '') + '?' + Object.keys(params).map(function (key) {
|
2016-06-14 17:31:36 -07:00
|
|
|
return encodeURIComponent(key) + '=' +
|
|
|
|
encodeURIComponent(params[key]);
|
|
|
|
}).join('&');
|
|
|
|
}
|
|
|
|
// Derive a fetch URL from the current URL, sans the GraphQL parameters.
|
|
|
|
var graphqlParamNames = {
|
|
|
|
query: true,
|
|
|
|
variables: true,
|
|
|
|
operationName: true
|
|
|
|
};
|
|
|
|
var otherParams = {};
|
|
|
|
for (var k in parameters) {
|
|
|
|
if (parameters.hasOwnProperty(k) && graphqlParamNames[k] !== true) {
|
|
|
|
otherParams[k] = parameters[k];
|
|
|
|
}
|
|
|
|
}
|
2017-02-28 20:05:45 +02:00
|
|
|
|
|
|
|
var subscriptionsClient;
|
|
|
|
var activeSubscriptionId = -1;
|
|
|
|
function initSubscriptions() {
|
|
|
|
var subscriptionsEndpoint = 'ws://' + window.location.hostname +
|
|
|
|
(window.location.port ? ':' + window.location.port: '') + '/subscriptions';
|
|
|
|
|
|
|
|
if (window.SubscriptionsTransportWs && window.SubscriptionsTransportWs.SubscriptionClient) {
|
|
|
|
subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient(subscriptionsEndpoint, {
|
|
|
|
reconnect: true
|
|
|
|
});
|
|
|
|
subscriptionsClient.onConnect(function() {
|
|
|
|
console.log('Connected to GraphQL Subscriptions server...');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function graphQlSubscriptionFetcher(graphQLParams) {
|
|
|
|
return {
|
|
|
|
subscribe: function(observer) {
|
|
|
|
observer.next('Your subscription data will apear here after server publication!');
|
|
|
|
|
|
|
|
activeSubscriptionId = subscriptionsClient.subscribe({
|
|
|
|
query: graphQLParams.query,
|
|
|
|
variables: graphQLParams.variables
|
|
|
|
}, function(error, result) {
|
|
|
|
if (error) {
|
|
|
|
observer.error(error);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
observer.next(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:07:35 -04:00
|
|
|
// We don't use safe-serialize for location, because it's not client input.
|
2016-06-30 09:25:31 -07:00
|
|
|
var fetchURL = locationQuery(otherParams, '${endpointURL}');
|
2017-02-28 20:05:45 +02:00
|
|
|
|
2016-06-14 17:31:36 -07:00
|
|
|
// Defines a GraphQL fetcher using the fetch API.
|
|
|
|
function graphQLFetcher(graphQLParams) {
|
2017-02-28 20:05:45 +02:00
|
|
|
if (subscriptionsClient && activeSubscriptionId !== -1) {
|
|
|
|
subscriptionsClient.unsubscribe(activeSubscriptionId);
|
|
|
|
}
|
|
|
|
if (subscriptionsClient && graphQLParams.query.startsWith('subscription')) {
|
|
|
|
return graphQlSubscriptionFetcher(graphQLParams);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return fetch('/graphql', {
|
|
|
|
method: 'post',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(graphQLParams),
|
|
|
|
credentials: 'include',
|
|
|
|
}).then(function (response) {
|
|
|
|
return response.text();
|
|
|
|
}).then(function (responseBody) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(responseBody);
|
|
|
|
} catch (error) {
|
|
|
|
return responseBody;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-06-14 17:31:36 -07:00
|
|
|
}
|
|
|
|
// When the query and variables string is edited, update the URL bar so
|
|
|
|
// that it can be easily shared.
|
|
|
|
function onEditQuery(newQuery) {
|
|
|
|
parameters.query = newQuery;
|
|
|
|
updateURL();
|
|
|
|
}
|
|
|
|
function onEditVariables(newVariables) {
|
|
|
|
parameters.variables = newVariables;
|
|
|
|
updateURL();
|
|
|
|
}
|
|
|
|
function onEditOperationName(newOperationName) {
|
|
|
|
parameters.operationName = newOperationName;
|
|
|
|
updateURL();
|
|
|
|
}
|
|
|
|
function updateURL() {
|
|
|
|
history.replaceState(null, null, locationQuery(parameters));
|
|
|
|
}
|
|
|
|
// Render <GraphiQL /> into the body.
|
|
|
|
ReactDOM.render(
|
|
|
|
React.createElement(GraphiQL, {
|
|
|
|
fetcher: graphQLFetcher,
|
|
|
|
onEditQuery: onEditQuery,
|
|
|
|
onEditVariables: onEditVariables,
|
|
|
|
onEditOperationName: onEditOperationName,
|
|
|
|
query: ${safeSerialize(queryString)},
|
|
|
|
response: ${safeSerialize(resultString)},
|
|
|
|
variables: ${safeSerialize(variablesString)},
|
|
|
|
operationName: ${safeSerialize(operationName)},
|
|
|
|
}),
|
|
|
|
document.body
|
|
|
|
);
|
2017-02-28 20:05:45 +02:00
|
|
|
|
|
|
|
initSubscriptions();
|
2016-06-14 17:31:36 -07:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>`;
|
2016-06-15 20:35:48 -07:00
|
|
|
}
|