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

Adds a SchemaBuilder type for modular schema creation #87

Merged

Conversation

NeedleInAJayStack
Copy link
Member

This adds a SchemaBuilder, which enables much more modular schema creation. By breaking up the schema definition, different parts of the schema could be created in different functions, files, or even packages.

@paulofaria
Copy link
Member

Are these additive changes? Didn't have enough time to properly look into it, yet.

@NeedleInAJayStack
Copy link
Member Author

Yup, purely additive.

@paulofaria
Copy link
Member

Have you tried implementing it using result builders? I think it fits better with Swift when comparing it to fluent builders.

@NeedleInAJayStack
Copy link
Member Author

NeedleInAJayStack commented Nov 10, 2022

Can you split up result builder definitions or do they always need to be defined in a single contiguous block?

I'll admit, I'm not a result builder expert. My goal with this was to allow schemas to be defined over a number of different functions, files, or even packages, because my schema definitions get REALLY huge and unwieldy when I have to keep them in a single block. Builders like Vapor's RouteBuilder or SQLKit's SQLPredicateBuilder achieve that goal pretty well. For example, we could have a project with the 3 files below, where functionality is separated effectively:

# Main.swift

let builder = SchemaBuilder<Res, Ctx>()
addHello(to: builder)
addGoodbye(to: builder)
let schema = try builder.build()
print(schema.execute("{ goodbye }", ...)) // Prints: {"goodbye": "for now"}
# Hello.swift

extension Res {
    func hello() -> String {
        "world"
    }
} 
func addHello(to builder: SchemaBuilder<Res, Ctx>) {
    builder.addQuery(
        Field("hello", at: \.hello)
    )
}
# Goodbye.swift

extension Res {
    func goodbye() -> String {
        "for now"
    }
} 
func addGoodbye(to builder: SchemaBuilder<Res, Ctx>) {
    builder.addQuery(
        Field("goodbye", at: \.goodbye)
    )
}

I didn't know how to accomplish something like this with a resultBuilder pattern. If there is one, could you provide me a quick example?

@NeedleInAJayStack
Copy link
Member Author

Also, just to be clear, the SchemaBuilder.add* methods take result builder arguments. And I'm not removing any existing Schema.init result builder functionality.

@paulofaria
Copy link
Member

Hey, Jay! Sure, I'll give you an update this week!

@NeedleInAJayStack
Copy link
Member Author

Hey @paulofaria, any updates on this? I'd be happy to make the changes if you can get me an example of the multi-file result-builder schema construction!

@paulofaria
Copy link
Member

Sorry! I'm moving to a new house, so things are chaotic around here. I'll try to find the time, though. Should be straight forward.

@paulofaria
Copy link
Member

Hey, @NeedleInAJayStack. Can you please check you reddit DMs? 😁

@NeedleInAJayStack
Copy link
Member Author

@paulofaria Hah, what? I'm not on Reddit, but you can always reach me at NeedleInAJayStack@protonmail.com

@cshadek
Copy link
Contributor

cshadek commented Dec 27, 2022

Any updates on this?

@NeedleInAJayStack
Copy link
Member Author

I think @paulofaria was going to send me an example of a distributed ResultBuilder

@paulofaria
Copy link
Member

Really sorry about the delay. @NeedleInAJayStack can I update this PR directly, with the result builder approach? I think it's easier.

@NeedleInAJayStack
Copy link
Member Author

@paulofaria No worries! Sure, feel free to edit this directly or on your own branch of my work.

@paulofaria
Copy link
Member

Hey, I’m really sorry about this. I think it’s best to merge this as is and improve later, if necessary.

paulofaria
paulofaria previously approved these changes Jan 26, 2023
Copy link
Member

@paulofaria paulofaria left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@NeedleInAJayStack
Copy link
Member Author

Awesome, thanks Paulo! Mind approving again after my rebase?

@paulofaria
Copy link
Member

@NeedleInAJayStack thank you very much for this, Jay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants