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

[Feature] Add possibility to listen on displayOptions changes #142

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module system it is exported as `GraphQLVoyager` global variable.
+ `displayOptions.sortByAlphabet` [`boolean`, default `false`] - sort fields on graph by alphabet
+ `displayOptions.showLeafFields` [`boolean`, default `true`] - show all scalars and enums
+ `displayOptions.hideRoot` [`boolean`, default `false`] - hide the root type
+ `onDisplayOptionsChange` [function: `(displayOptions) => void`] _(optional)_ - called when user change displayOptions with UI. Can be used to save settings into localstorage or query params
+ `hideDocs` [`boolean`, default `false`] - hide the docs sidebar
+ `hideSettings` [`boolean`, default `false`] - hide settings panel
+ `workerURI` [`string`] _(optional)_ - absolute or relative path to Voyager web worker. By default it will try to load it from `voyager.worker.js`.
Expand Down
36 changes: 34 additions & 2 deletions demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@ export default class Demo extends React.Component {
introspection: defaultPreset,
};

// not part of state because we parse it in constructor and just use in render
displayOptions = {};

constructor(props) {
super(props);

const { url, withCredentials } = getQueryParams();
const { url, withCredentials, ...restQueryParams } = getQueryParams();
// parse rest of query params we expect them to be in JSON format
// we might pick also some options that are not valid displayOptions but they will be ignored
// by Voyager component
for (const [key, value] of Object.entries(restQueryParams)) {
try {
this.displayOptions[key] = JSON.parse(value);
} catch (_) {
console.log(
`Not expected value for key "${key}" or value >${value}< is not in json format so just ignoring it`,
);
}
}
if (url) {
this.state.introspection = introspectionQuery =>
fetch(url, {
Expand All @@ -45,7 +60,11 @@ export default class Demo extends React.Component {

return (
<MuiThemeProvider theme={theme}>
<GraphQLVoyager introspection={introspection}>
<GraphQLVoyager
introspection={introspection}
displayOptions={this.displayOptions}
onDisplayOptionsChange={setQueryParamsWithoutRefresh}
>
<GraphQLVoyager.PanelHeader>
<div className="voyager-panel">
<Logo />
Expand All @@ -71,6 +90,19 @@ export default class Demo extends React.Component {
}
}

function setQueryParamsWithoutRefresh(displayOptions) {
if ('URLSearchParams' in window) {
const searchParams = new URLSearchParams(window.location.search);
for (const [key, value] of Object.entries(displayOptions)) {
searchParams.set(key, JSON.stringify(value));
}
var newRelativePathQuery = window.location.pathname + '?' + searchParams.toString();
history.pushState(null, '', newRelativePathQuery);
} else {
console.log('Missing URLSearchParams so just printing new displayOptions', displayOptions);
}
}

function getQueryParams(): { [key: string]: string } {
const query = window.location.search.substring(1);
const params = {};
Expand Down
5 changes: 5 additions & 0 deletions src/components/Voyager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function normalizeDisplayOptions(options) {
export interface VoyagerProps {
introspection: IntrospectionProvider | Object;
displayOptions?: VoyagerDisplayOptions;
onDisplayOptionsChange?: (displayOptions: VoyagerDisplayOptions) => void;
hideDocs?: boolean;
hideSettings?: boolean;
workerURI?: string;
Expand All @@ -64,6 +65,7 @@ export default class Voyager extends React.Component<VoyagerProps> {
hideRoot: PropTypes.bool,
showLeafFields: PropTypes.bool,
}),
onDisplayOptionsChange: PropTypes.func,
hideDocs: PropTypes.bool,
hideSettings: PropTypes.bool,
workerURI: PropTypes.string,
Expand Down Expand Up @@ -232,6 +234,9 @@ export default class Voyager extends React.Component<VoyagerProps> {

handleDisplayOptionsChange = delta => {
const displayOptions = { ...this.state.displayOptions, ...delta };
if (this.props.onDisplayOptionsChange) {
this.props.onDisplayOptionsChange(displayOptions);
}
this.updateIntrospection(this.state.introspectionData, displayOptions);
};

Expand Down