diff --git a/Examples/HelloWorldServer/Package.resolved b/Examples/HelloWorldServer/Package.resolved index 5c14e4d..63f05ba 100644 --- a/Examples/HelloWorldServer/Package.resolved +++ b/Examples/HelloWorldServer/Package.resolved @@ -6,8 +6,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/GraphQLSwift/GraphQL.git", "state" : { - "revision" : "a83766b34139a855476166b1dc44d9ae9d8ecff4", - "version" : "4.3.0" + "revision" : "d45529c29041638915c6efc0bc5f9a832ebbb34b", + "version" : "4.4.0" } }, { diff --git a/Examples/HelloWorldServer/Sources/HelloWorldServer/Resolvers.swift b/Examples/HelloWorldServer/Sources/HelloWorldServer/Resolvers.swift index e25dc08..4dbd5a3 100644 --- a/Examples/HelloWorldServer/Sources/HelloWorldServer/Resolvers.swift +++ b/Examples/HelloWorldServer/Sources/HelloWorldServer/Resolvers.swift @@ -202,4 +202,16 @@ struct Subscription: GraphQLGenerated.Subscription { } }.any() } + + static func watchUserOrPost( + id: String, + context: GraphQLContext, + info _: GraphQLResolveInfo + ) async throws -> AnyAsyncSequence<(any GraphQLGenerated.UserOrPost)?> { + return AsyncStream<(any GraphQLGenerated.UserOrPost)?> { continuation in + context.onTriggerWatch = { [weak context] in + continuation.yield(context?.users[id] ?? context?.posts[id]) + } + }.any() + } } diff --git a/Examples/HelloWorldServer/Sources/HelloWorldServer/schema.graphql b/Examples/HelloWorldServer/Sources/HelloWorldServer/schema.graphql index ba9dbc9..304c74b 100644 --- a/Examples/HelloWorldServer/Sources/HelloWorldServer/schema.graphql +++ b/Examples/HelloWorldServer/Sources/HelloWorldServer/schema.graphql @@ -121,4 +121,5 @@ type Mutation { type Subscription { watchUser(id: ID!): User + watchUserOrPost(id: ID!): UserOrPost } diff --git a/Examples/HelloWorldServer/Tests/HelloWorldServerTests/HelloWorldServerTests.swift b/Examples/HelloWorldServer/Tests/HelloWorldServerTests/HelloWorldServerTests.swift index 4df8e48..6249e3f 100644 --- a/Examples/HelloWorldServer/Tests/HelloWorldServerTests/HelloWorldServerTests.swift +++ b/Examples/HelloWorldServer/Tests/HelloWorldServerTests/HelloWorldServerTests.swift @@ -147,4 +147,54 @@ struct HelloWorldServerTests { ) ) } + + @Test func subscriptionUnion() async throws { + let schema = try buildGraphQLSchema(resolvers: Resolvers.self) + let context = GraphQLContext( + users: [ + "user_1": .init(id: "user_1", name: "John", age: 18, role: .user, email: "john@example.com") + ], + posts: [ + "post_1": .init(id: "post_1", title: "Foo", content: "bar", authorId: "1") + ] + ) + let stream = try await graphqlSubscribe( + schema: schema, + request: """ + subscription { + watchUserOrPost(id: "post_1") { + ... on User { + id + name + email + age + role + } + ... on Post { + id + title + content + } + } + } + """, + context: context + ).get() + + var iterator = stream.makeAsyncIterator() + + context.triggerWatch() + #expect( + try await iterator.next() + == GraphQLResult( + data: [ + "watchUserOrPost": [ + "id": "post_1", + "title": "Foo", + "content": "bar", + ] + ] + ) + ) + } } diff --git a/Examples/StarWars/Package.resolved b/Examples/StarWars/Package.resolved index b99e4b9..398163a 100644 --- a/Examples/StarWars/Package.resolved +++ b/Examples/StarWars/Package.resolved @@ -33,8 +33,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/GraphQLSwift/GraphQL.git", "state" : { - "revision" : "a83766b34139a855476166b1dc44d9ae9d8ecff4", - "version" : "4.3.0" + "revision" : "d45529c29041638915c6efc0bc5f9a832ebbb34b", + "version" : "4.4.0" } }, { diff --git a/Package.resolved b/Package.resolved index 5499771..33014a6 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,13 +1,13 @@ { - "originHash" : "66b7f6278d702f64d00bafe865c19ca50c6e21d21160020fbb7ff28d84530008", + "originHash" : "8f9854682b3a147b60568c74560bb368062034175514f1ef91b24994858c5101", "pins" : [ { "identity" : "graphql", "kind" : "remoteSourceControl", "location" : "https://github.com/GraphQLSwift/GraphQL.git", "state" : { - "revision" : "a83766b34139a855476166b1dc44d9ae9d8ecff4", - "version" : "4.3.0" + "revision" : "d45529c29041638915c6efc0bc5f9a832ebbb34b", + "version" : "4.4.0" } }, { diff --git a/Package.swift b/Package.swift index d5ac1ee..f032fcd 100644 --- a/Package.swift +++ b/Package.swift @@ -26,7 +26,7 @@ let package = Package( ), ], dependencies: [ - .package(url: "https://github.com/GraphQLSwift/GraphQL.git", from: "4.3.0"), + .package(url: "https://github.com/GraphQLSwift/GraphQL.git", from: "4.4.0"), .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0"), .package(url: "https://github.com/swiftlang/swift-syntax.git", "600.0.1"..<"603.0.0"), .package(url: "https://github.com/jpsim/Yams.git", "4.0.0"..<"7.0.0"), diff --git a/Sources/GraphQLGeneratorCore/Generator/BuildGraphQLSchemaGenerator.swift b/Sources/GraphQLGeneratorCore/Generator/BuildGraphQLSchemaGenerator.swift index d049551..5abb23d 100644 --- a/Sources/GraphQLGeneratorCore/Generator/BuildGraphQLSchemaGenerator.swift +++ b/Sources/GraphQLGeneratorCore/Generator/BuildGraphQLSchemaGenerator.swift @@ -172,10 +172,17 @@ package struct BuildGraphQLSchemaGenerator { case .parent: variableName = "\(typeName)Fields" } + + let castType = try swiftTypeDeclaration( + for: type, + includeNamespace: true, + nameGenerator: nameGenerator + ) var output = """ let \(typeName) = schema.typeMap["\(type.name)"] as? GraphQLObjectType + \(typeName)?.isTypeOf = { source, _ in source is (any \(castType))} let \(variableName) = try \(typeName)?.fields() ?? [:] """ diff --git a/Tests/GraphQLGeneratorCoreTests/SchemaGeneratorTests.swift b/Tests/GraphQLGeneratorCoreTests/SchemaGeneratorTests.swift index 0348e1a..a0077f7 100644 --- a/Tests/GraphQLGeneratorCoreTests/SchemaGeneratorTests.swift +++ b/Tests/GraphQLGeneratorCoreTests/SchemaGeneratorTests.swift @@ -54,6 +54,7 @@ struct SchemaGeneratorTests { let schema = try GraphQL.buildSchema(source: graphQLRawSDL) let bar = schema.typeMap["Bar"] as? GraphQLObjectType + bar?.isTypeOf = { source, _ in source is (any GraphQLGenerated.Bar)} let barFields = try bar?.fields() ?? [:] barFields["foo"]?.resolve = { @Sendable source, args, context, info in let parent = try cast(source, to: (any GraphQLGenerated.Bar).self) @@ -65,6 +66,7 @@ struct SchemaGeneratorTests { } let query = schema.typeMap["Query"] as? GraphQLObjectType + query?.isTypeOf = { source, _ in source is (any GraphQLGenerated.Query)} let queryFields = try query?.fields() ?? [:] queryFields["foo"]?.resolve = { @Sendable source, args, context, info in let context = try cast(context, to: GraphQLContext.self) @@ -155,6 +157,7 @@ struct SchemaGeneratorTests { let expected = """ let book = schema.typeMap["Book"] as? GraphQLObjectType + book?.isTypeOf = { source, _ in source is (any GraphQLGenerated.Book)} let bookFields = try book?.fields() ?? [:] bookFields["title"]?.resolve = { @Sendable source, args, context, info in let parent = try cast(source, to: (any GraphQLGenerated.Book).self) @@ -191,6 +194,7 @@ struct SchemaGeneratorTests { let expected = """ let query = schema.typeMap["Query"] as? GraphQLObjectType + query?.isTypeOf = { source, _ in source is (any GraphQLGenerated.Query)} let queryFields = try query?.fields() ?? [:] queryFields["user"]?.resolve = { @Sendable source, args, context, info in let context = try cast(context, to: GraphQLContext.self) @@ -220,6 +224,7 @@ struct SchemaGeneratorTests { let expected = """ let mutation = schema.typeMap["Mutation"] as? GraphQLObjectType + mutation?.isTypeOf = { source, _ in source is (any GraphQLGenerated.Mutation)} let mutationFields = try mutation?.fields() ?? [:] mutationFields["createUser"]?.resolve = { @Sendable source, args, context, info in let context = try cast(context, to: GraphQLContext.self) @@ -249,6 +254,7 @@ struct SchemaGeneratorTests { let expected = """ let subscription = schema.typeMap["Subscription"] as? GraphQLObjectType + subscription?.isTypeOf = { source, _ in source is (any GraphQLGenerated.Subscription)} let subscriptionFields = try subscription?.fields() ?? [:] subscriptionFields["userUpdated"]?.resolve = { @Sendable source, _, _, _ in return source