|
| 1 | +import * as path from 'path' |
| 2 | +import * as fs from 'fs' |
| 3 | +import * as findUp from 'find-up' |
| 4 | + |
| 5 | +import getLoadingMarkup from './get-loading-markup' |
| 6 | + |
| 7 | +export interface MiddlewareOptions { |
| 8 | + endpoint: string |
| 9 | + subscriptionEndpoint?: string |
| 10 | + setTitle?: string |
| 11 | + folderName?: string |
| 12 | +} |
| 13 | + |
| 14 | +export interface RenderPageOptions extends MiddlewareOptions { |
| 15 | + version: string |
| 16 | + env?: any |
| 17 | +} |
| 18 | + |
| 19 | +const configPath = findUp.sync(['.graphqlconfig', '.graphqlconfig.yml']) |
| 20 | +const configString = configPath |
| 21 | + ? fs.readFileSync(configPath, 'utf-8') |
| 22 | + : undefined |
| 23 | +const folderName = configPath |
| 24 | + ? path.basename(path.dirname(configPath)) |
| 25 | + : undefined |
| 26 | + |
| 27 | +const loading = getLoadingMarkup() |
| 28 | + |
| 29 | +export function renderPlaygroundPage(options: RenderPageOptions) { |
| 30 | + const extendedOptions = { |
| 31 | + ...options, |
| 32 | + configString, |
| 33 | + folderName, |
| 34 | + canSaveConfig: false, |
| 35 | + env: process.env, |
| 36 | + } |
| 37 | + return ` |
| 38 | + <!DOCTYPE html> |
| 39 | + <html> |
| 40 | + <head> |
| 41 | + <meta charset=utf-8 /> |
| 42 | + <meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui"> |
| 43 | + <title>GraphQL Playground</title> |
| 44 | + <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/graphql-playground@${ |
| 45 | + options.version |
| 46 | + }/build/static/css/index.css" /> |
| 47 | + <link rel="shortcut icon" href="//cdn.jsdelivr.net/npm/graphql-playground@${ |
| 48 | + options.version |
| 49 | + }/build/favicon.png" /> |
| 50 | + <script src="//cdn.jsdelivr.net/npm/graphql-playground@${ |
| 51 | + options.version |
| 52 | + }/build/static/js/middleware.js"></script> |
| 53 | + </head> |
| 54 | + <body> |
| 55 | + <style type="text/css"> |
| 56 | + html { |
| 57 | + font-family: "Open Sans", sans-serif; |
| 58 | + overflow: hidden; |
| 59 | + } |
| 60 | + |
| 61 | + body { |
| 62 | + margin: 0; |
| 63 | + background: #172a3a; |
| 64 | + } |
| 65 | + |
| 66 | + .playgroundIn { |
| 67 | + -webkit-animation: playgroundIn 0.5s ease-out forwards; |
| 68 | + animation: playgroundIn 0.5s ease-out forwards; |
| 69 | + } |
| 70 | + |
| 71 | + @-webkit-keyframes playgroundIn { |
| 72 | + from { |
| 73 | + opacity: 0; |
| 74 | + -webkit-transform: translateY(10px); |
| 75 | + -ms-transform: translateY(10px); |
| 76 | + transform: translateY(10px); |
| 77 | + } |
| 78 | + to { |
| 79 | + opacity: 1; |
| 80 | + -webkit-transform: translateY(0); |
| 81 | + -ms-transform: translateY(0); |
| 82 | + transform: translateY(0); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @keyframes playgroundIn { |
| 87 | + from { |
| 88 | + opacity: 0; |
| 89 | + -webkit-transform: translateY(10px); |
| 90 | + -ms-transform: translateY(10px); |
| 91 | + transform: translateY(10px); |
| 92 | + } |
| 93 | + to { |
| 94 | + opacity: 1; |
| 95 | + -webkit-transform: translateY(0); |
| 96 | + -ms-transform: translateY(0); |
| 97 | + transform: translateY(0); |
| 98 | + } |
| 99 | + } |
| 100 | + </style> |
| 101 | + ${loading.container} |
| 102 | + <div id="root" /> |
| 103 | + <script type="text/javascript"> |
| 104 | + window.addEventListener('load', function (event) { |
| 105 | + ${loading.script} |
| 106 | + |
| 107 | + const root = document.getElementById('root'); |
| 108 | + root.classList.add('playgroundIn'); |
| 109 | + |
| 110 | + GraphQLPlayground.init(root, ${JSON.stringify( |
| 111 | + extendedOptions, |
| 112 | + null, |
| 113 | + 2, |
| 114 | + )}) |
| 115 | + }) |
| 116 | + </script> |
| 117 | + </body> |
| 118 | + </html> |
| 119 | +` |
| 120 | +} |
0 commit comments