Skip to content

Commit 25f0fb3

Browse files
committed
Use default Query type if it isn't defined in the introspection result (#537)
1 parent 2986307 commit 25f0fb3

File tree

6 files changed

+2303
-7
lines changed

6 files changed

+2303
-7
lines changed

src/main/com/intellij/lang/jsgraphql/ide/introspection/GraphQLIntrospectionResultToSchema.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ public Document createSchemaDefinition(@NotNull Map<String, Object> introspectio
3939
assertTrue(introspectionResult.get("__schema") != null, () -> "__schema expected");
4040
Map<String, Object> schema = (Map<String, Object>) introspectionResult.get("__schema");
4141

42+
SchemaDefinition.Builder schemaDefinition = SchemaDefinition.newSchemaDefinition();
4243

4344
Map<String, Object> queryType = (Map<String, Object>) schema.get("queryType");
44-
assertNotNull(queryType, () -> "queryType expected");
45-
TypeName query = TypeName.newTypeName().name((String) queryType.get("name")).build();
46-
boolean nonDefaultQueryName = !"Query".equals(query.getName());
47-
48-
SchemaDefinition.Builder schemaDefinition = SchemaDefinition.newSchemaDefinition();
49-
schemaDefinition.operationTypeDefinition(
50-
OperationTypeDefinition.newOperationTypeDefinition().name("query").typeName(query).build());
45+
boolean nonDefaultQueryName = false;
46+
if (queryType != null) {
47+
TypeName query = TypeName.newTypeName().name((String) queryType.get("name")).build();
48+
nonDefaultQueryName = !"Query".equals(query.getName());
49+
schemaDefinition.operationTypeDefinition(
50+
OperationTypeDefinition.newOperationTypeDefinition().name("query").typeName(query).build());
51+
}
5152

5253
Map<String, Object> mutationType = (Map<String, Object>) schema.get("mutationType");
5354
boolean nonDefaultMutationName = false;

src/test/com/intellij/lang/jsgraphql/introspection/GraphQLIntrospectionServiceTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ public void testPrintIntrospectionWithUndefinedDirectives() {
5555
doTest("schemaWithUndefinedDirectives.json", "schemaWithUndefinedDirectives.graphql");
5656
}
5757

58+
public void testPrintIntrospectionWithoutRootTypes() {
59+
doTest("schemaWithoutRootTypes.json", "schemaWithoutRootTypes.graphql");
60+
}
61+
62+
public void testPrintIntrospectionWithCustomRootTypes() {
63+
doTest("schemaWithCustomRootTypes.json", "schemaWithCustomRootTypes.graphql");
64+
}
65+
5866
private void doTest(@NotNull String source, @NotNull String expected) {
5967
myFixture.configureByText(
6068
"result.graphql",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
schema {
2+
query: CustomQuery
3+
mutation: CustomMutation
4+
subscription: CustomSubscription
5+
}
6+
7+
type CustomMutation {
8+
createUser: User
9+
}
10+
11+
type CustomQuery {
12+
user: User
13+
}
14+
15+
type CustomSubscription {
16+
users: [User]
17+
}
18+
19+
type User {
20+
name: String
21+
}

0 commit comments

Comments
 (0)