Skip to content

Commit 675f043

Browse files
Add renderVoyager into standalone. Deprecate init. (#316)
1 parent 13b735e commit 675f043

File tree

5 files changed

+29
-28
lines changed

5 files changed

+29
-28
lines changed

README.md

+4-12
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,18 @@ module system it is exported as `GraphQLVoyager` global variable.
4242
- `hideSettings` [`boolean`, default `false`] - hide settings panel
4343
- `hideVoyagerLogo` [`boolean`, default `true`] - hide voyager logo
4444

45-
### `init` function
46-
47-
The signature of the `init` function:
48-
49-
```js
50-
(hostElement: HTMLElement, options: object) => void
51-
```
52-
53-
- `hostElement` - parent element
54-
- `options` - is the JS object with [properties](#properties) of `Voyager` component
55-
5645
## Using pre-bundled version
5746

5847
You can get GraphQL Voyager bundle from the following places:
5948

6049
- [![jsDelivr](https://data.jsdelivr.com/v1/package/npm/graphql-voyager/badge)](https://www.jsdelivr.com/package/npm/graphql-voyager)
61-
- some exact version - https://cdn.jsdelivr.net/npm/graphql-voyager@1.2/dist/voyager.standalone.js
50+
- some exact version - https://cdn.jsdelivr.net/npm/graphql-voyager@1.3/dist/voyager.standalone.js
6251
- latest version - https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.standalone.js
6352
- from `dist` folder of the npm package `graphql-voyager`
6453

54+
**Note: `voyager.standalone.js` is bundled with react, so you just need to call
55+
`renderVoyager` function that's it.**
56+
6557
### [HTML example](./example/cdn)
6658

6759
## Using as a dependency

demo/index.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import * as React from 'react';
2-
import * as ReactDOMClient from 'react-dom/client';
3-
4-
import { Voyager, voyagerIntrospectionQuery } from '../src';
1+
import { renderVoyager, voyagerIntrospectionQuery } from '../src/standalone';
52

63
async function fetchPreset(name: string) {
74
const response = await fetch(`./presets/${name}_introspection.json`);
@@ -32,16 +29,12 @@ Promise.all([
3229
const introspection =
3330
url != null ? fetchIntrospection(url, withCredentials) : defaultPreset;
3431

35-
const rootElement = document.getElementById('root');
36-
const reactRoot = ReactDOMClient.createRoot(rootElement);
37-
reactRoot.render(
38-
React.createElement(Voyager, {
39-
introspection,
40-
introspectionPresets: PRESETS,
41-
allowToChangeSchema: true,
42-
hideVoyagerLogo: false,
43-
}),
44-
);
32+
renderVoyager(document.getElementById('root'), {
33+
introspection,
34+
introspectionPresets: PRESETS,
35+
allowToChangeSchema: true,
36+
hideVoyagerLogo: false,
37+
});
4538
});
4639

4740
async function fetchIntrospection(url: string, withCredentials: string) {

src/index.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import * as ReactDOM from 'react-dom';
22

33
import { Voyager, VoyagerProps } from './components';
44

5+
// FIXME: remove in next breaking version
6+
/** @deprecated please use renderVoyagerPage **/
57
export function init(element: HTMLElement, options: VoyagerProps) {
68
ReactDOM.render(<Voyager {...options} />, element);
79
}
810

9-
export { Voyager } from './components';
11+
export { Voyager, type VoyagerProps } from './components';
12+
1013
// FIXME: remove in next breaking version
1114
export { Voyager as GraphQLVoyager };
1215

src/standalone.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from 'react';
2+
import * as ReactDOMClient from 'react-dom/client';
3+
4+
import { Voyager, type VoyagerProps } from './index';
5+
6+
export function renderVoyager(rootElement: HTMLElement, props: VoyagerProps) {
7+
const reactRoot = ReactDOMClient.createRoot(rootElement);
8+
reactRoot.render(React.createElement(Voyager, props));
9+
}
10+
11+
export * from './index';

webpack.config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default function buildWebpackConfig(env: Env): webpack.Configuration {
2020
if (env.lib === true) {
2121
return {
2222
...baseConfig,
23+
entry: './src/index.tsx',
2324
externals: NodeExternals(),
2425
output: {
2526
...baseConfig.output,
@@ -31,6 +32,7 @@ export default function buildWebpackConfig(env: Env): webpack.Configuration {
3132
if (env.standalone === true) {
3233
return {
3334
...baseConfig,
35+
entry: './src/standalone.ts',
3436
optimization: { minimize: true },
3537
externals: undefined,
3638
output: {
@@ -44,6 +46,7 @@ export default function buildWebpackConfig(env: Env): webpack.Configuration {
4446
if (env.min === true) {
4547
return {
4648
...baseConfig,
49+
entry: './src/index.tsx',
4750
optimization: { minimize: true },
4851
externals: {
4952
react: {
@@ -76,7 +79,6 @@ const baseConfig: webpack.Configuration = {
7679
extensions: ['.ts', '.tsx', '.mjs', '.js', '.json', '.css', '.svg'],
7780
alias: { '../../worker': '../../worker-dist' },
7881
},
79-
entry: './src/index.tsx',
8082
output: {
8183
path: path.resolve(__dirname, 'dist'),
8284
sourceMapFilename: '[file].map',

0 commit comments

Comments
 (0)