Skip to content

allow unions to include abstract types #3682

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

Open
wants to merge 1 commit into
base: next
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
2 changes: 1 addition & 1 deletion benchmark/github-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -56058,7 +56058,7 @@
},
{
"name": "UNION",
"description": "Indicates this type is a union. `possibleTypes` is a valid field.",
"description": "Indicates this type is a union. `memberTypes` and `possibleTypes` are valid fields.",
"isDeprecated": false,
"deprecationReason": null
},
Expand Down
4 changes: 2 additions & 2 deletions docs-old/APIReference-TypeSystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,12 @@ class GraphQLUnionType {

type GraphQLUnionTypeConfig = {
name: string,
types: GraphQLObjectsThunk | Array<GraphQLObjectType>,
types: GraphQLCompositesThunk | Array<GraphQLCompositeType>,
resolveType?: (value: any, info?: GraphQLResolveInfo) => ?GraphQLObjectType;
description?: ?string;
};

type GraphQLObjectsThunk = () => Array<GraphQLObjectType>;
type GraphQLCompositesThunk = () => Array<GraphQLCompositeType>;
```

When a field can return one of a heterogeneous set of types, a Union type
Expand Down
13 changes: 7 additions & 6 deletions src/type/__tests__/definition-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,23 +375,24 @@ describe('Type System: Unions', () => {
name: 'SomeUnion',
types: [ObjectType],
});
expect(unionType.getTypes()).to.deep.equal([ObjectType]);
expect(unionType.getMemberTypes()).to.deep.equal([ObjectType]);
expect(unionType.getPossibleTypes()).to.deep.equal([ObjectType]);
});

it('accepts a Union type with function returning an array of types', () => {
const unionType = new GraphQLUnionType({
name: 'SomeUnion',
types: () => [ObjectType],
});
expect(unionType.getTypes()).to.deep.equal([ObjectType]);
expect(unionType.getMemberTypes()).to.deep.equal([ObjectType]);
});

it('accepts a Union type without types', () => {
const unionType = new GraphQLUnionType({
it('accepts a recursive Union type', () => {
const unionType: GraphQLUnionType = new GraphQLUnionType({
name: 'SomeUnion',
types: [],
types: () => [unionType],
});
expect(unionType.getTypes()).to.deep.equal([]);
expect(unionType.getMemberTypes()).to.deep.equal([unionType]);
});

it('rejects an Union type with invalid name', () => {
Expand Down
94 changes: 94 additions & 0 deletions src/type/__tests__/introspection-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('Introspection', () => {
descriptions: false,
specifiedByUrl: true,
directiveIsRepeatable: true,
memberTypes: true,
});

const result = graphqlSync({ schema, source });
Expand Down Expand Up @@ -56,6 +57,7 @@ describe('Introspection', () => {
inputFields: null,
interfaces: [],
enumValues: null,
memberTypes: null,
possibleTypes: null,
},
{
Expand All @@ -66,6 +68,7 @@ describe('Introspection', () => {
inputFields: null,
interfaces: null,
enumValues: null,
memberTypes: null,
possibleTypes: null,
},
{
Expand All @@ -76,6 +79,7 @@ describe('Introspection', () => {
inputFields: null,
interfaces: null,
enumValues: null,
memberTypes: null,
possibleTypes: null,
},
{
Expand Down Expand Up @@ -181,6 +185,7 @@ describe('Introspection', () => {
inputFields: null,
interfaces: [],
enumValues: null,
memberTypes: null,
possibleTypes: null,
},
{
Expand Down Expand Up @@ -284,6 +289,25 @@ describe('Introspection', () => {
isDeprecated: false,
deprecationReason: null,
},
{
name: 'memberTypes',
args: [],
type: {
kind: 'LIST',
name: null,
ofType: {
kind: 'NON_NULL',
name: null,
ofType: {
kind: 'OBJECT',
name: '__Type',
ofType: null,
},
},
},
isDeprecated: false,
deprecationReason: null,
},
{
name: 'possibleTypes',
args: [],
Expand Down Expand Up @@ -376,6 +400,7 @@ describe('Introspection', () => {
inputFields: null,
interfaces: [],
enumValues: null,
memberTypes: null,
possibleTypes: null,
},
{
Expand Down Expand Up @@ -427,6 +452,7 @@ describe('Introspection', () => {
deprecationReason: null,
},
],
memberTypes: null,
possibleTypes: null,
},
{
Expand Down Expand Up @@ -538,6 +564,7 @@ describe('Introspection', () => {
inputFields: null,
interfaces: [],
enumValues: null,
memberTypes: null,
possibleTypes: null,
},
{
Expand Down Expand Up @@ -627,6 +654,7 @@ describe('Introspection', () => {
inputFields: null,
interfaces: [],
enumValues: null,
memberTypes: null,
possibleTypes: null,
},
{
Expand Down Expand Up @@ -690,6 +718,7 @@ describe('Introspection', () => {
inputFields: null,
interfaces: [],
enumValues: null,
memberTypes: null,
possibleTypes: null,
},
{
Expand Down Expand Up @@ -798,6 +827,7 @@ describe('Introspection', () => {
inputFields: null,
interfaces: [],
enumValues: null,
memberTypes: null,
possibleTypes: null,
},
{
Expand Down Expand Up @@ -904,6 +934,7 @@ describe('Introspection', () => {
deprecationReason: null,
},
],
memberTypes: null,
possibleTypes: null,
},
],
Expand Down Expand Up @@ -1612,6 +1643,68 @@ describe('Introspection', () => {
});
});

it('exposes memberTypes for Union types', () => {
const schema = buildSchema(`
union SomeUnion = SomeObject

union AnotherUnion = SomeUnion | SomeObject

type SomeObject {
someField(arg: String): String
}

schema {
query: SomeObject
}
`);

const source = `
{
SomeObject: __type(name: "SomeObject") {
memberTypes {
name
}
possibleTypes {
name
}
}
SomeUnion: __type(name: "SomeUnion") {
memberTypes {
name
}
possibleTypes {
name
}
}
AnotherUnion: __type(name: "AnotherUnion") {
memberTypes {
name
}
possibleTypes {
name
}
}
}
`;

expect(graphqlSync({ schema, source })).to.deep.equal({
data: {
SomeObject: {
memberTypes: null,
possibleTypes: null,
},
SomeUnion: {
memberTypes: [{ name: 'SomeObject' }],
possibleTypes: [{ name: 'SomeObject' }],
},
AnotherUnion: {
memberTypes: [{ name: 'SomeUnion' }, { name: 'SomeObject' }],
possibleTypes: [{ name: 'SomeObject' }],
},
},
});
});

it('executes an introspection query without calling global resolvers', () => {
const schema = buildSchema(`
type Query {
Expand All @@ -1623,6 +1716,7 @@ describe('Introspection', () => {
specifiedByUrl: true,
directiveIsRepeatable: true,
schemaDescription: true,
memberTypes: true,
});

/* c8 ignore start */
Expand Down
Loading