Skip to content

Commit 5fb1919

Browse files
authored
Merge pull request #1 from korir248/main
Comments depicting what the code does
2 parents 3c01675 + 56217ef commit 5fb1919

File tree

7 files changed

+24
-13
lines changed

7 files changed

+24
-13
lines changed

examples/web_app_graphql/src/app_env_vars.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use serde::Deserialize;
22

3+
//Environment variables required for the app to run
34
#[derive(Deserialize, Debug, Clone)]
45
pub struct AppEnvVars {
56
pub meilisearch_api_key: String,

examples/web_app_graphql/src/graphql_schema/users/get_users.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct GetUsers;
88

99
#[Object]
1010
impl GetUsers {
11+
//Resolver for querying the database for user records
1112
pub async fn get_users(&self, ctx: &Context<'_>) -> Result<Vec<User>> {
1213
use crate::schema::users::dsl::users;
1314

examples/web_app_graphql/src/graphql_schema/users/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ pub mod search;
55
use get_users::GetUsers;
66
use search::SearchUsers;
77

8+
//Combines user queries into one struct
89
#[derive(Default, MergedObject)]
910
pub struct UsersQuery(pub GetUsers, pub SearchUsers);

examples/web_app_graphql/src/graphql_schema/users/search.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,40 @@ impl SearchUsers {
1212
async fn search(&self, ctx: &Context<'_>, query_string: String) -> Result<Vec<User>> {
1313
use crate::schema::users::dsl::users;
1414

15-
let app_data = ctx.data::<GraphQlData>().map_err(|e| {
15+
let GraphQlData { pool, client } = ctx.data().map_err(|e| {
1616
log::error!("Failed to get app data: {:?}", e);
1717
e
1818
})?;
1919

20-
let mut connection = app_data.pool.get().await?;
20+
let mut connection = pool.get().await?;
2121

2222
let list_users = users.load::<User>(&mut connection).await?;
2323

24-
match app_data.client.get_index("users").await {
24+
match client.get_index("users").await {
25+
//If getting the index is successful, we add documents to it
2526
Ok(index) => {
2627
index.add_documents(&list_users, Some("id")).await?;
2728
}
29+
30+
//If getting the index fails, we create it and then add documents to the new index
2831
Err(_) => {
29-
let task = app_data.client.create_index("users", Some("id")).await?;
30-
let task = task
31-
.wait_for_completion(&app_data.client, None, None)
32-
.await?;
33-
let index = task.try_make_index(&app_data.client).unwrap();
32+
let task = client.create_index("users", Some("id")).await?;
33+
let task = task.wait_for_completion(client, None, None).await?;
34+
let index = task.try_make_index(client).unwrap();
3435

3536
index.add_documents(&list_users, Some("id")).await?;
3637
}
3738
}
3839

39-
let index = app_data.client.get_index("users").await?;
40+
let index = client.get_index("users").await?;
4041

42+
//We build the query
4143
let query = SearchQuery::new(&index).with_query(&query_string).build();
4244

4345
let results: SearchResults<User> = index.execute_query(&query).await?;
4446

45-
log::error!("{:#?}", results.hits);
46-
47+
//Tranform the results into a type that implements OutputType
48+
//Required for return types to implement this trait
4749
let search_results: Vec<User> = results
4850
.hits
4951
.into_iter()

examples/web_app_graphql/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use meilisearch_sdk::Client as SearchClient;
1818

1919
pub type ApplicationSchema = Schema<Query, EmptyMutation, EmptySubscription>;
2020

21+
//Represents application data passed to graphql resolvers
2122
pub struct GraphQlData {
2223
pub pool: Pool<AsyncPgConnection>,
2324
pub client: SearchClient,
@@ -35,6 +36,7 @@ pub async fn index(schema: web::Data<ApplicationSchema>, req: GraphQLRequest) ->
3536
schema.execute(req_inner).await.into()
3637
}
3738

39+
//We build the graphql schema and any data required to be passed to all resolvers
3840
pub fn build_schema(app_env_vars: &AppEnvVars) -> Result<ApplicationSchema, ApplicationError> {
3941
let client = SearchClient::new(
4042
&app_env_vars.meilisearch_host,

examples/web_app_graphql/src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use actix_web::{guard, App, HttpServer};
55
use diesel::migration::MigrationSource;
66
use diesel::{Connection, PgConnection};
77
use diesel_migrations::FileBasedMigrations;
8-
use meilisearch_ex::{build_schema, index, index_graphiql, app_env_vars::AppEnvVars, errors::ApplicationError};
9-
8+
use meilisearch_ex::{
9+
app_env_vars::AppEnvVars, build_schema, errors::ApplicationError, index, index_graphiql,
10+
};
1011

1112
#[actix_web::main]
1213
async fn main() -> Result<(), ApplicationError> {
@@ -16,6 +17,7 @@ async fn main() -> Result<(), ApplicationError> {
1617

1718
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
1819

20+
//Run migrations on app start
1921
let mut db_connection = PgConnection::establish(&app_env_vars.database_url)?;
2022
let mut migrations = FileBasedMigrations::from_path(&app_env_vars.migrations_dir_path)?
2123
.migrations()
@@ -42,6 +44,7 @@ async fn main() -> Result<(), ApplicationError> {
4244
.max_age(3600)
4345
.supports_credentials(),
4446
)
47+
//Add schema to application `Data` extractor
4548
.app_data(web::Data::new(schema.clone()))
4649
.service(web::resource("/").guard(guard::Post()).to(index))
4750
.service(web::resource("/").guard(guard::Get()).to(index_graphiql))

examples/web_app_graphql/src/models.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
44

55
use crate::schema::users;
66

7+
//Struct that corresponds to our database structure for users table
78
#[derive(SimpleObject, Deserialize, Serialize, Queryable, Selectable, Debug)]
89
#[diesel(table_name = users)]
910
pub struct User {

0 commit comments

Comments
 (0)