Slide 1

Slide 1 text

SERVER-SIDE SWIFT AND GRAPHQL: A MATCH MADE IN ! HEAVEN OR HELL ? DEEP DISH SWIFT | CHICAGO, IL | MAY 2023 ELLEN SHAPIRO | MASTODON.SOCIAL/@DESIGNATEDNERD | PIXITEAPPS.COM

Slide 2

Slide 2 text

FULL ! DISCLOSURE @DesignatedNerd@mastodon.social

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

https://www.flickr.com/photos/fwooper7/9650957891

Slide 6

Slide 6 text

SOME DEFINITIONS @DesignatedNerd@mastodon.social

Slide 7

Slide 7 text

"Docker is a container virtualization platform that builds on tools like Virtual Box!" @DesignatedNerd@mastodon.social

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

SOME BASIC DEFINITIONS @DesignatedNerd@mastodon.social

Slide 10

Slide 10 text

SOME BASIC DEFINITIONS FOR A LOT OF APPS @DesignatedNerd@mastodon.social

Slide 11

Slide 11 text

YOUR APP'S DATA @DesignatedNerd@mastodon.social

Slide 12

Slide 12 text

YOUR APP'S DATA THE INFORMATION YOU WANT TO STORE, MANIPULATE AND/OR DISPLAY @DesignatedNerd@mastodon.social

Slide 13

Slide 13 text

THE SERVER @DesignatedNerd@mastodon.social

Slide 14

Slide 14 text

THE SERVER THE PLACE WHERE THE CANONICAL VERSION OF YOUR DATA LIVES @DesignatedNerd@mastodon.social

Slide 15

Slide 15 text

THE SERVER THE PLACE WHERE THE CANONICAL VERSION OF YOUR DATA LIVES (Eventually)

Slide 16

Slide 16 text

THE SERVER > Some way to store data @DesignatedNerd@mastodon.social

Slide 17

Slide 17 text

THE SERVER > Some way to store data > Some way to fetch data from storage @DesignatedNerd@mastodon.social

Slide 18

Slide 18 text

THE SERVER > Some way to store data > Some way to fetch data from storage > Some way to receive requests for data @DesignatedNerd@mastodon.social

Slide 19

Slide 19 text

THE SERVER > Some way to store data > Some way to fetch data from storage > Some way to receive requests for data > Some way to return responses for requested data @DesignatedNerd@mastodon.social

Slide 20

Slide 20 text

THE SERVER > Some way to store data > Some way to fetch data from storage > Some way to receive requests for data > Some way to return responses for requested data > Some security mechanisms @DesignatedNerd@mastodon.social

Slide 21

Slide 21 text

THE SERVER @DesignatedNerd@mastodon.social

Slide 22

Slide 22 text

SERVER-SIDE SWIFT @DesignatedNerd@mastodon.social

Slide 23

Slide 23 text

SERVER-SIDE SWIFT INFRASTRUCTURE TO BUILD AND RUN THE SERVER IN SWIFT @DesignatedNerd@mastodon.social

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

VAPOR @DesignatedNerd@mastodon.social

Slide 26

Slide 26 text

VAPOR A SWIFT LIBRARY FOR RUNNING YOUR SERVER @DesignatedNerd@mastodon.social

Slide 27

Slide 27 text

FLUENT @DesignatedNerd@mastodon.social

Slide 28

Slide 28 text

FLUENT A SWIFT ADAPTER FOR TALKING TO MULTIPLE DATA STORAGE TYPES @DesignatedNerd@mastodon.social

Slide 29

Slide 29 text

THE SERVER > Some way to store data > Some way to fetch data from storage > Some way to receive requests for data > Some way to return responses for requested data > Some security mechanisms @DesignatedNerd@mastodon.social

Slide 30

Slide 30 text

THE SERVER > Some way to store data (Fluent) > Some way to fetch data from storage (Fluent) > Some way to receive requests for data > Some way to return responses for requested data > Some security mechanisms @DesignatedNerd@mastodon.social

Slide 31

Slide 31 text

