-
Notifications
You must be signed in to change notification settings - Fork 98
Feature/plugin custom graphql scalars #47
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
Changes from 8 commits
0e2b43e
9fe6dd3
025c824
745e400
9c3647a
014ec5b
77348c4
ce1453f
47f6bae
7673dc1
c48ce5f
ae727bd
0180b44
433d702
48ae2f0
7d597f7
ead2b1d
064c579
8062b6b
7674eba
f684f5c
3e99795
93cd44b
437b01d
65c0290
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
|
|
||
| # GraphQL Scalar Input Plugins | ||
|
|
||
| These allow custom handling of custom GraphQL scalars. | ||
|
|
||
| # Definition | ||
|
|
||
| A GraphQL Scalar Input pluging implements the following function signatures: | ||
| ```js | ||
| function canProcess(arg): boolean | ||
|
|
||
| function render(arg, styleConfig, onChangeHandler): void | ||
|
|
||
| export default { | ||
| canProcess, | ||
| render, | ||
| name: "InputHandlerName" | ||
| } | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| See the bundled `DateInput` plugin. | ||
|
|
||
| When instantiating the GraphiQL Explorer, pass the list of desired plugins as the prop `graphqlCustomScalarPlugins`. | ||
|
|
||
| # Usage | ||
|
|
||
| ## Enabling bundled plugins | ||
|
|
||
| By default, these plugins are disabled. To enable the bundled plugins, instantiate the explorer with the prop `enableBundledPlugins` set to `true`. | ||
|
|
||
| ## Example adding custom plugins | ||
|
|
||
| Any number of plugins can be added, and can override existing bundled plugins. | ||
|
|
||
| Plugins are checked in the order they are given in the `graphqlCustomScalarPlugins` list. The first plugin with a `canProcess` value that returns `true` will be used. Bundled plugins are always checked last, after all customised plugins. | ||
|
|
||
| ```js | ||
| // Note this is just an example using a third-party plugin. This is for illustrative purposes only | ||
| const anotherThirdPartyHandler = require('<graphiql-explorer-example-custom-scalar-plugin>') | ||
|
|
||
| const customISODateTypeHandler = { | ||
| render: (arg, styleConfig, onChangeHandler) => ( | ||
| <input | ||
| type="date" | ||
| value={arg.defaultValue} | ||
| onChange={event => { | ||
| // Set the date placed into the query to be an ISO dateTime string | ||
| const isoDate = new Date(event.target.value).toISOString(); | ||
| event.target = { value: isoDate }; | ||
| return onChangeHandler(event); | ||
| }} | ||
| /> | ||
| ), | ||
| canProcess: (arg) => arg && arg.type && arg.type.name === 'Date', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it should be "DateTime" instead of "Date", because you use .toISOString(). |
||
| name: 'customDate', | ||
| } | ||
|
|
||
| const configuredPlugins = [anotherThirdPartyHandler, customISODateTypeHandler] | ||
| ``` | ||
| Then later, in your render method where you create the explorer... | ||
| ``` | ||
| <GraphiQLExplorer | ||
| schema={schema} | ||
| query={query} | ||
| onEdit={this._handleEditQuery} | ||
| onRunOperation={operationName => | ||
| this._graphiql.handleRunQuery(operationName) | ||
| } | ||
| explorerIsOpen={this.state.explorerIsOpen} | ||
| onToggleExplorer={this._handleToggleExplorer} | ||
| getDefaultScalarArgValue={getDefaultScalarArgValue} | ||
| makeDefaultArg={makeDefaultArg} | ||
| enableBundledPlugins={true} | ||
| graphqlCustomScalarPlugins={configuredPlugins} | ||
| /> | ||
| ``` | ||
| > To see examples of instantiating the explorer, see the [example repo](https://github.com/OneGraph/graphiql-explorer-example). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| function canProcess(arg) { | ||
| return arg && arg.type && arg.type.name === 'Date'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it should be "DateTime" instead of "Date", because you use .toISOString().
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The DateInput is distinct from the example in the README which demonstrates adding additional handlers not bundled with the explorer. This bundled plugin is just a direct implementation of the Date input in the POC PR. |
||
| } | ||
|
|
||
| function render(arg, styleConfig, onChangeHandler) { | ||
| return ( | ||
| <input | ||
| type="date" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it should be "datetime-local" instead of "date", because you use .toISOString(). |
||
| value={arg.defaultValue} | ||
| onChange={onChangeHandler} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| const DatePlugin = { | ||
| canProcess, | ||
| render, | ||
| name: 'DateInput' | ||
| }; | ||
|
|
||
| export default DatePlugin; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import DatePlugin from './bundled/DateInput' | ||
|
|
||
| const bundledPlugins = [DatePlugin]; | ||
|
|
||
| class ScalarInputPluginManager { | ||
| constructor(plugins = [], enableBundledPlugins = false) { | ||
| let enabledPlugins = plugins; | ||
| if (enableBundledPlugins) { | ||
| // ensure bundled plugins are the last plugins checked. | ||
| enabledPlugins.push(...bundledPlugins); | ||
| } | ||
| this.plugins = enabledPlugins; | ||
| } | ||
|
|
||
| process(arg, styleConfig, onChangeHandler) { | ||
| // plugins are provided in order, the first matching plugin will be used. | ||
| const handler = this.plugins.find(plugin => plugin.canProcess(arg)); | ||
| if (handler) { | ||
| return handler.render(arg, styleConfig, onChangeHandler); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| export default ScalarInputPluginManager; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it should be
"datetime-local"instead of"date", because you use.toISOString().