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
7 changes: 7 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"version": 1,
"indentation" : {
"spaces" : 4
},
"lineBreakBeforeEachArgument": true
}
4 changes: 2 additions & 2 deletions Examples/HelloWorldServer/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription
let package = Package(
name: "HelloWorldServer",
platforms: [
.macOS(.v13),
.macOS(.v13)
],
dependencies: [
.package(name: "graphql-generator", path: "../.."),
Expand All @@ -20,7 +20,7 @@ let package = Package(
.product(name: "GraphQLGeneratorRuntime", package: "graphql-generator"),
],
plugins: [
.plugin(name: "GraphQLGeneratorPlugin", package: "graphql-generator"),
.plugin(name: "GraphQLGeneratorPlugin", package: "graphql-generator")
]
),
.testTarget(
Expand Down
59 changes: 48 additions & 11 deletions Examples/HelloWorldServer/Sources/HelloWorldServer/Resolvers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ extension GraphQLScalars {
case .string:
return map
default:
throw GraphQLError(message: "EmailAddress cannot represent non-string value: \(map)")
throw GraphQLError(
message: "EmailAddress cannot represent non-string value: \(map)"
)
}
}

Expand Down Expand Up @@ -89,7 +91,9 @@ struct User: GraphQLGenerated.User {
// Can't use @graphQLResolver macro because we must convert from String to GraphQLScalars.EmailAddress

let email: String
func email(context _: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> GraphQLScalars.EmailAddress {
func email(context _: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws
-> GraphQLScalars.EmailAddress
{
return .init(email: email)
}
}
Expand All @@ -99,7 +103,10 @@ struct Contact: GraphQLGenerated.Contact {

/// Required implementations
let email: String
func email(context _: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> GraphQLScalars.EmailAddress {
func email(
context _: GraphQLContext,
info _: GraphQL.GraphQLResolveInfo
) async throws -> GraphQLScalars.EmailAddress {
return .init(email: email)
}
}
Expand All @@ -113,37 +120,63 @@ struct Post: GraphQLGenerated.Post {

/// Required implementations
let authorId: String
func author(context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> any GraphQLGenerated.User {
func author(
context: GraphQLContext,
info _: GraphQL.GraphQLResolveInfo
) async throws -> any GraphQLGenerated.User {
return context.users[authorId]!
}
}

struct Query: GraphQLGenerated.Query {
/// Required implementations
static func user(id: String, context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> (any GraphQLGenerated.User)? {
static func user(
id: String,
context: GraphQLContext,
info _: GraphQL.GraphQLResolveInfo
) async throws -> (any GraphQLGenerated.User)? {
return context.users[id]
}

static func users(context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> [any GraphQLGenerated.User] {
static func users(
context: GraphQLContext,
info _: GraphQL.GraphQLResolveInfo
) async throws -> [any GraphQLGenerated.User] {
return context.users.values.map { $0 as any GraphQLGenerated.User }
}

static func post(id: String, context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> (any GraphQLGenerated.Post)? {
static func post(
id: String,
context: GraphQLContext,
info _: GraphQL.GraphQLResolveInfo
) async throws -> (any GraphQLGenerated.Post)? {
return context.posts[id]
}

static func posts(limit _: Int?, context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> [any GraphQLGenerated.Post] {
static func posts(
limit _: Int?,
context: GraphQLContext,
info _: GraphQL.GraphQLResolveInfo
) async throws -> [any GraphQLGenerated.Post] {
return context.posts.values.map { $0 as any GraphQLGenerated.Post }
}

static func userOrPost(id: String, context: GraphQLContext, info _: GraphQLResolveInfo) async throws -> (any GraphQLGenerated.UserOrPost)? {
static func userOrPost(
id: String,
context: GraphQLContext,
info _: GraphQLResolveInfo
) async throws -> (any GraphQLGenerated.UserOrPost)? {
return context.users[id] ?? context.posts[id]
}
}

struct Mutation: GraphQLGenerated.Mutation {
/// Required implementations
static func upsertUser(userInfo: GraphQLGenerated.UserInfo, context: GraphQLContext, info _: GraphQLResolveInfo) -> any GraphQLGenerated.User {
static func upsertUser(
userInfo: GraphQLGenerated.UserInfo,
context: GraphQLContext,
info _: GraphQLResolveInfo
) -> any GraphQLGenerated.User {
let user = User(
id: userInfo.id,
name: userInfo.name,
Expand All @@ -158,7 +191,11 @@ struct Mutation: GraphQLGenerated.Mutation {

struct Subscription: GraphQLGenerated.Subscription {
/// Required implementations
static func watchUser(id: String, context: GraphQLContext, info _: GraphQLResolveInfo) async throws -> AnyAsyncSequence<(any GraphQLGenerated.User)?> {
static func watchUser(
id: String,
context: GraphQLContext,
info _: GraphQLResolveInfo
) async throws -> AnyAsyncSequence<(any GraphQLGenerated.User)?> {
return AsyncStream<(any GraphQLGenerated.User)?> { continuation in
context.onTriggerWatch = { [weak context] in
continuation.yield(context?.users[id])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
import GraphQL
@testable import HelloWorldServer
import Testing

@testable import HelloWorldServer

@Suite
struct HelloWorldServerTests {
@Test func query() async throws {
let schema = try buildGraphQLSchema(resolvers: Resolvers.self)
let context = GraphQLContext(
users: ["1": .init(id: "1", name: "John", age: 18, role: .user, email: "john@example.com")],
users: [
"1": .init(id: "1", name: "John", age: 18, role: .user, email: "john@example.com")
],
posts: ["1": .init(id: "1", title: "Foo", content: "bar", authorId: "1")]
)
let actual = try await graphql(
schema: schema,
request: """
{
posts {
id
title
content
author {
{
posts {
id
name
email
age
role
title
content
author {
id
name
email
age
role
}
}
}
}
""",
""",
context: context
)
let expected = GraphQLResult(
Expand All @@ -44,8 +47,8 @@ struct HelloWorldServerTests {
"age": 18,
"role": "USER",
],
],
],
]
]
]
)
#expect(actual == expected)
Expand All @@ -60,16 +63,16 @@ struct HelloWorldServerTests {
let actual = try await graphql(
schema: schema,
request: """
mutation {
upsertUser(userInfo: {id: "2", name: "Jane", email: "jane@example.com"}) {
id
name
email
age
role
mutation {
upsertUser(userInfo: {id: "2", name: "Jane", email: "jane@example.com"}) {
id
name
email
age
role
}
}
}
""",
""",
context: context
)
let expected = GraphQLResult(
Expand All @@ -80,7 +83,7 @@ struct HelloWorldServerTests {
"email": "jane@example.com",
"age": nil,
"role": "USER",
],
]
]
)
#expect(actual == expected)
Expand All @@ -89,55 +92,59 @@ struct HelloWorldServerTests {
@Test func subscription() async throws {
let schema = try buildGraphQLSchema(resolvers: Resolvers.self)
let context = GraphQLContext(
users: ["1": .init(id: "1", name: "John", age: 18, role: .user, email: "john@example.com")],
users: [
"1": .init(id: "1", name: "John", age: 18, role: .user, email: "john@example.com")
],
posts: [:]
)
let stream = try await graphqlSubscribe(
schema: schema,
request: """
subscription {
watchUser(id: "1") {
id
name
email
age
role
subscription {
watchUser(id: "1") {
id
name
email
age
role
}
}
}
""",
""",
context: context
).get()

var iterator = stream.makeAsyncIterator()

context.triggerWatch()
#expect(
try await iterator.next() == GraphQLResult(
data: [
"watchUser": [
"id": "1",
"name": "John",
"email": "john@example.com",
"age": 18,
"role": "USER",
],
]
)
try await iterator.next()
== GraphQLResult(
data: [
"watchUser": [
"id": "1",
"name": "John",
"email": "john@example.com",
"age": 18,
"role": "USER",
]
]
)
)

context.triggerWatch()
#expect(
try await iterator.next() == GraphQLResult(
data: [
"watchUser": [
"id": "1",
"name": "John",
"email": "john@example.com",
"age": 18,
"role": "USER",
],
]
)
try await iterator.next()
== GraphQLResult(
data: [
"watchUser": [
"id": "1",
"name": "John",
"email": "john@example.com",
"age": 18,
"role": "USER",
]
]
)
)
}
}
8 changes: 4 additions & 4 deletions Examples/StarWars/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import PackageDescription
let package = Package(
name: "StarWars",
platforms: [
.macOS(.v13),
.macOS(.v13)
],
products: [
.library(
name: "StarWars",
targets: ["StarWars"]
),
)
],
dependencies: [
.package(name: "graphql-generator", path: "../.."),
Expand All @@ -30,13 +30,13 @@ let package = Package(
.product(name: "GraphQLGeneratorRuntime", package: "graphql-generator"),
],
plugins: [
.plugin(name: "GraphQLGeneratorPlugin", package: "graphql-generator"),
.plugin(name: "GraphQLGeneratorPlugin", package: "graphql-generator")
]
),
.testTarget(
name: "StarWarsTests",
dependencies: [
"StarWars",
"StarWars"
]
),
]
Expand Down
Loading
Loading