THE SERVER > Some way to store data (Fluent) > Some way to fetch data from storage (Fluent) > Some way to receive requests for data (Vapor) > Some way to return responses for requested data (Vapor) > Some security mechanisms (mostly Vapor) @DesignatedNerd@mastodon.social

Slide 32

Slide 32 text

A CLIENT @DesignatedNerd@mastodon.social

Slide 33

Slide 33 text

A CLIENT A THING THAT INTERACTS WITH DATA FROM YOUR SERVER @DesignatedNerd@mastodon.social

Slide 34

Slide 34 text

A CLIENT > Requests data from the server @DesignatedNerd@mastodon.social

Slide 35

Slide 35 text

A CLIENT > Requests data from the server > Processes and displays data from the server @DesignatedNerd@mastodon.social

Slide 36

Slide 36 text

A CLIENT > Requests data from the server > Processes and displays data from the server > Processes and displays user input @DesignatedNerd@mastodon.social

Slide 37

Slide 37 text

A CLIENT > Requests data from the server > Processes and displays data from the server > Processes and displays user input > Sends updated data to the server @DesignatedNerd@mastodon.social

Slide 38

Slide 38 text

A CLIENT @DesignatedNerd@mastodon.social

Slide 39

Slide 39 text

EXAMPLES OF CLIENTS > Website @DesignatedNerd@mastodon.social

Slide 40

Slide 40 text

EXAMPLES OF CLIENTS > Website > iOS App > Android App @DesignatedNerd@mastodon.social

Slide 41

Slide 41 text

EXAMPLES OF CLIENTS > Website > iOS App > Android App > Another server not owned by you @DesignatedNerd@mastodon.social

Slide 42

Slide 42 text

EXAMPLES OF CLIENTS > Website > iOS App > Android App > Another server not owned by you > An IoT Device @DesignatedNerd@mastodon.social

Slide 43

Slide 43 text

Photo via https://flickr.com/photos/ockam/16307930064

Slide 44

Slide 44 text

THE PUBLIC API @DesignatedNerd@mastodon.social

Slide 45

Slide 45 text

THE PUBLIC API THE DEFINITION OF HOW THE CLIENT AND SERVER CAN TALK TO EACH OTHER @DesignatedNerd@mastodon.social

Slide 46

Slide 46 text

APPLICATION PROGRAMMING INTERFACE @DesignatedNerd@mastodon.social

Slide 47

Slide 47 text

INTERFACE @DesignatedNerd@mastodon.social

Slide 48

Slide 48 text

REST @DesignatedNerd@mastodon.social

Slide 49

Slide 49 text

REST REPRESENTATIONAL STATE TRANSFER @DesignatedNerd@mastodon.social

Slide 50

Slide 50 text

api/user/1 { "id": "1", "name": "Bart Simpson" "age": "10" "family_member_ids": [ "2", "3", "4", "5" ] } @DesignatedNerd@mastodon.social

Slide 51

Slide 51 text

