Skip to content

[RFC] inputUnion (WIP) #782

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

Closed
wants to merge 3 commits into from
Closed
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
29 changes: 29 additions & 0 deletions example/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
GraphQLSchema,
GraphQLObjectType,
GraphQLUnionType,
GraphQLInputUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLInterfaceType,
Expand Down Expand Up @@ -110,6 +111,31 @@ const TestUnion = new GraphQLUnionType({
},
});

const InputUnionFirst = new GraphQLInputObjectType({
name: 'InputUnionFirst',
fields: () => ({
name: {
type: GraphQLString,
description: 'Common name string for InputUnionFirst.',
},
}),
});

const InputUnionSecond = new GraphQLInputObjectType({
name: 'InputUnionSecond',
fields: () => ({
name: {
type: GraphQLString,
description: 'Common name string for InputUnionFirst.',
},
}),
});

const TestInputUnion = new GraphQLInputUnionType({
name: 'TestInputUnion',
types: [InputUnionFirst, InputUnionSecond],
});

const TestType = new GraphQLObjectType({
name: 'Test',
fields: () => ({
Expand Down Expand Up @@ -153,6 +179,8 @@ const TestType = new GraphQLObjectType({
id: { type: GraphQLID },
enum: { type: TestEnum },
object: { type: TestInputObject },
union: { type: TestInputUnion },

// List
listString: { type: new GraphQLList(GraphQLString) },
listInt: { type: new GraphQLList(GraphQLInt) },
Expand All @@ -161,6 +189,7 @@ const TestType = new GraphQLObjectType({
listID: { type: new GraphQLList(GraphQLID) },
listEnum: { type: new GraphQLList(TestEnum) },
listObject: { type: new GraphQLList(TestInputObject) },
listUnion: { type: new GraphQLList(TestInputUnion) },
},
},
}),
Expand Down
4 changes: 2 additions & 2 deletions resources/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ fi
rm -rf dist/ && mkdir -p dist/
babel src --ignore __tests__ --out-dir dist/
echo "Bundling graphiql.js..."
browserify -g browserify-shim -s GraphiQL dist/index.js > graphiql.js
browserify -s GraphiQL dist/index.js > graphiql.js
echo "Bundling graphiql.min.js..."
browserify -g browserify-shim -t uglifyify -s GraphiQL dist/index.js 2> /dev/null | uglifyjs -c > graphiql.min.js 2> /dev/null
browserify -t uglifyify -s GraphiQL dist/index.js 2> /dev/null | uglifyjs -c > graphiql.min.js 2> /dev/null
echo "Bundling graphiql.css..."
postcss --no-map --use autoprefixer -d dist/ css/*.css
cat dist/*.css > graphiql.css
Expand Down
98 changes: 50 additions & 48 deletions src/components/DocExplorer/TypeDoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLInputUnionType,
GraphQLEnumType,
} from 'graphql';

Expand Down Expand Up @@ -49,9 +50,13 @@ export default class TypeDoc extends React.Component {

let typesTitle;
let types;

if (type instanceof GraphQLUnionType) {
typesTitle = 'possible types';
types = schema.getPossibleTypes(type);
} else if (type instanceof GraphQLInputUnionType) {
typesTitle = 'possible input types';
types = schema.getPossibleTypes(type);
} else if (type instanceof GraphQLInterfaceType) {
typesTitle = 'implementations';
types = schema.getPossibleTypes(type);
Expand All @@ -65,11 +70,11 @@ export default class TypeDoc extends React.Component {
typesDef = (
<div className="doc-category">
<div className="doc-category-title">{typesTitle}</div>
{types.map(subtype => (
{types.map(subtype =>
<div key={subtype.name} className="doc-category-item">
<TypeLink type={subtype} onClick={onClickType} />
</div>
))}
</div>,
)}
</div>
);
}
Expand All @@ -85,15 +90,15 @@ export default class TypeDoc extends React.Component {
<div className="doc-category-title">{'fields'}</div>
{fields
.filter(field => !field.isDeprecated)
.map(field => (
.map(field =>
<Field
key={field.name}
type={type}
field={field}
onClickType={onClickType}
onClickField={onClickField}
/>
))}
/>,
)}
</div>
);

Expand All @@ -102,21 +107,21 @@ export default class TypeDoc extends React.Component {
deprecatedFieldsDef = (
<div className="doc-category">
<div className="doc-category-title">{'deprecated fields'}</div>
{!this.state.showDeprecated ? (
<button className="show-btn" onClick={this.handleShowDeprecated}>
{'Show deprecated fields...'}
</button>
) : (
deprecatedFields.map(field => (
<Field
key={field.name}
type={type}
field={field}
onClickType={onClickType}
onClickField={onClickField}
/>
))
)}
{!this.state.showDeprecated
? <button
className="show-btn"
onClick={this.handleShowDeprecated}>
{'Show deprecated fields...'}
</button>
: deprecatedFields.map(field =>
<Field
key={field.name}
type={type}
field={field}
onClickType={onClickType}
onClickField={onClickField}
/>,
)}
</div>
);
}
Expand All @@ -140,15 +145,15 @@ export default class TypeDoc extends React.Component {
deprecatedValuesDef = (
<div className="doc-category">
<div className="doc-category-title">{'deprecated values'}</div>
{!this.state.showDeprecated ? (
<button className="show-btn" onClick={this.handleShowDeprecated}>
{'Show deprecated values...'}
</button>
) : (
deprecatedValues.map(value => (
<EnumValue key={value.name} value={value} />
))
)}
{!this.state.showDeprecated
? <button
className="show-btn"
onClick={this.handleShowDeprecated}>
{'Show deprecated values...'}
</button>
: deprecatedValues.map(value =>
<EnumValue key={value.name} value={value} />,
)}
</div>
);
}
Expand Down Expand Up @@ -182,30 +187,28 @@ function Field({ type, field, onClickType, onClickField }) {
{field.name}
</a>
{field.args &&
field.args.length > 0 && [
'(',
<span key="args">
{field.args.map(arg => (
<Argument key={arg.name} arg={arg} onClickType={onClickType} />
))}
</span>,
')',
]}
field.args.length > 0 && [
'(',
<span key="args">
{field.args.map(arg =>
<Argument key={arg.name} arg={arg} onClickType={onClickType} />,
)}
</span>,
')',
]}
{': '}
<TypeLink type={field.type} onClick={onClickType} />
<DefaultValue field={field} />
{field.description && (
{field.description &&
<MarkdownContent
className="field-short-description"
markdown={field.description}
/>
)}
{field.deprecationReason && (
/>}
{field.deprecationReason &&
<MarkdownContent
className="doc-deprecation"
markdown={field.deprecationReason}
/>
)}
/>}
</div>
);
}
Expand All @@ -225,12 +228,11 @@ function EnumValue({ value }) {
className="doc-value-description"
markdown={value.description}
/>
{value.deprecationReason && (
{value.deprecationReason &&
<MarkdownContent
className="doc-deprecation"
markdown={value.deprecationReason}
/>
)}
/>}
</div>
);
}
Expand Down
53 changes: 53 additions & 0 deletions test/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
GraphQLSchema,
GraphQLObjectType,
GraphQLUnionType,
GraphQLInputUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLInterfaceType,
Expand Down Expand Up @@ -128,6 +129,48 @@ const TestUnion = new GraphQLUnionType({
},
});

const InputUnionFirst = new GraphQLObjectType({
name: 'First',
fields: () => ({
name: {
type: GraphQLString,
description: 'Common name string for InputUnionFirst.',
},
first: {
type: new GraphQLList(TestInterface),
resolve: () => {
return true;
},
},
}),
interfaces: [TestInterface],
});

const InputUnionSecond = new GraphQLObjectType({
name: 'First',
fields: () => ({
name: {
type: GraphQLString,
description: 'Common name string for InputUnionSecond.',
},
first: {
type: new GraphQLList(TestInterface),
resolve: () => {
return true;
},
},
}),
interfaces: [TestInterface],
});

const TestInputUnion = new GraphQLInputUnionType({
name: 'TestInputUnion',
types: [InputUnionFirst, InputUnionSecond],
resolveType() {
return InputUnionFirst;
},
});

const TestType = new GraphQLObjectType({
name: 'Test',
fields: () => ({
Expand All @@ -148,6 +191,16 @@ const TestType = new GraphQLObjectType({
description: '> union field from Test type, block-quoted.',
resolve: () => ({}),
},
inputUnion: {
type: TestInputUnion,
description: `
input union field from Test type, with unordered list:
- how
- about
- that
`,
resolve: () => ({}),
},
id: {
type: GraphQLID,
description: 'id field from Test type.',
Expand Down
3 changes: 1 addition & 2 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import express from 'express';
import path from 'path';
import browserify from 'browserify';
import browserifyShim from 'browserify-shim';
import watchify from 'watchify';
import babelify from 'babelify';
import graphqlHTTP from 'express-graphql';
Expand All @@ -28,7 +27,7 @@ const b = browserify({
entries: [path.join(__dirname, '../src/index.js')],
cache: {},
packageCache: {},
transform: [babelify, browserifyShim],
transform: [babelify],
plugin: [watchify],
standalone: 'GraphiQL',
globalTransform: 'browserify-shim',
Expand Down