+17
-8
example/src/database.gleam
+17
-8
example/src/database.gleam
···
83
83
}
84
84
85
85
/// Get a user by ID
86
-
pub fn get_user(
87
-
conn: sqlight.Connection,
88
-
user_id: Int,
89
-
) -> Result(User, String) {
86
+
pub fn get_user(conn: sqlight.Connection, user_id: Int) -> Result(User, String) {
90
87
let sql = "SELECT id, name, email FROM users WHERE id = ?"
91
88
92
89
let decoder = {
···
97
94
}
98
95
99
96
case
100
-
sqlight.query(sql, on: conn, with: [sqlight.int(user_id)], expecting: decoder)
97
+
sqlight.query(
98
+
sql,
99
+
on: conn,
100
+
with: [sqlight.int(user_id)],
101
+
expecting: decoder,
102
+
)
101
103
{
102
104
Ok([user]) -> Ok(user)
103
105
Ok([]) -> Error("User not found")
···
135
137
}
136
138
137
139
case
138
-
sqlight.query(sql, on: conn, with: [sqlight.int(post_id)], expecting: decoder)
140
+
sqlight.query(
141
+
sql,
142
+
on: conn,
143
+
with: [sqlight.int(post_id)],
144
+
expecting: decoder,
145
+
)
139
146
{
140
147
Ok([post]) -> Ok(post)
141
148
Ok([]) -> Error("Post not found")
···
149
156
conn: sqlight.Connection,
150
157
author_id: Int,
151
158
) -> List(Post) {
152
-
let sql = "SELECT id, title, content, author_id FROM posts WHERE author_id = ?"
159
+
let sql =
160
+
"SELECT id, title, content, author_id FROM posts WHERE author_id = ?"
153
161
154
162
let decoder = {
155
163
use id <- decode.field(0, decode.int)
···
174
182
name: String,
175
183
email: String,
176
184
) -> Result(User, String) {
177
-
let sql = "INSERT INTO users (name, email) VALUES (?, ?) RETURNING id, name, email"
185
+
let sql =
186
+
"INSERT INTO users (name, email) VALUES (?, ?) RETURNING id, name, email"
178
187
179
188
let decoder = {
180
189
use id <- decode.field(0, decode.int)
+18
-12
example/src/graphql_schema.gleam
+18
-12
example/src/graphql_schema.gleam
···
117
117
/// Build the Query type
118
118
pub fn query_type(conn: sqlight.Connection) -> schema.Type {
119
119
schema.object_type("Query", "Root query type", [
120
-
schema.field("users", schema.list_type(user_type()), "Get all users", fn(
121
-
_ctx,
122
-
) {
123
-
let users = database.get_users(conn)
124
-
Ok(value.List(list.map(users, user_to_value)))
125
-
}),
120
+
schema.field(
121
+
"users",
122
+
schema.list_type(user_type()),
123
+
"Get all users",
124
+
fn(_ctx) {
125
+
let users = database.get_users(conn)
126
+
Ok(value.List(list.map(users, user_to_value)))
127
+
},
128
+
),
126
129
schema.field_with_args(
127
130
"user",
128
131
user_type(),
···
158
161
}
159
162
},
160
163
),
161
-
schema.field("posts", schema.list_type(post_type()), "Get all posts", fn(
162
-
_ctx,
163
-
) {
164
-
let posts = database.get_posts(conn)
165
-
Ok(value.List(list.map(posts, post_to_value)))
166
-
}),
164
+
schema.field(
165
+
"posts",
166
+
schema.list_type(post_type()),
167
+
"Get all posts",
168
+
fn(_ctx) {
169
+
let posts = database.get_posts(conn)
170
+
Ok(value.List(list.map(posts, post_to_value)))
171
+
},
172
+
),
167
173
schema.field_with_args(
168
174
"post",
169
175
post_type(),