Skip to content
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

Rework AST to be more type safe and allow mutation #95

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "120acb15c39aa3217e9888e515de160378fbcc1e",
"version": "2.18.0"
"revision": "154f1d32366449dcccf6375a173adf4ed2a74429",
"version": "2.38.0"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.library(name: "GraphQL", targets: ["GraphQL"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.10.1")),
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.38.0")),
.package(url: "https://github.com/apple/swift-collections", .upToNextMajor(from: "1.0.0")),
],
targets: [
Expand Down
54 changes: 25 additions & 29 deletions Sources/GraphQL/Execution/Execute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -359,23 +359,21 @@ func buildExecutionContext(

for definition in documentAST.definitions {
switch definition {
case let definition as OperationDefinition:
case .executableDefinition(.operation(let definition)):
guard !(operationName == nil && possibleOperation != nil) else {
throw GraphQLError(
message: "Must provide operation name if query contains multiple operations."
)
}

if operationName == nil || definition.name?.value == operationName {
possibleOperation = definition
}

case let definition as FragmentDefinition:
case .executableDefinition(.fragment(let definition)):
fragments[definition.name.value] = definition

default:
throw GraphQLError(
message: "GraphQL cannot execute a request containing a \(definition.kind).",
message: "GraphQL cannot execute a request containing a \(type(of: definition)).",
nodes: [definition]
)
}
Expand Down Expand Up @@ -502,7 +500,7 @@ func collectFields(

for selection in selectionSet.selections {
switch selection {
case let field as Field:
case .field(let field):
let shouldInclude = try shouldIncludeNode(
exeContext: exeContext,
directives: field.directives
Expand All @@ -519,7 +517,7 @@ func collectFields(
}

fields[name]?.append(field)
case let inlineFragment as InlineFragment:
case .inlineFragment(let inlineFragment):
let shouldInclude = try shouldIncludeNode(
exeContext: exeContext,
directives: inlineFragment.directives
Expand All @@ -542,43 +540,41 @@ func collectFields(
fields: &fields,
visitedFragmentNames: &visitedFragmentNames
)
case let fragmentSpread as FragmentSpread:
case .fragmentSpread(let fragmentSpread):
let fragmentName = fragmentSpread.name.value

let shouldInclude = try shouldIncludeNode(
exeContext: exeContext,
directives: fragmentSpread.directives
)

guard visitedFragmentNames[fragmentName] == nil && shouldInclude else {
continue
}

visitedFragmentNames[fragmentName] = true

guard let fragment = exeContext.fragments[fragmentName] else {
continue
}

let fragmentConditionMatches = try doesFragmentConditionMatch(
exeContext: exeContext,
fragment: fragment,
type: runtimeType
)

guard fragmentConditionMatches else {
continue
}

try collectFields(
exeContext: exeContext,
runtimeType: runtimeType,
selectionSet: fragment.selectionSet,
fields: &fields,
visitedFragmentNames: &visitedFragmentNames
)
default:
break
}
}

Expand Down Expand Up @@ -629,15 +625,15 @@ func doesFragmentConditionMatch(
return true
}

guard let conditionalType = typeFromAST(schema: exeContext.schema, inputTypeAST: typeConditionAST) else {
guard let conditionalType = typeFromAST(schema: exeContext.schema, inputTypeAST: .namedType(typeConditionAST)) else {
return true
}

if let conditionalType = conditionalType as? GraphQLObjectType, conditionalType.name == type.name {
return true
}

if let abstractType = conditionalType as? GraphQLAbstractType {
if let abstractType = conditionalType as? (any GraphQLAbstractType) {
return exeContext.schema.isSubType(
abstractType: abstractType,
maybeSubType: type
Expand Down Expand Up @@ -765,7 +761,7 @@ func resolveOrError(
// in the execution context.
func completeValueCatchingError(
exeContext: ExecutionContext,
returnType: GraphQLType,
returnType: any GraphQLType,
fieldASTs: [Field],
info: GraphQLResolveInfo,
path: IndexPath,
Expand Down Expand Up @@ -817,7 +813,7 @@ func completeValueCatchingError(
// location information.
func completeValueWithLocatedError(
exeContext: ExecutionContext,
returnType: GraphQLType,
returnType: any GraphQLType,
fieldASTs: [Field],
info: GraphQLResolveInfo,
path: IndexPath,
Expand Down Expand Up @@ -867,7 +863,7 @@ func completeValueWithLocatedError(
*/
func completeValue(
exeContext: ExecutionContext,
returnType: GraphQLType,
returnType: any GraphQLType,
fieldASTs: [Field],
info: GraphQLResolveInfo,
path: IndexPath,
Expand Down Expand Up @@ -916,13 +912,13 @@ func completeValue(

// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
// returning .null if serialization is not possible.
if let returnType = returnType as? GraphQLLeafType {
if let returnType = returnType as? any GraphQLLeafType {
return exeContext.eventLoopGroup.next().makeSucceededFuture(try completeLeafValue(returnType: returnType, result: r))
}

// If field type is an abstract type, Interface or Union, determine the
// runtime Object type and complete for that type.
if let returnType = returnType as? GraphQLAbstractType {
if let returnType = returnType as? any GraphQLAbstractType {
return try completeAbstractValue(
exeContext: exeContext,
returnType: returnType,
Expand Down Expand Up @@ -999,7 +995,7 @@ func completeListValue(
* Complete a Scalar or Enum by serializing to a valid value, returning
* .null if serialization is not possible.
*/
func completeLeafValue(returnType: GraphQLLeafType, result: Any?) throws -> Map {
func completeLeafValue(returnType: any GraphQLLeafType, result: Any?) throws -> Map {
guard let result = result else {
return .null
}
Expand All @@ -1023,7 +1019,7 @@ func completeLeafValue(returnType: GraphQLLeafType, result: Any?) throws -> Map
*/
func completeAbstractValue(
exeContext: ExecutionContext,
returnType: GraphQLAbstractType,
returnType: any GraphQLAbstractType,
fieldASTs: [Field],
info: GraphQLResolveInfo,
path: IndexPath,
Expand All @@ -1046,7 +1042,7 @@ func completeAbstractValue(
}

// If resolveType returns a string, we assume it's a GraphQLObjectType name.
var runtimeType: GraphQLType?
var runtimeType: (any GraphQLType)?

switch resolveResult {
case .name(let name):
Expand Down Expand Up @@ -1143,7 +1139,7 @@ func defaultResolveType(
value: Any,
eventLoopGroup: EventLoopGroup,
info: GraphQLResolveInfo,
abstractType: GraphQLAbstractType
abstractType: any GraphQLAbstractType
) throws -> TypeResolveResult? {
let possibleTypes = info.schema.getPossibleTypes(abstractType: abstractType)

Expand Down
10 changes: 5 additions & 5 deletions Sources/GraphQL/Execution/Values.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func getVariableValue(schema: GraphQLSchema, definitionAST: VariableDefinition,
let type = typeFromAST(schema: schema, inputTypeAST: definitionAST.type)
let variable = definitionAST.variable

guard let inputType = type as? GraphQLInputType else {
guard let inputType = type as? (any GraphQLInputType) else {
throw GraphQLError(
message:
"Variable \"$\(variable.name.value)\" expected value of type " +
Expand Down Expand Up @@ -112,11 +112,11 @@ func getVariableValue(schema: GraphQLSchema, definitionAST: VariableDefinition,
/**
* Given a type and any value, return a runtime value coerced to match the type.
*/
func coerceValue(value: Map, type: GraphQLInputType) throws -> Map {
func coerceValue(value: Map, type: any GraphQLInputType) throws -> Map {
if let nonNull = type as? GraphQLNonNull {
// Note: we're not checking that the result of coerceValue is non-null.
// We only call this function after calling validate.
guard let nonNullType = nonNull.ofType as? GraphQLInputType else {
guard let nonNullType = nonNull.ofType as? (any GraphQLInputType) else {
throw GraphQLError(message: "NonNull must wrap an input type")
}
return try coerceValue(value: value, type: nonNullType)
Expand All @@ -127,7 +127,7 @@ func coerceValue(value: Map, type: GraphQLInputType) throws -> Map {
}

if let list = type as? GraphQLList {
guard let itemType = list.ofType as? GraphQLInputType else {
guard let itemType = list.ofType as? (any GraphQLInputType) else {
throw GraphQLError(message: "Input list must wrap an input type")
}

Expand Down Expand Up @@ -168,7 +168,7 @@ func coerceValue(value: Map, type: GraphQLInputType) throws -> Map {
return .dictionary(object)
}

if let leafType = type as? GraphQLLeafType {
if let leafType = type as? (any GraphQLLeafType) {
return try leafType.parseValue(value: value)
}

Expand Down
Loading