GraphQLSwift / Graphiti

The Swift GraphQL Schema framework for macOS and Linux
MIT License
531 stars 67 forks source link

Instance-based PartialSchema #99

Closed d-exclaimation closed 1 year ago

d-exclaimation commented 1 year ago

Note This is a lower priority change, it doesn't need to be merged right away

Changes overview

Reasoning

Subclassing declaration would have more boilerplate than Instance based declaration

Example

let AuthSchema = PartialSchema<Resolver, Context>(
    types: {
        Type(User.self) {
            Field("id", at: \.id)
            Field("name", at: \.name)
            Field("email", at: \.email)
            Field("orders", at: User.orders, as: [TypeReference<Order>].self)
        }
        Type(Credential.self) {
            Field("token", at: \.token)
            Field("expiresAt", at: \.expiresAt)
            Field("user", at: \.user)
        }
    },
    query: {
        Field("me", at: Resolver.me)
    },
    mutation: {
        Field("login", at: Resolver.login)
        Field("signup", at: Resolver.signup)
    }
)

let OrderSchema = PartialSchema<Resolver, Context>(
    types: {
        Type(Order.self) {
            Field("id", at: \.id)
            Field("quantity", at: \.quantity)
            Field("cost", at: \.cost)
            Field("user", at: Order.user)
            Field("status", at: Order.currentStatus, as: TypeReference<Status>?.self)
            Field("progress", at: Order.progress, as: [TypeReference<Status>].self)
        }
        Type(Status.self) {
            Field("distance", at: \.distance)
            Field("progress", at: \.progress)
            Field("order", at: Status.order)
        }
    },
    query: {
        Field("order", at: Resolver.orderById) {
            Argument("id", at: \.id)
        }
    },
    mutation: {
        Field("createOrder", at: Resolver.createOrder) {
            Argument("quantity", at: \.quantity)
        }
    }
)

let schema = try Schema.create(from: [AuthSchema, OrderSchema])
d-exclaimation commented 1 year ago

Also I updated the README with examples to use PartialSchema

NeedleInAJayStack commented 1 year ago

I saw that and I love it!

d-exclaimation commented 1 year ago

@NeedleInAJayStack Mind doing the release again?

NeedleInAJayStack commented 1 year ago

Sure, no problem