api/user/1: { "id": "1", "name": "Bart Simpson" "age": "10" "family_members": [ { "id": "2", "name": "Lisa Simpson", "age": 8 }, { "id": 3", "name": "Homer Simpson", "age": 39 }, { "id": "4", "name": "Marge Simpson", "age": 39 }, { "id": "5", "name": "Maggie Simpson", "age": 1 } ] } @DesignatedNerd@mastodon.social

Slide 52

Slide 52 text

@DesignatedNerd@mastodon.social

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Photo via https://flickr.com/photos/johnath/7356295658

Slide 55

Slide 55 text

VAPOR HAS BUILT IN REST SUPPORT @DesignatedNerd@mastodon.social

Slide 56

Slide 56 text

GRAPHQL @DesignatedNerd@mastodon.social

Slide 57

Slide 57 text

GRAPHQL A TYPE OF PUBLIC API THAT ALLOWS CLIENTS TO REQUEST SPECIFIC FIELDS FROM THE SERVER @DesignatedNerd@mastodon.social

Slide 58

Slide 58 text

Source: https://flickr.com/photos/acaben/2816139/

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

WHAT CAN I ASK FOR? HOW CAN I ASK FOR IT? @DesignatedNerd@mastodon.social

Slide 61

Slide 61 text

REST: THERE IS NO STANDARD WAY @DesignatedNerd@mastodon.social

Slide 62

Slide 62 text

GOOD: OPENAPI (FKA SWAGGER) @DesignatedNerd@mastodon.social

Slide 63

Slide 63 text

GOOD: OPENAPI (FKA SWAGGER) @DesignatedNerd@mastodon.social

Slide 64

Slide 64 text

LESS GOOD... @DesignatedNerd@mastodon.social

Slide 65

Slide 65 text

"Oh no, we don't have any place where all the HTTP endpoints are written down. Just grep the codebase of the monolith, that should get you what you need." @DesignatedNerd@mastodon.social

Slide 66

Slide 66 text

"Oh no, we don't have any place where all the HTTP endpoints are written down. Just grep the codebase of the monolith, that should get you what you need." (An actual thing someone told me to do the first week of a new job)

Slide 67

Slide 67 text

! @DesignatedNerd@mastodon.social

Slide 68

Slide 68 text

THE GRAPHQL SCHEMA @DesignatedNerd@mastodon.social

Slide 69

Slide 69 text

THE GRAPHQL SCHEMA A CONTRACT OF WHAT CAN BE REQUESTED, WHAT WILL BE RETURNED, AND HOW @DesignatedNerd@mastodon.social

Slide 70

Slide 70 text

IN THE SCHEMA > Scalars @DesignatedNerd@mastodon.social

Slide 71

Slide 71 text

IN THE SCHEMA > Scalars > Custom Types @DesignatedNerd@mastodon.social

Slide 72

Slide 72 text

IN THE SCHEMA > Scalars > Custom Types > Fields/Properties @DesignatedNerd@mastodon.social

Slide 73

Slide 73 text

IN THE SCHEMA > Scalars > Custom Types > Fields/Properties > Relationships (inferred) @DesignatedNerd@mastodon.social

Slide 74

Slide 74 text

A NOTE ABOUT GRAPHQL ❓ OPTIONALITY @DesignatedNerd@mastodon.social

Slide 75

Slide 75 text

is Optional Swift Type GraphQL Type @DesignatedNerd@mastodon.social

Slide 76

Slide 76 text

is Optional Swift Type GraphQL Type No @DesignatedNerd@mastodon.social

Slide 77

Slide 77 text

is Optional Swift Type GraphQL Type No String @DesignatedNerd@mastodon.social

Slide 78

Slide 78 text

is Optional Swift Type GraphQL Type No String String! @DesignatedNerd@mastodon.social

Slide 79

Slide 79 text

! @DesignatedNerd@mastodon.social

Slide 80

Slide 80 text

GRAPHQL TYPES ARE NULLABLE BY DEFAULT @DesignatedNerd@mastodon.social

Slide 81

Slide 81 text

is Optional Swift Type GraphQL Type No String String! @DesignatedNerd@mastodon.social

Slide 82

Slide 82 text

is Optional Swift Type GraphQL Type No String String! Yes @DesignatedNerd@mastodon.social

Slide 83

Slide 83 text

is Optional Swift Type GraphQL Type No String String! Yes String? @DesignatedNerd@mastodon.social

Slide 84

Slide 84 text

is Optional Swift Type GraphQL Type No String String! Yes String? String @DesignatedNerd@mastodon.social

Slide 85

Slide 85 text

A GRAPHQL OPERATION @DesignatedNerd@mastodon.social

Slide 86

Slide 86 text

A GRAPHQL OPERATION A DEFINITION OF A REQUEST TO FETCH, CHANGE, OR LISTEN TO CHANGES FOR YOUR DATA @DesignatedNerd@mastodon.social

Slide 87

Slide 87 text

OPERATIONS > Query: I'd like some info, please @DesignatedNerd@mastodon.social

Slide 88

Slide 88 text

OPERATIONS > Query: I'd like some info, please > Mutation: I'd like to make a change @DesignatedNerd@mastodon.social

Slide 89

Slide 89 text

OPERATIONS > Query: I'd like some info, please > Mutation: I'd like to make a change > Subscription: Keep me up to date on changes @DesignatedNerd@mastodon.social

Slide 90

Slide 90 text

OPERATIONS > Query: I'd like some info, please > Mutation: I'd like to make a change > Subscription: Keep me up to date on changes @DesignatedNerd@mastodon.social

Slide 91

Slide 91 text

@DesignatedNerd@mastodon.social

Slide 92

Slide 92 text

HOW DO WE COMBINE VAPOR AND GRAPHQL? @DesignatedNerd@mastodon.social

Slide 93

Slide 93 text

Photo: https://flickr.com/photos/daveseven/6138666188

Slide 94

Slide 94 text

`HTTPS://GITHUB.COM/DESIGNATEDNERD/ MOVEITMOVEIT @DesignatedNerd@mastodon.social

Slide 95

Slide 95 text

GRAPHQLKIT @DesignatedNerd@mastodon.social

Slide 96

Slide 96 text

dependencies: [ .package(url: "https://github.com/alexsteinerde/graphql-kit.git", from: "2.0.0"), ], @DesignatedNerd@mastodon.social

Slide 97

Slide 97 text

@DesignatedNerd@mastodon.social

Slide 98

Slide 98 text

WHAT IS ALL THIS? @DesignatedNerd@mastodon.social

Slide 99

Slide 99 text

Source: https://flickr.com/photos/xurble/378943497

Slide 100

Slide 100 text

Source: https://flickr.com/photos/xurble/378943497

Slide 101

Slide 101 text

Source: https://flickr.com/photos/xurble/378943497

Slide 102

Slide 102 text

HIGHEST LEVEL ⬇ LOWEST LEVEL @DesignatedNerd@mastodon.social

Slide 103

Slide 103 text

GRAPHQLKIT @DesignatedNerd@mastodon.social

Slide 104

Slide 104 text

GRAPHQLKIT EASY GRAPHQL INTEGRATION WITH A VAPOR SERVER @DesignatedNerd@mastodon.social

Slide 105

Slide 105 text

GRAPHITI @DesignatedNerd@mastodon.social

Slide 106

Slide 106 text

GRAPHITI HELPS YOU BUILD A GRAPHQL SCHEMA IN SWIFT @DesignatedNerd@mastodon.social

Slide 107

Slide 107 text

GRAPHQLSWIFT/GRAPHQL @DesignatedNerd@mastodon.social

Slide 108

Slide 108 text

GRAPHQLSWIFT/GRAPHQL THE ABSOLUTE BASE HANDLING OF GQL IN SWIFT @DesignatedNerd@mastodon.social

Slide 109

Slide 109 text

VAPOR + FLUENT @DesignatedNerd@mastodon.social

Slide 110

Slide 110 text

VAPOR + FLUENT (YOU KNOW THESE TWO ALREADY) @DesignatedNerd@mastodon.social

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

// These all get pulled automatically with GraphQL kit, but you need // to explicitly declare them as dependencies to use them directly. .package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "4.0.0")), .package(url: "https://github.com/vapor/fluent.git", .upToNextMajor(from: "4.0.0")), .package(url: "https://github.com/vapor/fluent-sqlite-driver.git", .upToNextMajor(from: "4.0.0")), .package(url: "https://github.com/GraphQLSwift/Graphiti", .upToNextMinor(from: "0.26.0")), @DesignatedNerd@mastodon.social

Slide 113

Slide 113 text

.target(name: "App", dependencies: [ .product(name: "Fluent", package: "fluent"), .product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"), .product(name: "Vapor", package: "vapor"), .product(name: "GraphQLKit", package: "graphql-kit"), .product(name: "GraphiQLVapor", package: "graphiql-vapor"), ]), @DesignatedNerd@mastodon.social

Slide 114

Slide 114 text

LET'S BUILD A GRAPHQL API! @DesignatedNerd@mastodon.social

Slide 115

Slide 115 text

BACKING OBJECT CREATION @DesignatedNerd@mastodon.social

Slide 116

Slide 116 text

BACKING OBJECT CREATION: THE SAME AS FOR REST @DesignatedNerd@mastodon.social

Slide 117

Slide 117 text

final class User: Model, Content { static var schema = "users" @ID(key: .id) var id: UUID? @Field(key: "email") var email: String @Field(key: "name") var name: String @Field(key: "hashedPassword") var hashedPassword: String @Children(for: \.$user) var moves: [Move] // ... } @DesignatedNerd@mastodon.social

Slide 118

Slide 118 text

final class User: Model, Content { static var schema = "users" @ID(key: .id) var id: UUID? @Field(key: "email") var email: String @Field(key: "name") var name: String @Field(key: "hashedPassword") var hashedPassword: String @Children(for: \.$user) var moves: [Move] // ... } @DesignatedNerd@mastodon.social

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

final class User: Model, Content { static var schema = "users" @ID(key: .id) var id: UUID? @Field(key: "email") var email: String @Field(key: "name") var name: String @Field(key: "hashedPassword") var hashedPassword: String @Children(for: \.$user) var moves: [Move] // ... } @DesignatedNerd@mastodon.social

Slide 121

Slide 121 text

final class User: Model, Content { static var schema = "users" @ID(key: .id) var id: UUID? @Field(key: "email") var email: String @Field(key: "name") var name: String @Field(key: "hashedPassword") var hashedPassword: String @Children(for: \.$user) var moves: [Move] // ... } @DesignatedNerd@mastodon.social

Slide 122

Slide 122 text

final class User: Model, Content { static var schema = "users" @ID(key: .id) var id: UUID? @Field(key: "email") var email: String @Field(key: "name") var name: String @Field(key: "hashedPassword") var hashedPassword: String @Children(for: \.$user) var moves: [Move] // ... } @DesignatedNerd@mastodon.social

Slide 123

Slide 123 text

final class User: Model, Content { static var schema = "users" @ID(key: .id) var id: UUID? @Field(key: "email") var email: String @Field(key: "name") var name: String @Field(key: "hashedPassword") var hashedPassword: String @Children(for: \.$user) var moves: [Move] // ... } @DesignatedNerd@mastodon.social

Slide 124

Slide 124 text

final class User: Model, Content { static var schema = "users" enum DatabaseKeys: String { case email case name case hashedPassword } @ID(key: .id) var id: UUID? @Field(key: "email") var email: String @Field(key: "name") var name: String @Field(key: "hashedPassword") var hashedPassword: String @Children(for: \.$user) var moves: [Move] // ... } @DesignatedNerd@mastodon.social

Slide 125

Slide 125 text

final class User: Model, Content { static var schema = "users" enum DatabaseKeys: String { case email case name case hashedPassword } @ID(key: .id) var id: UUID? @Field(key: DatabaseKeys.email.fieldKey) var email: String @Field(key: DatabaseKeys.name.fieldKey) var name: String @Field(key: DatabaseKeys.hashedPassword.fieldKey) var hashedPassword: String @Children(for: \.$user) var moves: [Move] // ... } @DesignatedNerd@mastodon.social

Slide 126

Slide 126 text

CREATING A CONFIG @DesignatedNerd@mastodon.social

Slide 127

Slide 127 text

// configures your application public func configure(_ app: Application) throws { app.databases.use(.sqlite(.memory), as: .sqlite) // Create the appropriate migrations app.migrations.add(User.Migration()) app.migrations.add(Move.Migration()) try app.autoMigrate().wait() // Register the schema and its resolver. // TODO } @DesignatedNerd@mastodon.social

Slide 128

Slide 128 text

// configures your application public func configure(_ app: Application) throws { app.databases.use(.sqlite(.memory), as: .sqlite) // Create the appropriate migrations app.migrations.add(User.Migration()) app.migrations.add(Move.Migration()) try app.autoMigrate().wait() // Register the schema and its resolver. // TODO } @DesignatedNerd@mastodon.social

Slide 129

Slide 129 text

// configures your application public func configure(_ app: Application) throws { app.databases.use(.sqlite(.memory), as: .sqlite) // Create the appropriate migrations app.migrations.add(User.Migration()) app.migrations.add(Move.Migration()) try app.autoMigrate().wait() // Register the schema and its resolver. // TODO } @DesignatedNerd@mastodon.social

Slide 130

Slide 130 text

RESOLVER @DesignatedNerd@mastodon.social

Slide 131

Slide 131 text

RESOLVER FUNCTION TO GET DATA AND GIVE IT TO GQL @DesignatedNerd@mastodon.social

Slide 132

Slide 132 text

BARE-BONES RESOLVER class GraphQLResolver { } @DesignatedNerd@mastodon.social

Slide 133

Slide 133 text

DECLARING YOUR SCHEMA @DesignatedNerd@mastodon.social

Slide 134

Slide 134 text

let movingSchema = try! Schema { Type(User.self) { Field("id", at: \.id) Field("email", at: \.email) Field("name", at: \.name) } } @DesignatedNerd@mastodon.social

Slide 135

Slide 135 text

let movingSchema = try! Schema { Type(User.self) { Field("id", at: \.id) Field("email", at: \.email) Field("name", at: \.name) } } @DesignatedNerd@mastodon.social

Slide 136

Slide 136 text

let movingSchema = try! Schema { Type(User.self) { Field("id", at: \.id) Field("email", at: \.email) Field("name", at: \.name) } } @DesignatedNerd@mastodon.social

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

// configure.swift // Register the schema and its resolver. app.register(graphQLSchema: movingSchema, withResolver: GraphQLResolver()) @DesignatedNerd@mastodon.social

Slide 139

Slide 139 text

@DesignatedNerd@mastodon.social

Slide 140

Slide 140 text

"YOU FORGOT TO DECLARE THIS TYPE IN THE SCHEMA, YA DING DONG" @DesignatedNerd@mastodon.social

Slide 141

Slide 141 text

let movingSchema = try! Schema { Scalar(UUID.self) Type(User.self) { Field("id", at: \.id) Field("email", at: \.email) Field("name", at: \.name) } } @DesignatedNerd@mastodon.social

Slide 142

Slide 142 text

! @DesignatedNerd@mastodon.social

Slide 143

Slide 143 text

let movingSchema = try! Schema { Scalar(UUID.self) Type(User.self) { Field("id", at: \.id) Field("email", at: \.email) Field("name", at: \.name) } } @DesignatedNerd@mastodon.social

Slide 144

Slide 144 text

@DesignatedNerd@mastodon.social

Slide 145

Slide 145 text

@DesignatedNerd@mastodon.social

Slide 146

Slide 146 text

let movingSchema = try! Schema { Scalar(UUID.self) Type(User.self) { Field("id", at: \.id, as: UUID.self) Field("email", at: \.email) Field("name", at: \.name) } } @DesignatedNerd@mastodon.social

Slide 147

Slide 147 text

@DesignatedNerd@mastodon.social

Slide 148

Slide 148 text

@DesignatedNerd@mastodon.social

Slide 149

Slide 149 text

!❗ @DesignatedNerd@mastodon.social

Slide 150

Slide 150 text

HANDLING OPERATIONS @DesignatedNerd@mastodon.social

Slide 151

Slide 151 text

Mutation { Field("createUser", at: GraphQLResolver.createUser) { Argument("name", at: \.name) Argument("email", at: \.email) Argument("password", at: \.password) } } @DesignatedNerd@mastodon.social

Slide 152

Slide 152 text

Mutation { Field("createUser", at: GraphQLResolver.createUser) { Argument("name", at: \.name) Argument("email", at: \.email) Argument("password", at: \.password) } } @DesignatedNerd@mastodon.social

Slide 153

Slide 153 text

Mutation { Field("createUser", at: GraphQLResolver.createUser) { Argument("name", at: \.name) Argument("email", at: \.email) Argument("password", at: \.password) } } @DesignatedNerd@mastodon.social

Slide 154

Slide 154 text

struct UserCreateArguments: Codable { let name: String let email: String let password: String } extension GraphQLResolver { func createUser(request: Request, arguments: UserCreateArguments) throws -> EventLoopFuture { let user = try User(email: arguments.email, password: arguments.password, name: arguments.name) return user.create(on: request.db).map { user } } } @DesignatedNerd@mastodon.social

Slide 155

Slide 155 text

struct UserCreateArguments: Codable { let name: String let email: String let password: String } extension GraphQLResolver { func createUser(request: Request, arguments: UserCreateArguments) throws -> EventLoopFuture { let user = try User(email: arguments.email, password: arguments.password, name: arguments.name) return user.create(on: request.db).map { user } } } @DesignatedNerd@mastodon.social

Slide 156

Slide 156 text

struct UserCreateArguments: Codable { let name: String let email: String let password: String } extension GraphQLResolver { func createUser(request: Request, arguments: UserCreateArguments) throws -> EventLoopFuture { let user = try User(email: arguments.email, password: arguments.password, name: arguments.name) return user.create(on: request.db).map { user } } } @DesignatedNerd@mastodon.social

Slide 157

Slide 157 text

Mutation { Field("createUser", at: GraphQLResolver.createUser) { Argument("name", at: \.name) Argument("email", at: \.email) Argument("password", at: \.password) } } @DesignatedNerd@mastodon.social

Slide 158

Slide 158 text

Mutation { Field("createUser", at: GraphQLResolver.createUser) { Argument("name", at: \.name) Argument("email", at: \.email) Argument("password", at: \.password) } } @DesignatedNerd@mastodon.social

Slide 159

Slide 159 text

TRYING IT OUT @DesignatedNerd@mastodon.social

Slide 160

Slide 160 text

EASIEST: GRAPHIQL @DesignatedNerd@mastodon.social

Slide 161

Slide 161 text

// Something to help visualize and test your schema and operations .package(url: "https://github.com/alexsteinerde/graphiql-vapor.git", .upToNextMajor(from: "2.0.0")), @DesignatedNerd@mastodon.social

Slide 162

Slide 162 text

.target(name: "App", dependencies: [ // (existing dependencies) .product(name: "GraphiQLVapor", package: "graphiql-vapor"), ]), @DesignatedNerd@mastodon.social

Slide 163

Slide 163 text

import GraphiQLVapor public func configure(_ app: Application) throws { //... (stuff you already saw) // Enable GraphiQL web page for built-in goodness if !app.environment.isRelease { app.enableGraphiQL() } } @DesignatedNerd@mastodon.social

Slide 164

Slide 164 text

No content

Slide 165

Slide 165 text

@DesignatedNerd@mastodon.social

Slide 166

Slide 166 text

No content

Slide 167

Slide 167 text

@DesignatedNerd@mastodon.social

Slide 168

Slide 168 text

@DesignatedNerd@mastodon.social

Slide 169

Slide 169 text

APOLLO STUDIO HTTPS://STUDIO.APOLLOGRAPHQL.COM/SANDBOX/EXPLORER @DesignatedNerd@mastodon.social

Slide 170

Slide 170 text

@DesignatedNerd@mastodon.social

Slide 171

Slide 171 text

@DesignatedNerd@mastodon.social

Slide 172

Slide 172 text

@DesignatedNerd@mastodon.social

Slide 173

Slide 173 text

No content

Slide 174

Slide 174 text

@DesignatedNerd@mastodon.social

Slide 175

Slide 175 text

CORS: CROSS-ORIGIN RESOURCE SHARING @DesignatedNerd@mastodon.social

Slide 176

Slide 176 text

CORS: CROSS-ORIGIN RESOURCE SHARING (DISABLED BY DEFAULT IN VAPOR) @DesignatedNerd@mastodon.social

Slide 177

Slide 177 text

let corsConfiguration = CORSMiddleware.Configuration( allowedOrigin: .custom("https://studio.apollographql.com"), allowedMethods: [.POST], allowedHeaders: [ .accept, .contentType, .accessControlAllowOrigin, .origin ] ) let cors = CORSMiddleware(configuration: corsConfiguration) app.middleware.use(cors) @DesignatedNerd@mastodon.social

Slide 178

Slide 178 text

! PITFALLS @DesignatedNerd@mastodon.social

Slide 179

Slide 179 text

LOTS OF FIGHTING WITH THE COMPILER @DesignatedNerd@mastodon.social

Slide 180

Slide 180 text

No content

Slide 181

Slide 181 text

THERE CAN ONLY BE ONE (RESOLVER CLASS) @DesignatedNerd@mastodon.social

Slide 182

Slide 182 text

class MoveResolver { private let userResolver = UserResolver() // MARK: - Pass-Through Methods func createUser(request: Request, arguments: UserCreateArguments) throws -> EventLoopFuture { try userResolver.createUser(request: request, arguments: arguments) } //... } @DesignatedNerd@mastodon.social

Slide 183

Slide 183 text

GraphQLResolver+Moves.swift class GraphQLResolver { func createMove(request: Request, arguments: CreateMoveArguments) -> EventLoopFuture { //... } } GraphQLResolver+Users.swift extension GraphQLResolver func createUser(request: Request, arguments: UserCreateArguments) throws -> EventLoopFuture { //... } } @DesignatedNerd@mastodon.social

Slide 184

Slide 184 text

DATA LOADING INEFFICIENCIES @DesignatedNerd@mastodon.social

Slide 185

Slide 185 text

DATA LOADER @DesignatedNerd@mastodon.social

Slide 186

Slide 186 text

404 DATA LOADER NOT FOUND @DesignatedNerd@mastodon.social

Slide 187

Slide 187 text

@DesignatedNerd@mastodon.social

Slide 188

Slide 188 text

THE VERDICT: @DesignatedNerd@mastodon.social

Slide 189

Slide 189 text

THE VERDICT: IT DEPENDS @DesignatedNerd@mastodon.social

Slide 190

Slide 190 text

No content

Slide 191

Slide 191 text

THE VERDICTS: > ! If you know a lot about both Vapor/Fluent AND GQL @DesignatedNerd@mastodon.social

Slide 192

Slide 192 text

THE VERDICTS > ! If you know a lot about both Vapor/Fluent AND GQL > " If you know a lot about Vapor/Fluent but not GQL @DesignatedNerd@mastodon.social

Slide 193

Slide 193 text

THE VERDICTS > ! If you know a lot about both Vapor/Fluent AND GQL > " If you know a lot about Vapor/Fluent but not GQL > # If you know a lot about GQL but not Vapor/Fluent @DesignatedNerd@mastodon.social

Slide 194

Slide 194 text

THE VERDICTS > ! If you know a lot about both Vapor/Fluent AND GQL > " If you know a lot about Vapor/Fluent but not GQL > # If you know a lot about GQL but not Vapor/Fluent > $ If you don't know a lot about either one @DesignatedNerd@mastodon.social

Slide 195

Slide 195 text

No content

Slide 196

Slide 196 text

OBLIGATORY SUMMARY SLIDE @DesignatedNerd@mastodon.social

Slide 197

Slide 197 text

OBLIGATORY SUMMARY SLIDE > ! If you know a lot about both Vapor/Fluent AND GQL > " If you know a lot about Vapor/Fluent but not GQL > # If you know a lot about GQL but not Vapor/Fluent > $ If you don't know a lot about either one @DesignatedNerd@mastodon.social

Slide 198

Slide 198 text

THANK YOU! @DesignatedNerd@mastodon.social

Slide 199

Slide 199 text

LINKS! > Vapor/Fluent Docs: https://docs.vapor.codes/ > GraphQLKit: https://github.com/alexsteinerde/ graphql-kit/ > (slightly outdated) Tutorial on getting started: https://www.kodeco.com/21148796-graphql- tutorial-for-server-side-swift-with-vapor- getting-started @DesignatedNerd@mastodon.social