![600_521069976](/_cms/media/600_521069976.webp)
May Swift Meetup Recap | GraphQL, Swift, and Vapor
![600_521069976](/_cms/media/600_521069976.webp)
Unlocking the Power of GraphQL with Swift and Vapor: A Comprehensive Overview
As the main contributor to the GraphQL Swift implementation, Jay provided an in-depth look at the PassiveLogic team’s backend web services and cloud infrastructure. He discussed the enhancements made to the GraphQL Swift library, such as the addition of subscriptions, web socket protocols, and validation rules. Highlighting the superiority of GraphQL over traditional REST APIs, Jay explained how the Swift NIO asynchronous eventing protocol and the GraphQL Swift organization on GitHub streamline development. He introduced key tools like Graphiti, a Swift package simplifying GraphQL schema definitions, and Pioneer, a Vapor middleware for delivering GraphQL APIs, both of which are designed to enhance GraphQL API development and visualization.
Getting Started with Vapor CLI and Setting Up a GraphQL API
Jay's hands-on tutorial began with installing and using the Vapor CLI to create a new Swift project named "Vapor GraphQL." He guided attendees through initial setup and configuration, demonstrating how to download and add new package dependencies.
Creating a Simple GraphQL API with Vapor
Jay elaborated on the process of creating a simple GraphQL API using Vapor. He discussed the importance of creating a “context” and “resolver” for the API, explaining that the context provides necessary information for the resolver function, which in turn returns the desired data. He also demonstrated how to define a schema and integrate the resolver function at the top level of the GraphQL API. By integrating the API with Vapor, features like introspection and a graphical web app for better visualization are enabled.
Exploring Vapor Middleware and GraphQL Endpoint Functionality
During his presentation, Jay showcased the functionality of Vapor Middleware with a GraphQL endpoint. He highlighted features such as auto-complete and strong typing, and illustrated how to define an API with arguments. Adding a 'Hello' argument to the API, which prompts for a 'Name' argument when called, demonstrates the middleware's simplicity and potential for more complex resolvers.
Crafting Custom Types and Resolvers in GraphQL
Toward the end of the meeting, Jay delved into the creation of new types in GraphQL. He explained how to add these custom types into the schema and create resolvers to return specific types. He also showed how to perform traversals to access information from related types and verify that newly created resolvers execute correctly when specific fields are requested.
Understanding GraphQL, WebSockets, and Swift Compatibility
The evening was wrapped up by explaining the differences between HTTP and web socket requests, and how GraphQL customizes subscriptions. He demonstrated setting up a web socket to increment a counter every second, visible in the browser's network tab. The group discussed using macros to simplify type definitions and addressed GraphQL schema versioning challenges, suggesting deprecation of unused features or creating separate endpoints for different versions. He introduced the concept of Federated GraphQL, enabling multiple servers to be stitched together for comprehensive data sourcing, and highlighted the native Swift support for data loaders and the Apollo iOS client library compatibility.
Resources
Here is a link to the GraphQL API created during the presentation. If you would like to watch the whole Meetup Zoom recording, you can find that here; passcode: AA$^I^8$
Stay tuned for next Meetup! PassiveLogic hosts a Swift Developers Meetup every month. If you can’t join in person, you can always join us via Zoom. Join the Utah Swift Developers Meetup group here. You can join the group here and receive alerts when we have an upcoming event.
Follow PassiveLogic on LinkedIn and X(Twitter) to stay up to date on the company’s latest news.