Skip to content

Commit 3e5a81e

Browse files
feat!: Schema query is optional
1 parent 7d91bd0 commit 3e5a81e

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

Sources/GraphQL/Execution/Execute.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,14 @@ func getOperationRootType(
466466
) throws -> GraphQLObjectType {
467467
switch operation.operation {
468468
case .query:
469-
return schema.queryType
469+
guard let queryType = schema.queryType else {
470+
throw GraphQLError(
471+
message: "Schema is not configured for queries",
472+
nodes: [operation]
473+
)
474+
}
475+
476+
return queryType
470477
case .mutation:
471478
guard let mutationType = schema.mutationType else {
472479
throw GraphQLError(
@@ -1213,9 +1220,9 @@ func getFieldDef(
12131220
parentType: GraphQLObjectType,
12141221
fieldName: String
12151222
) throws -> GraphQLFieldDefinition {
1216-
if fieldName == SchemaMetaFieldDef.name, schema.queryType.name == parentType.name {
1223+
if fieldName == SchemaMetaFieldDef.name, schema.queryType?.name == parentType.name {
12171224
return SchemaMetaFieldDef
1218-
} else if fieldName == TypeMetaFieldDef.name, schema.queryType.name == parentType.name {
1225+
} else if fieldName == TypeMetaFieldDef.name, schema.queryType?.name == parentType.name {
12191226
return TypeMetaFieldDef
12201227
} else if fieldName == TypeNameMetaFieldDef.name {
12211228
return TypeNameMetaFieldDef

Sources/GraphQL/Type/Schema.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
*/
2828
public final class GraphQLSchema {
29-
public let queryType: GraphQLObjectType
29+
public let queryType: GraphQLObjectType?
3030
public let mutationType: GraphQLObjectType?
3131
public let subscriptionType: GraphQLObjectType?
3232
public let directives: [GraphQLDirective]
@@ -35,7 +35,7 @@ public final class GraphQLSchema {
3535
private var subTypeMap: [String: [String: Bool]] = [:]
3636

3737
public init(
38-
query: GraphQLObjectType,
38+
query: GraphQLObjectType? = nil,
3939
mutation: GraphQLObjectType? = nil,
4040
subscription: GraphQLObjectType? = nil,
4141
types: [GraphQLNamedType] = [],
@@ -49,9 +49,11 @@ public final class GraphQLSchema {
4949
self.directives = directives.isEmpty ? specifiedDirectives : directives
5050

5151
// Build type map now to detect any errors within this schema.
52-
var initialTypes: [GraphQLNamedType] = [
53-
queryType,
54-
]
52+
var initialTypes: [GraphQLNamedType] = []
53+
54+
if let query = queryType {
55+
initialTypes.append(query)
56+
}
5557

5658
if let mutation = mutationType {
5759
initialTypes.append(mutation)

Sources/GraphQL/Utilities/TypeInfo.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ func getFieldDef(
238238
let name = fieldAST.name.value
239239

240240
if let parentType = parentType as? GraphQLNamedType {
241-
if name == SchemaMetaFieldDef.name, schema.queryType.name == parentType.name {
241+
if name == SchemaMetaFieldDef.name, schema.queryType?.name == parentType.name {
242242
return SchemaMetaFieldDef
243243
}
244244

245-
if name == TypeMetaFieldDef.name, schema.queryType.name == parentType.name {
245+
if name == TypeMetaFieldDef.name, schema.queryType?.name == parentType.name {
246246
return TypeMetaFieldDef
247247
}
248248
}

0 commit comments

Comments
 (0)