🌊 A GraphQL implementation in Gleam
1import database
2import gleam/io
3import gleam/option
4import gleam/string
5import graphql_schema
6import swell/executor
7import swell/schema
8
9pub fn main() -> Nil {
10 io.println("🌊 Swell + SQLite Example")
11 io.println("=" |> string.repeat(50))
12 io.println("")
13
14 // Setup in-memory SQLite database
15 io.println("Setting up in-memory SQLite database...")
16 let assert Ok(conn) = database.setup_database()
17 io.println("✓ Database initialized with sample data")
18 io.println("")
19
20 // Build GraphQL schema
21 let graphql_schema = graphql_schema.build_schema(conn)
22 io.println("✓ GraphQL schema created")
23 io.println("")
24
25 // Example 1: Query all users
26 io.println("Query 1: Get all users")
27 io.println("-" |> string.repeat(50))
28 let query1 = "{ users { id name email } }"
29 io.println("GraphQL: " <> query1)
30 io.println("")
31
32 let ctx1 = schema.context(option.None)
33 case executor.execute(query1, graphql_schema, ctx1) {
34 Ok(executor.Response(data: data, errors: [])) -> {
35 io.println("Result: " <> string.inspect(data))
36 }
37 Ok(executor.Response(data: data, errors: errors)) -> {
38 io.println("Data: " <> string.inspect(data))
39 io.println("Errors: " <> string.inspect(errors))
40 }
41 Error(err) -> {
42 io.println("Error: " <> err)
43 }
44 }
45 io.println("")
46
47 // Example 2: Query a specific user by ID
48 io.println("Query 2: Get user with ID 1")
49 io.println("-" |> string.repeat(50))
50 let query2 = "{ user(id: 1) { id name email } }"
51 io.println("GraphQL: " <> query2)
52 io.println("")
53
54 let ctx2 = schema.context(option.None)
55 case executor.execute(query2, graphql_schema, ctx2) {
56 Ok(executor.Response(data: data, errors: [])) -> {
57 io.println("Result: " <> string.inspect(data))
58 }
59 Ok(executor.Response(data: data, errors: errors)) -> {
60 io.println("Data: " <> string.inspect(data))
61 io.println("Errors: " <> string.inspect(errors))
62 }
63 Error(err) -> {
64 io.println("Error: " <> err)
65 }
66 }
67 io.println("")
68
69 // Example 3: Query all posts
70 io.println("Query 3: Get all posts")
71 io.println("-" |> string.repeat(50))
72 let query3 = "{ posts { id title content authorId } }"
73 io.println("GraphQL: " <> query3)
74 io.println("")
75
76 let ctx3 = schema.context(option.None)
77 case executor.execute(query3, graphql_schema, ctx3) {
78 Ok(executor.Response(data: data, errors: [])) -> {
79 io.println("Result: " <> string.inspect(data))
80 }
81 Ok(executor.Response(data: data, errors: errors)) -> {
82 io.println("Data: " <> string.inspect(data))
83 io.println("Errors: " <> string.inspect(errors))
84 }
85 Error(err) -> {
86 io.println("Error: " <> err)
87 }
88 }
89 io.println("")
90
91 // Example 4: Create a new user with a mutation
92 io.println("Mutation 1: Create a new user")
93 io.println("-" |> string.repeat(50))
94 let mutation1 =
95 "mutation { createUser(input: { name: \"Stephanie Gilmore\", email: \"steph@surfmail.com\" }) { id name email } }"
96 io.println("GraphQL: " <> mutation1)
97 io.println("")
98
99 let ctx4 = schema.context(option.None)
100 case executor.execute(mutation1, graphql_schema, ctx4) {
101 Ok(executor.Response(data: data, errors: [])) -> {
102 io.println("Result: " <> string.inspect(data))
103 }
104 Ok(executor.Response(data: data, errors: errors)) -> {
105 io.println("Data: " <> string.inspect(data))
106 io.println("Errors: " <> string.inspect(errors))
107 }
108 Error(err) -> {
109 io.println("Error: " <> err)
110 }
111 }
112 io.println("")
113
114 // Example 5: Query the newly created user
115 io.println("Query 4: Verify the new user was created")
116 io.println("-" |> string.repeat(50))
117 let query5 = "{ users { id name email } }"
118 io.println("GraphQL: " <> query5)
119 io.println("")
120
121 let ctx5 = schema.context(option.None)
122 case executor.execute(query5, graphql_schema, ctx5) {
123 Ok(executor.Response(data: data, errors: [])) -> {
124 io.println("Result: " <> string.inspect(data))
125 }
126 Ok(executor.Response(data: data, errors: errors)) -> {
127 io.println("Data: " <> string.inspect(data))
128 io.println("Errors: " <> string.inspect(errors))
129 }
130 Error(err) -> {
131 io.println("Error: " <> err)
132 }
133 }
134 io.println("")
135
136 io.println("=" |> string.repeat(50))
137 io.println("✓ All examples completed!")
138}