Skip to content
Merged
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 Examples/HelloWorldServer/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Examples/HelloWorldServer/Sources/HelloWorldServer/Resolvers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,5 @@ type Mutation {

type Subscription {
watchUser(id: ID!): User
watchUserOrPost(id: ID!): UserOrPost
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
]
)
)
}
}
4 changes: 2 additions & 2 deletions Examples/StarWars/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() ?? [:]
"""

Expand Down
6 changes: 6 additions & 0 deletions Tests/GraphQLGeneratorCoreTests/SchemaGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Loading