Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce monaco editor to GraphiQL #17

Merged
merged 9 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if (process.env.ESM) {
}

if (process.env.CDN) {
envConfig.modules = 'umd';
envConfig.modules = 'amd';
envConfig.targets = null;
envConfig.ignoreBrowserslistConfig = false;
}
Expand All @@ -28,6 +28,9 @@ module.exports = {
test: {
plugins: [require.resolve('babel-plugin-macros')],
},
development: {
compact: false,
},
},
plugins: [
require.resolve('@babel/plugin-proposal-class-properties'),
Expand Down
122 changes: 122 additions & 0 deletions examples/graphiql-cdn-subscriptions/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<!--
* Copyright (c) 2019 GraphQL Contributors
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright (c) 2019 GraphQL Contributors
* Copyright (c) 2020 GraphQL Contributors

😛

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha, nice!

* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
-->
<!DOCTYPE html>
<html>
<head>
<style>
body {
height: 100%;
margin: 0;
width: 100%;
overflow: hidden;
}

#graphiql {
height: 100vh;
}
</style>

<!--
This GraphiQL example depends on Promise and fetch, which are available in
modern browsers, but can be "polyfilled" for older browsers.
GraphiQL itself depends on React DOM.
If you do not want to rely on a CDN, you can host these files locally or
include them directly in your favored resource bunder.
-->
<script
crossorigin
src="https://unpkg.com/react@16/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"
></script>
<script crossorigin src="//unpkg.com/graphql@15.0.0"></script>
<script
crossorigin
src="https://unpkg.com/graphiql/graphiql.min.js"
></script>

<!--
These two files can be found in the npm module, however you may wish to
copy them directly into your environment, or perhaps include them in your
favored resource bundler.
-->
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" />
</head>

<body>
<div id="graphiql">Loading...</div>
<script>
const mockSubcriptionClient = {
subscribe: () => {},
unsubscribe: () => {},
};

const hasSubscriptionOperation = graphQlParams => {
const queryDoc = parse(graphQlParams.query);

for (let definition of queryDoc.definitions) {
if (definition.kind === 'OperationDefinition') {
const operation = definition.operation;
if (operation === 'subscription') {
return true;
}
}
}

return false;
};

export const graphQLFetcher = (subscriptionsClient, fallbackFetcher) => {
let activeSubscription = null;

return graphQLParams => {
if (subscriptionsClient && activeSubscription !== null) {
subscriptionsClient.unsubscribe();
}

if (subscriptionsClient && hasSubscriptionOperation(graphQLParams)) {
return {
subscribe: observer => {
observer.next(
'Your subscription data will appear here after server publication!',
);

subscriptionsClient.subscribe(
{
query: graphQLParams.query,
variables: graphQLParams.variables,
},
function (error, result) {
if (error) {
observer.error(error);
} else {
observer.next(result);
}
},
);
activeSubscription = true;
},
};
} else {
return fallbackFetcher(graphQLParams);
}
};
};

ReactDOM.render(
React.createElement(GraphiQL, {
fetcher: graphQLFetcher,
defaultVariableEditorOpen: true,
}),
document.getElementById('graphiql'),
);
</script>
</body>
</html>
56 changes: 56 additions & 0 deletions examples/graphiql-cdn-subscriptions/subscriptionsExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { SubscriptionClient } from 'subscriptions-transport-ws';
import { parse } from 'graphql';

const hasSubscriptionOperation = (graphQlParams: any) => {
const queryDoc = parse(graphQlParams.query);

for (const definition of queryDoc.definitions) {
if (definition.kind === 'OperationDefinition') {
const operation = definition.operation;
if (operation === 'subscription') {
return true;
}
}
}

return false;
};

export const graphQLFetcher = (
subscriptionsClient: SubscriptionClient,
fallbackFetcher: Function,
) => {
let activeSubscriptionId: number | null = null;

return (graphQLParams: any) => {
if (subscriptionsClient && activeSubscriptionId !== null) {
subscriptionsClient.unsubscribe(activeSubscriptionId);
}

if (subscriptionsClient && hasSubscriptionOperation(graphQLParams)) {
return {
subscribe: (observer: { error: Function, next: Function }) => {
observer.next(
'Your subscription data will appear 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);
}
},
);
},
};
} else {
return fallbackFetcher(graphQLParams);
}
};
};
24 changes: 19 additions & 5 deletions examples/monaco-graphql-webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js';

import 'regenerator-runtime/runtime';
import 'monaco-graphql/esm/monaco.contribution';
// / <reference path='monaco-graphql/esm/typings/monaco.d.ts'/>

// NOTE: using loader syntax becuase Yaml worker imports editor.worker directly and that
// import shouldn't go through loader syntax.
Expand All @@ -12,7 +13,7 @@ import JSONWorker from 'worker-loader!monaco-editor/esm/vs/language/json/json.wo
// @ts-ignore
import GraphQLWorker from 'worker-loader!monaco-graphql/esm/graphql.worker';

const SCHEMA_URL = 'https://swapi-graphql.netlify.app/.netlify/functions/index';
const SCHEMA_URL = 'https://api.spacex.land/graphql/';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about this one! :D


