diff --git a/samples/js/graphiql/.gitignore b/samples/js/graphiql/.gitignore index 07007cf6cc..345162c835 100644 --- a/samples/js/graphiql/.gitignore +++ b/samples/js/graphiql/.gitignore @@ -1,6 +1,7 @@ -node_modules -out.js -generated-files .babelrc .eslintrc.js .flowconfig +generated-files +graphiql.css +node_modules +out.js diff --git a/samples/js/graphiql/src/main.js b/samples/js/graphiql/src/main.js index fdb5ded573..c6a1b4b1e1 100644 --- a/samples/js/graphiql/src/main.js +++ b/samples/js/graphiql/src/main.js @@ -9,6 +9,7 @@ import ReactDOM from 'react-dom'; import GraphiQL from 'graphiql'; window.onload = load; +window.onpopstate = load; let params = {}; @@ -21,7 +22,7 @@ function load() { } function loadUnsafe() { - params = getParams(location.href); + params = getParams(location.search); if (!params.db) { renderPrompt('Noms GraphQL Endpoint?'); @@ -31,23 +32,18 @@ function loadUnsafe() { render(); } -function getParams(href: string): {[key: string]: string} { - // This way anything after the # will end up in params, which is what we want. - const paramsIdx = href.indexOf('?'); - if (paramsIdx === -1) { +function getParams(search: string): {[key: string]: string} { + if (search[0] !== '?') { return {}; } - return decodeURIComponent(href.slice(paramsIdx + 1)).split('&').reduce((params, kv) => { - // Make sure that '=' characters are preserved in query values, since '=' is valid base64. - const eqIndex = kv.indexOf('='); - if (eqIndex === -1) { - params[kv] = ''; - } else { - params[kv.slice(0, eqIndex)] = kv.slice(eqIndex + 1); - } - return params; - }, {}); + const params = {}; + for (const param of search.slice(1).split('&')) { + const eq = param.indexOf('='); + params[param.slice(0, eq)] = decodeURIComponent(param.slice(eq + 1)); + } + + return params; } type PromptProps = { @@ -60,6 +56,10 @@ class Prompt extends React.Component { fontFamily: 'Menlo', fontSize: '14px', }; + const headerStyle = { + fontSize: '16px', + fontWeight: 'bold', + }; const divStyle = { alignItems: 'center', display: 'flex', @@ -70,21 +70,45 @@ class Prompt extends React.Component { marginBottom: '0.5em', width: '50ex', }); - const server = 'http://localhost:8000'; + + const defaults = { + db: 'http://localhost:8000', + ds: 'photoindexer/all/run', + endpoint: 'graphql', + }; return
- {this.props.msg} + {this.props.msg}
this._handleOnSubmit(e)}> - - - + + + + + + + + + + +
@@ -93,10 +117,10 @@ class Prompt extends React.Component { _handleOnSubmit(e) { e.preventDefault(); - const qs = ['db', 'ds', 'auth'] + const qs = ['db', 'endpoint', 'ds', 'auth', 'extra'] .map(k => [k, this.refs[k].value]) .filter(([, v]) => !!v) - .map(([k, v]) => `${k}=${v}`) + .map(([k, v]) => `${k}=${encodeURIComponent(v)}`) .join('&'); window.history.pushState({}, undefined, qs === '' || ('?' + qs)); load(); @@ -104,7 +128,7 @@ class Prompt extends React.Component { } function graphQLFetcher(graphQLParams) { - const url = `${params.db}/graphql/`; + const url = `${params.db}/${params.endpoint}`; const headers = new Headers(); headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8'); @@ -112,8 +136,9 @@ function graphQLFetcher(graphQLParams) { headers.append('Authorization', `Bearer ${params.auth}`); } + const extra = params.extra ? ('&' + params.extra) : ''; return fetch(url, { - body: `ds=${params.ds}&query=${encodeURIComponent(graphQLParams.query)}`, + body: `ds=${params.ds}&query=${encodeURIComponent(graphQLParams.query)}${extra}`, headers, method: 'POST', mode: 'cors',