Skip to content

Commit

Permalink
Schema: keep order of user provided types (#2410)
Browse files Browse the repository at this point in the history
Motivation: #2362
  • Loading branch information
IvanGoncharov authored Jan 29, 2020
1 parent 6652178 commit 68a0818
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 136 deletions.
8 changes: 4 additions & 4 deletions src/__tests__/starWarsIntrospection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ describe('Star Wars Introspection Tests', () => {
expect(data).to.deep.equal({
__schema: {
types: [
{ name: 'Query' },
{ name: 'Episode' },
{ name: 'Human' },
{ name: 'Character' },
{ name: 'String' },
{ name: 'Human' },
{ name: 'Episode' },
{ name: 'Droid' },
{ name: 'Query' },
{ name: 'Boolean' },
{ name: '__Schema' },
{ name: '__Type' },
{ name: '__TypeKind' },
{ name: 'Boolean' },
{ name: '__Field' },
{ name: '__InputValue' },
{ name: '__EnumValue' },
Expand Down
4 changes: 2 additions & 2 deletions src/execution/__tests__/union-interface-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('Execute: Union and intersection types', () => {
name: 'Named',
fields: [{ name: 'name' }],
interfaces: [],
possibleTypes: [{ name: 'Person' }, { name: 'Dog' }, { name: 'Cat' }],
possibleTypes: [{ name: 'Dog' }, { name: 'Cat' }, { name: 'Person' }],
enumValues: null,
inputFields: null,
},
Expand All @@ -208,7 +208,7 @@ describe('Execute: Union and intersection types', () => {
name: 'Mammal',
fields: [{ name: 'progeny' }, { name: 'mother' }, { name: 'father' }],
interfaces: [{ name: 'Life' }],
possibleTypes: [{ name: 'Person' }, { name: 'Dog' }, { name: 'Cat' }],
possibleTypes: [{ name: 'Dog' }, { name: 'Cat' }, { name: 'Person' }],
enumValues: null,
inputFields: null,
},
Expand Down
18 changes: 9 additions & 9 deletions src/type/__tests__/introspection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ describe('Introspection', () => {
enumValues: null,
possibleTypes: null,
},
{
kind: 'SCALAR',
name: 'Boolean',
fields: null,
inputFields: null,
interfaces: null,
enumValues: null,
possibleTypes: null,
},
{
kind: 'OBJECT',
name: '__Schema',
Expand Down Expand Up @@ -385,15 +394,6 @@ describe('Introspection', () => {
],
possibleTypes: null,
},
{
kind: 'SCALAR',
name: 'Boolean',
fields: null,
inputFields: null,
interfaces: null,
enumValues: null,
possibleTypes: null,
},
{
kind: 'OBJECT',
name: '__Field',
Expand Down
51 changes: 51 additions & 0 deletions src/type/__tests__/schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,57 @@ describe('Type System: Schema', () => {
});
});

it('preserve order of use provided types', () => {
const aType = new GraphQLObjectType({
name: 'A',
fields: {
sub: { type: new GraphQLScalarType({ name: 'ASub' }) },
},
});
const zType = new GraphQLObjectType({
name: 'Z',
fields: {
sub: { type: new GraphQLScalarType({ name: 'ZSub' }) },
},
});
const queryType = new GraphQLObjectType({
name: 'Query',
fields: {
a: { type: aType },
z: { type: zType },
sub: { type: new GraphQLScalarType({ name: 'QuerySub' }) },
},
});
const schema = new GraphQLSchema({
types: [zType, queryType, aType],
query: queryType,
});

const typeNames = Object.keys(schema.getTypeMap());
expect(typeNames).to.deep.equal([
'Z',
'ZSub',
'Query',
'QuerySub',
'A',
'ASub',
'Boolean',
'String',
'__Schema',
'__Type',
'__TypeKind',
'__Field',
'__InputValue',
'__EnumValue',
'__Directive',
'__DirectiveLocation',
]);

// Also check that this order is stable
const copySchema = new GraphQLSchema(schema.toConfig());
expect(Object.keys(copySchema.getTypeMap())).to.deep.equal(typeNames);
});

it('can be Object.toStringified', () => {
const schema = new GraphQLSchema({});

Expand Down
16 changes: 8 additions & 8 deletions src/type/__tests__/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1330,11 +1330,11 @@ describe('Type System: Interface fields must have output types', () => {
expect(validateSchema(schema)).to.deep.equal([
{
message:
'The type of BadInterface.badField must be Output Type but got: undefined.',
'The type of BadImplementing.badField must be Output Type but got: undefined.',
},
{
message:
'The type of BadImplementing.badField must be Output Type but got: undefined.',
'The type of BadInterface.badField must be Output Type but got: undefined.',
},
]);
});
Expand All @@ -1346,10 +1346,10 @@ describe('Type System: Interface fields must have output types', () => {
const schema = schemaWithInterfaceFieldOfType(type);
expect(validateSchema(schema)).to.deep.equal([
{
message: `The type of BadInterface.badField must be Output Type but got: ${typeStr}.`,
message: `The type of BadImplementing.badField must be Output Type but got: ${typeStr}.`,
},
{
message: `The type of BadImplementing.badField must be Output Type but got: ${typeStr}.`,
message: `The type of BadInterface.badField must be Output Type but got: ${typeStr}.`,
},
]);
});
Expand All @@ -1361,14 +1361,14 @@ describe('Type System: Interface fields must have output types', () => {
expect(validateSchema(schema)).to.deep.equal([
{
message:
'The type of BadInterface.badField must be Output Type but got: [function Number].',
'The type of BadImplementing.badField must be Output Type but got: [function Number].',
},
{
message: 'Expected GraphQL named type but got: [function Number].',
message:
'The type of BadInterface.badField must be Output Type but got: [function Number].',
},
{
message:
'The type of BadImplementing.badField must be Output Type but got: [function Number].',
message: 'Expected GraphQL named type but got: [function Number].',
},
]);
});
Expand Down
Loading

0 comments on commit 68a0818

Please sign in to comment.