// @ts-ignore
window.MonacoEnvironment = {
Expand Down Expand Up @@ -51,22 +52,31 @@ const resultsEditor = monaco.editor.create(
document.getElementById('results') as HTMLElement,
{
model: variablesModel,
automaticLayout: true,
},
);
const variablesEditor = monaco.editor.create(
document.getElementById('variables') as HTMLElement,
{
value: `{ }`,
language: 'json',
automaticLayout: true,
},
);
const model = monaco.editor.createModel(
`
query Example {
allFilms {
films {
id
}
launchesPast(limit: 10) {
mission_name
# format me using the right click context menu
launch_date_local
launch_site {
site_name_long
}
links {
article_link
video_link
}
}
}
`,
Expand All @@ -78,9 +88,13 @@ const operationEditor = monaco.editor.create(
document.getElementById('operation') as HTMLElement,
{
model,
automaticLayout: true,
},
);

// @ts-ignore
monaco.languages.graphql.graphqlDefaults.setSchemaUri(SCHEMA_URL);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cshaver if we do this inside schema context, then it will reload schema and validations, completion, etc everything!!!


/**
* Basic Operation Exec Example
*/
Expand Down
18 changes: 10 additions & 8 deletions packages/graphiql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@
"build": "yarn build-clean && yarn build-cjs && yarn build-esm",
"build-cjs": "tsc",
"build-esm": "tsc --project ./tsconfig.esm.json",
"build-clean": "rimraf esm dist webpack *.html",
"build-clean": "rimraf webpack *.html bundle",
"build-bundles": "yarn build-bundles-clean && yarn build-bundles-dev && yarn build-bundles-min",
"build-bundles-dev": "cross-env NODE_ENV=development CDN=1 yarn webpack -d --bail",
"build-bundles-min": "cross-env ANALYZE=1 NODE_ENV=production CDN=1 yarn webpack -p --bail",
"build-bundles-dev": "cross-env NODE_ENV=development CDN=1 yarn webpack -d --bail && copy 'resources/renderExample.js' bundle/dev/",
"build-bundles-min": "cross-env ANALYZE=1 NODE_ENV=production CDN=1 yarn webpack -p --bail && copy 'resources/renderExample.js' bundle/dist/",
"build-bundles-clean": "rimraf 'graphiql.*{js,css}' *.html",
"build-demo": "build-storybook -o ./storybook",
"check": "tsc --noEmit",
"dev": "cross-env NODE_ENV=development webpack-dev-server --config resources/webpack.config.js",
"cypress-open": "yarn e2e-server 'cypress open'",
"e2e": "yarn e2e-server 'cypress run'",
Expand All @@ -45,10 +44,11 @@
"@emotion/core": "^10.0.28",
"@mdx-js/react": "^1.5.2",
"codemirror": "^5.52.2",
"codemirror-graphql": "^0.12.0-alpha.7",
"copy-to-clipboard": "^3.2.0",
"entities": "^2.0.0",
"markdown-it": "^10.0.0",
"monaco-graphql": "^2.3.4-alpha.4",
"monaco-editor": "^0.20.0",
"regenerator-runtime": "^0.13.5",
"theme-ui": "^0.3.1"
},
Expand All @@ -70,7 +70,7 @@
"babel-loader": "^8.1.0",
"babel-plugin-macros": "^2.8.0",
"cross-env": "^7.0.0",
"css-loader": "3.4.2",
"css-loader": "^3.5.1",
"cssnano": "^4.1.10",
"error-overlay-webpack-plugin": "^0.4.1",
"express": "4.17.1",
Expand All @@ -82,6 +82,7 @@
"jest": "^24.8.0",
"jsdom": "16.2.0",
"mini-css-extract-plugin": "^0.9.0",
"monaco-editor-webpack-plugin": "^1.9.0",
"postcss-import": "^12.0.1",
"postcss-loader": "^3.0.0",
"postcss-preset-env": "^6.7.0",
Expand All @@ -95,11 +96,12 @@
"serve": "^11.3.0",
"start-server-and-test": "^1.10.11",
"style-loader": "^1.1.3",
"ts-loader": "^6.2.2",
"ts-loader": "^7.0.0",
"typescript": "3.8.3",
"webpack": "4.42.1",
"webpack-bundle-analyzer": "^3.6.1",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
"webpack-dev-server": "^3.10.3",
"worker-loader": "^2.0.0"
}
}
46 changes: 25 additions & 21 deletions packages/graphiql/resources/index.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,41 @@
-->
<!DOCTYPE html>
<html>
<head>
<style>
body {
height: 100%;
margin: 0;
width: 100%;
overflow: hidden;
}
#graphiql {
height: 100vh;
}
</style>

<!--
<head>
<style>
body {
height: 100%;
margin: 0;
width: 100%;
overflow: hidden;
}

#graphiql {
height: 100vh;
}
</style>

<!--
This GraphiQL example depends on Promise and fetch, which are available in
modern browsers, but can be "polyfilled" for older browsers.
GraphiQL itself depends on React DOM.
If you do not want to rely on a CDN, you can host these files locally or
include them directly in your favored resource bunder.
-->
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>

<!--
<!--
These two files can be found in the npm module, however you may wish to
copy them directly into your environment, or perhaps include them in your
favored resource bundler.
-->
</head>
<body>
<div id="graphiql">Loading...</div>
<script defer src="/resources/renderExample.js" type="application/javascript"></script>
</body>
</head>

<body>
<div id="graphiql">Loading...</div>
<script defer src="resources/renderExample.js" type="application/javascript"></script>
</body>

</html>
Loading