platform/packages/graphql/angel_graphql/lib/src/graphiql.dart

90 lines
2.9 KiB
Dart
Raw Permalink Normal View History

2018-08-02 17:02:00 +00:00
import 'package:angel_framework/angel_framework.dart';
2018-11-01 21:27:36 +00:00
import 'package:http_parser/http_parser.dart';
2018-08-02 17:02:00 +00:00
/// Returns a simple [RequestHandler] that renders the GraphiQL visual interface for GraphQL.
///
/// By default, the interface expects your backend to be mounted at `/graphql`; this is configurable
/// via [graphQLEndpoint].
2019-04-18 04:12:52 +00:00
RequestHandler graphiQL(
2019-04-24 17:56:11 +00:00
{String graphQLEndpoint = '/graphql', String subscriptionsEndpoint}) {
2018-08-02 17:02:00 +00:00
return (req, res) {
res
2019-08-14 16:02:51 +00:00
..contentType = MediaType('text', 'html')
2019-04-18 04:12:52 +00:00
..write(renderGraphiql(
graphqlEndpoint: graphQLEndpoint,
subscriptionsEndpoint: subscriptionsEndpoint))
2018-11-01 21:27:36 +00:00
..close();
2018-08-02 17:02:00 +00:00
};
}
2019-04-18 04:12:52 +00:00
String renderGraphiql(
2019-04-24 17:56:11 +00:00
{String graphqlEndpoint = '/graphql', String subscriptionsEndpoint}) {
2019-04-18 04:12:52 +00:00
var subscriptionsScripts = '',
subscriptionsFetcher = '',
fetcherName = 'graphQLFetcher';
if (subscriptionsEndpoint != null) {
fetcherName = 'subscriptionsFetcher';
subscriptionsScripts = '''
2019-04-19 02:02:42 +00:00
<script src="//unpkg.com/subscriptions-transport-ws@0.8.3/browser/client.js"></script>
2019-04-18 04:12:52 +00:00
<script src="//unpkg.com/graphiql-subscriptions-fetcher@0.0.2/browser/client.js"></script>
''';
subscriptionsFetcher = '''
2019-08-14 16:02:51 +00:00
let subscriptionsClient = window.SubscriptionsTransportWs.SubscriptionClient('$subscriptionsEndpoint', {
2019-04-18 04:12:52 +00:00
reconnect: true
});
let $fetcherName = window.GraphiQLSubscriptionsFetcher.graphQLFetcher(subscriptionsClient, graphQLFetcher);
''';
}
2018-08-02 17:02:00 +00:00
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Angel GraphQL</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.11/graphiql.min.css">
<style>
html, body {
margin: 0;
padding: 0;
}
html, body, #app {
height: 100%;
}
</style>
</head>
<body>
<div id="app"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.min.js"></script>
2018-08-04 19:18:53 +00:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.11/graphiql.js"></script>
2019-04-18 04:12:52 +00:00
$subscriptionsScripts
2018-08-02 17:02:00 +00:00
<script>
window.onload = function() {
function graphQLFetcher(graphQLParams) {
return fetch('$graphqlEndpoint', {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(graphQLParams)
}).then(function(response) {
return response.json();
});
}
2019-04-18 04:12:52 +00:00
$subscriptionsFetcher
2018-08-02 17:02:00 +00:00
ReactDOM.render(
React.createElement(
GraphiQL,
2019-04-18 04:12:52 +00:00
{fetcher: $fetcherName}
2018-08-02 17:02:00 +00:00
),
document.getElementById('app')
);
};
</script>
</body>
</html>
'''
.trim();
}