-
Notifications
You must be signed in to change notification settings - Fork 98
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/plugin custom graphql scalars #47
Closed
TimLehner
wants to merge
25
commits into
OneGraph:master
from
agrimetrics:feature/plugin-custom-graphql-scalars
Closed
Changes from 17 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
0e2b43e
add a 'deprecated' class to fields which have been marked as such
TimLehner 9fe6dd3
Merge branch 'master' of https://github.com/OneGraph/graphiql-explorer
TimLehner 025c824
Refactor existing behaviour into seperate method
TimLehner 745e400
Introduce custom scalar input plugin manager
TimLehner 9c3647a
Create example DateInput plugin based on PR #46
TimLehner 014ec5b
Only enable bundled plugins if requested.
TimLehner 77348c4
update readme for custom-graphql-scalars plugin
TimLehner ce1453f
[bugfix] only disable the bundled plugins
TimLehner 47f6bae
typo
TimLehner 7673dc1
Date -> DateTime
TimLehner c48ce5f
[bugfix] if manager is null explorer could break
TimLehner ae727bd
show checkbox if rendered by plugin
TimLehner 0180b44
Add support for complex types
TimLehner 433d702
update example inputs when query text changes
TimLehner 48ae2f0
disable example input plugin if not selected
TimLehner 7d597f7
typo
TimLehner ead2b1d
update plugin readme
TimLehner 064c579
Merge changes from master
TimLehner 8062b6b
Move everything into Explorer.js following review
TimLehner 7674eba
Merge branch 'master' of https://github.com/OneGraph/graphiql-explore…
TimLehner f684f5c
fix broken merge
TimLehner 3e99795
Merge remote-tracking branch 'upstream/master'
dboakes-ag 93cd44b
Merge branch 'master' into feature/plugin-custom-graphql-scalars
dboakes-ag 437b01d
[sc-28504] Bump version and ensure package.json has agrimetrics in na…
dboakes-ag 65c0290
[sc-28504] Fix ScalarInputPluginManager handler and bump version
dboakes-ag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
|
||
# GraphQL Scalar Input Plugins | ||
|
||
These allow custom handling of custom GraphQL scalars. | ||
|
||
# Definition | ||
|
||
A GraphQL Scalar Input plugin must implement the following: | ||
```js | ||
function canProcess(arg): Boolean! | ||
function render(prop): React.Element! | ||
|
||
const name: String! | ||
``` | ||
|
||
# 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`. | ||
|
||
## Examples | ||
|
||
### Date Input | ||
|
||
See the bundled `DateInput` plugin, which demonstrates a simple implementation for a single GraphQL Scalar. | ||
|
||
When instantiating the GraphiQL Explorer, pass the list of desired plugins as the prop `graphqlCustomScalarPlugins`. | ||
|
||
### Complex Numbers | ||
|
||
This examples shows a plugin that can be used for more complicated input types which should form objects. | ||
|
||
For this example, consider the following schema: | ||
``` | ||
type ComplexNumber { | ||
real: Float! | ||
imaginary: Float! | ||
} | ||
|
||
input ComplexNumberInput { | ||
real: Float! | ||
imaginary: Float! | ||
} | ||
|
||
type Query { | ||
complexCalculations(z: ComplexNumberInput): ComplexResponse! | ||
} | ||
|
||
type ComplexResponse { | ||
real: Float! | ||
imaginary: Float! | ||
length: Float! | ||
complexConjugate: ComplexNumber! | ||
} | ||
``` | ||
|
||
The custom object type can be handled with a custom plugin. The file `ComplexNumberHandler.js` shows an example implementation for the `ComplexNumberInput`. | ||
|
||
```js | ||
import * as React from "react"; | ||
|
||
class ComplexNumberHandler extends React.Component { | ||
static canProcess = (arg) => arg && arg.type && arg.type.name === 'ComplexNumberInput'; | ||
|
||
updateComponent(arg, value, targetName) { | ||
const updatedFields = arg.value.fields.map(childArg => { | ||
if (childArg.name.value !== targetName) { | ||
return childArg; | ||
} | ||
const updatedChild = { ...childArg }; | ||
updatedChild.value.value = value; | ||
return updatedChild; | ||
}); | ||
|
||
const mappedArg = { ...arg }; | ||
mappedArg.value.fields = updatedFields; | ||
return mappedArg; | ||
} | ||
|
||
handleChangeEvent(event, complexComponent) { | ||
const { arg, selection, modifyArguments, argValue } = this.props; | ||
return modifyArguments(selection.arguments.map(originalArg => { | ||
if (originalArg.name.value !== arg.name) { | ||
return originalArg; | ||
} | ||
|
||
return this.updateComponent(originalArg, event.target.value, complexComponent); | ||
})); | ||
} | ||
|
||
getValue(complexArg, complexComponent) { | ||
const childNode = complexArg && complexArg.value.fields.find(childArg => childArg.name.value === complexComponent) | ||
|
||
if (complexArg && childNode) { | ||
return childNode.value.value; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
render() { | ||
const { selection, arg } = this.props; | ||
const selectedComplexArg = (selection.arguments || []).find(a => a.name.value === arg.name); | ||
const rePart = this.getValue(selectedComplexArg, 'real'); | ||
const imPart = this.getValue(selectedComplexArg, 'imaginary'); | ||
return ( | ||
<span> | ||
<input | ||
type="number" | ||
value={rePart} | ||
onChange={e => this.handleChangeEvent(e, 'real')} | ||
style={{ maxWidth: '50px', margin: '5px' }} | ||
step='any' | ||
disabled={!selectedComplexArg} | ||
/> | ||
+ | ||
<input | ||
type="number" | ||
defaultValue={imPart} | ||
onChange={e => this.handleChangeEvent(e, 'imaginary')} | ||
style={{ maxWidth: '50px', margin: '5px' }} | ||
step='any' | ||
disabled={!selectedComplexArg} | ||
/> i | ||
</span>); | ||
} | ||
} | ||
|
||
export default { | ||
canProcess: ComplexNumberHandler.canProcess, | ||
name: 'Complex Number', | ||
render: props => ( | ||
<ComplexNumberHandler | ||
{...props} | ||
/>), | ||
} | ||
``` | ||
To add the custom plugin, pass it to the GraphiQLExplorer on instantiation. | ||
```js | ||
import ComplexNumberHandler from './ComplexNumberHandler'; | ||
const configuredPlugins = [ComplexNumberHandler]; | ||
|
||
// Then later, in your render method where you create the explorer... | ||
<GraphiQLExplorer | ||
... | ||
graphqlCustomScalarPlugins={configuredPlugins} | ||
/> | ||
``` | ||
> To see examples of instantiating the explorer, see the [example repo](https://github.com/OneGraph/graphiql-explorer-example). | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
|
||
function render(props) { | ||
return ( | ||
<input | ||
type="date" | ||
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={props.arg.defaultValue} | ||
onChange={props.setArgValue} | ||
/> | ||
); | ||
} | ||
|
||
|
||
const DatePlugin = { | ||
canProcess, | ||
render, | ||
name: 'DateInput' | ||
}; | ||
|
||
export default DatePlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(props) { | ||
// plugins are provided in order, the first matching plugin will be used. | ||
const handler = this.plugins.find(plugin => plugin.canProcess(props.arg)); | ||
if (handler) { | ||
return handler.render(props); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
export default ScalarInputPluginManager; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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" instead of "Date", because you use .toISOString().
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.
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.