Skip to content

Commit 650a99f

Browse files
authored
Merge branch 'graphql:main' into fragment-args-2023
2 parents 02db413 + f201681 commit 650a99f

File tree

5 files changed

+110
-35
lines changed

5 files changed

+110
-35
lines changed

src/__testUtils__/viralSDL.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const viralSDL = `\
2+
schema {
3+
query: Query
4+
}
5+
6+
type Query {
7+
viruses: [Virus!]
8+
}
9+
10+
type Virus {
11+
name: String!
12+
knownMutations: [Mutation!]!
13+
}
14+
15+
type Mutation {
16+
name: String!
17+
geneSequence: String!
18+
}\
19+
`;

src/__testUtils__/viralSchema.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
GraphQLList,
3+
GraphQLNonNull,
4+
GraphQLObjectType,
5+
} from '../type/definition.js';
6+
import { GraphQLString } from '../type/scalars.js';
7+
import { GraphQLSchema } from '../type/schema.js';
8+
9+
const Mutation = new GraphQLObjectType({
10+
name: 'Mutation',
11+
fields: {
12+
name: {
13+
type: new GraphQLNonNull(GraphQLString),
14+
},
15+
geneSequence: {
16+
type: new GraphQLNonNull(GraphQLString),
17+
},
18+
},
19+
});
20+
21+
const Virus = new GraphQLObjectType({
22+
name: 'Virus',
23+
fields: {
24+
name: {
25+
type: new GraphQLNonNull(GraphQLString),
26+
},
27+
knownMutations: {
28+
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(Mutation))),
29+
},
30+
},
31+
});
32+
33+
const Query = new GraphQLObjectType({
34+
name: 'Query',
35+
fields: {
36+
viruses: {
37+
type: new GraphQLList(new GraphQLNonNull(Virus)),
38+
},
39+
},
40+
});
41+
42+
export const viralSchema = new GraphQLSchema({
43+
query: Query,
44+
});

src/utilities/__tests__/buildASTSchema-test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { assert, expect } from 'chai';
22
import { describe, it } from 'mocha';
33

44
import { dedent } from '../../__testUtils__/dedent.js';
5+
import { viralSDL } from '../../__testUtils__/viralSDL.js';
56

67
import type { Maybe } from '../../jsutils/Maybe.js';
78

@@ -1092,4 +1093,14 @@ describe('Schema Builder', () => {
10921093
'Unknown type: "UnknownType".',
10931094
);
10941095
});
1096+
1097+
it('correctly processes viral schema', () => {
1098+
const schema = buildSchema(viralSDL);
1099+
expect(schema.getQueryType()).to.contain({ name: 'Query' });
1100+
expect(schema.getType('Virus')).to.contain({ name: 'Virus' });
1101+
expect(schema.getType('Mutation')).to.contain({ name: 'Mutation' });
1102+
// Though the viral schema has a 'Mutation' type, it is not used for the
1103+
// 'mutation' operation.
1104+
expect(schema.getMutationType()).to.equal(undefined);
1105+
});
10951106
});

src/utilities/__tests__/printSchema-test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { expect } from 'chai';
22
import { describe, it } from 'mocha';
33

44
import { dedent, dedentString } from '../../__testUtils__/dedent.js';
5+
import { viralSchema } from '../../__testUtils__/viralSchema.js';
6+
import { viralSDL } from '../../__testUtils__/viralSDL.js';
57

68
import { DirectiveLocation } from '../../language/directiveLocation.js';
79

@@ -867,4 +869,8 @@ describe('Type System Printer', () => {
867869
}
868870
`);
869871
});
872+
it('prints viral schema correctly', () => {
873+
const printed = printSchema(viralSchema);
874+
expect(printed).to.equal(viralSDL);
875+
});
870876
});

src/utilities/printSchema.ts

+30-35
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,28 @@ function printFilteredSchema(
7070
}
7171

7272
function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> {
73-
if (schema.description == null && isSchemaOfCommonNames(schema)) {
74-
return;
75-
}
76-
77-
const operationTypes = [];
78-
7973
const queryType = schema.getQueryType();
80-
if (queryType) {
81-
operationTypes.push(` query: ${queryType.name}`);
82-
}
83-
8474
const mutationType = schema.getMutationType();
85-
if (mutationType) {
86-
operationTypes.push(` mutation: ${mutationType.name}`);
87-
}
88-
8975
const subscriptionType = schema.getSubscriptionType();
90-
if (subscriptionType) {
91-
operationTypes.push(` subscription: ${subscriptionType.name}`);
76+
77+
// Special case: When a schema has no root operation types, no valid schema
78+
// definition can be printed.
79+
if (!queryType && !mutationType && !subscriptionType) {
80+
return;
9281
}
9382

94-
return printDescription(schema) + `schema {\n${operationTypes.join('\n')}\n}`;
83+
// Only print a schema definition if there is a description or if it should
84+
// not be omitted because of having default type names.
85+
if (schema.description || !hasDefaultRootOperationTypes(schema)) {
86+
return (
87+
printDescription(schema) +
88+
'schema {\n' +
89+
(queryType ? ` query: ${queryType.name}\n` : '') +
90+
(mutationType ? ` mutation: ${mutationType.name}\n` : '') +
91+
(subscriptionType ? ` subscription: ${subscriptionType.name}\n` : '') +
92+
'}'
93+
);
94+
}
9595
}
9696

9797
/**
@@ -107,25 +107,20 @@ function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> {
107107
* }
108108
* ```
109109
*
110-
* When using this naming convention, the schema description can be omitted.
110+
* When using this naming convention, the schema description can be omitted so
111+
* long as these names are only used for operation types.
112+
*
113+
* Note however that if any of these default names are used elsewhere in the
114+
* schema but not as a root operation type, the schema definition must still
115+
* be printed to avoid ambiguity.
111116
*/
112-
function isSchemaOfCommonNames(schema: GraphQLSchema): boolean {
113-
const queryType = schema.getQueryType();
114-
if (queryType && queryType.name !== 'Query') {
115-
return false;
116-
}
117-
118-
const mutationType = schema.getMutationType();
119-
if (mutationType && mutationType.name !== 'Mutation') {
120-
return false;
121-
}
122-
123-
const subscriptionType = schema.getSubscriptionType();
124-
if (subscriptionType && subscriptionType.name !== 'Subscription') {
125-
return false;
126-
}
127-
128-
return true;
117+
function hasDefaultRootOperationTypes(schema: GraphQLSchema): boolean {
118+
/* eslint-disable eqeqeq */
119+
return (
120+
schema.getQueryType() == schema.getType('Query') &&
121+
schema.getMutationType() == schema.getType('Mutation') &&
122+
schema.getSubscriptionType() == schema.getType('Subscription')
123+
);
129124
}
130125

131126
export function printType(type: GraphQLNamedType): string {

0 commit comments

Comments
 (0)