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

Add option to enter endpoint URL (fix #116) #148

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions demo/IntrospectionModal.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
flex: 1;
outline: none;
padding: 10px;
margin: 5px 0;
font-size: 14px;
}
}
Expand Down
67 changes: 60 additions & 7 deletions demo/IntrospectionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ import './IntrospectionModal.css';
export interface IntrospectionModalProps {
open: boolean;
onClose: () => void;
onChange: (introspectin: any) => void;
onChange: (introspection: any) => void;
}

const Presets = 'Presets';
const URL = 'URL';
const SDL = 'SDL';
const Introspection = 'Introspection';
const tabNames = [Presets, SDL, Introspection];
const tabNames = [Presets, URL, SDL, Introspection];

const initialConfig = {
inputType: Presets,
activePreset: defaultPresetName,
urlText: null,
headers: null,
sdlText: null,
jsonText: null,
};
Expand Down Expand Up @@ -64,12 +67,27 @@ export class IntrospectionModal extends React.Component<IntrospectionModalProps>
this.props.onClose();
};

fetchIntrospection = (url, headers) => {
return fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
...JSON.parse(headers),
},
body: JSON.stringify({ query: introspectionQuery }),
}).then((response) => response.json());
}

handleSubmit = () => {
const { inputType, activePreset, jsonText, sdlText } = this.state.current;
const { inputType, activePreset, urlText, headers, jsonText, sdlText } = this.state.current;
switch (inputType) {
case Presets:
this.props.onChange(PRESETS[activePreset]);
break;
case URL:
this.fetchIntrospection(urlText, headers).then((data) => this.props.onChange(data));
Copy link
Member

Choose a reason for hiding this comment

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

How UI look like during fetch?
and where is error handling?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

UI looks the same as for the rest "Introspection" and "SDL" tabs. Please note, there is no any error handling for them too. E.g.:

  • If the user has entered invalid JSON or SDL into textarea, there is no any UI toast about JSON parsing or introspection from schema building errors.
  • If the user has not entered JSON or SDL into textarea at all, the "Display" button is still active.

Errors are printed in the browser console.

break;
case Introspection:
this.props.onChange(JSON.parse(jsonText));
break;
Expand All @@ -87,13 +105,25 @@ export class IntrospectionModal extends React.Component<IntrospectionModalProps>
this.changeCurrent({ activePreset });
}

handleURLChange = (event) => {
let urlText = event.target.value;
if (urlText === '') urlText = null;
this.changeCurrent({ urlText });
}

handleHeadersChange = (event) => {
let headers = event.target.value;
if (headers === '') headers = null;
this.changeCurrent({ headers });
}

handleSDLChange = (event) => {
let sdlText = event.target.value;
if (sdlText === '') sdlText = null;
this.changeCurrent({ sdlText });
}

handleJSONChange = (event) => {
handleIntrospectionChange = (event) => {
let jsonText = event.target.value;
if (jsonText === '') jsonText = null;
this.changeCurrent({ jsonText });
Expand All @@ -113,11 +143,13 @@ export class IntrospectionModal extends React.Component<IntrospectionModalProps>
onChange={this.handleTabChange}
>
<Tab label={Presets} />
<Tab label={URL} />
<Tab label={SDL} />
<Tab label={Introspection} />
</Tabs>
<div className="tab-container">
{inputType === Presets && this.renderPresetsTab()}
{inputType === URL && this.renderURLTab()}
{inputType === SDL && this.renderSDLTab()}
{inputType === Introspection && this.renderIntrospectionTab()}
</div>
Expand Down Expand Up @@ -156,12 +188,33 @@ export class IntrospectionModal extends React.Component<IntrospectionModalProps>
);
}

renderURLTab() {
const { urlText, headers } = this.state.current;
return (
<>
<textarea
value={urlText || ''}
placeholder="Paste URL here"
onChange={this.handleURLChange}
/>
<div>
Paste headers (in JSON format) into the textarea below.
Copy link
Member

Choose a reason for hiding this comment

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

Great for PoC but it bad UX in the long run, it would be constant source of frustration for users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe this is an acceptable solution. Please take a look at GraphQL Playground. Users paste headers in GraphQL Playground in JSON format. GraphQL Playground is production-ready tool.

graphqlbin

Also users paste variables in GraphiQL and GraphQL Playground in JSON format too. The target audience of these tools are developers. So I think this is ok.

</div>
<textarea
value={headers || ''}
placeholder="Paste headers here"
onChange={this.handleHeadersChange}
/>
</>
);
}

renderSDLTab() {
const { sdlText } = this.state.current;
return (
<textarea
value={sdlText || ''}
placeholder="Paste SDL Here"
placeholder="Paste SDL here"
onChange={this.handleSDLChange}
/>
);
Expand Down Expand Up @@ -189,8 +242,8 @@ export class IntrospectionModal extends React.Component<IntrospectionModalProps>
</Clipboard>
<textarea
value={jsonText || ''}
placeholder="Paste Introspection Here"
onChange={this.handleJSONChange}
placeholder="Paste Introspection (JSON) here"
onChange={this.handleIntrospectionChange}
/>
</>
);
Expand Down