Skip to content

Commit 1774889

Browse files
Merge #538
538: Added an example using actix-web, async_graphql and diesel r=irevoire a=korir248 # Pull Request ## Related issue Fixes #247 ## What does this PR do? - Adds an example which depivts using `actix-web` and `async_graphql` to query records in a `postgres` database ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Eugene Korir <[email protected]> Co-authored-by: Eugene Korir <[email protected]>
2 parents f7d4b3c + 2afbd38 commit 1774889

File tree

23 files changed

+540
-0
lines changed

23 files changed

+540
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DATABASE_URL=postgres://[username]:[password]@localhost/[database_name]
2+
MEILISEARCH_HOST=http://localhost:7700
3+
MEILISEARCH_API_KEY=[your-master-key]
4+
MIGRATIONS_DIR_PATH=migrations
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target
2+
/Cargo.lock
3+
4+
.env
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "meilisearch-ex"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Eugene Korir <[email protected]>"]
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
actix-cors = "0.6.5"
11+
actix-web = "4.4.0"
12+
async-graphql = "6.0.11"
13+
async-graphql-actix-web = "6.0.11"
14+
diesel = { version = "2.1.4", features = ["postgres"] }
15+
diesel-async = { version = "0.4.1", features = ["postgres", "deadpool"] }
16+
diesel_migrations = "2.1.0"
17+
dotenvy = "0.15.7"
18+
env_logger = "0.10.1"
19+
envy = "0.4.2"
20+
futures = "0.3.29"
21+
log = "0.4.20"
22+
meilisearch-sdk = "0.24.3"
23+
serde = { version = "1.0.192", features = ["derive"] }
24+
serde_json = "1.0.108"
25+
thiserror = "1.0.51"
26+
validator = { version = "0.16.1", features = ["derive"] }

examples/web_app_graphql/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Meilisearch example with graphql using `diesel`, `async_graphql` and `postgres`
2+
3+
## Contents
4+
5+
Setting up a graphql server using `async_graphql` and `actix-web`
6+
7+
Using `diesel` to query the database
8+
9+
Using `meilisearch-sdk` to search for records that match a given criteria
10+
11+
## Running the example
12+
13+
The meilisearch server needs to be running. You can run it by the command below
14+
15+
```bash
16+
meilisearch --master-key <your master key>
17+
```
18+
19+
Then you can run the application by simply running
20+
21+
```bash
22+
cargo run --release
23+
```
24+
25+
The above command will display a link to your running instance and you can simply proceed by clicking the link or navigating to your browser.
26+
27+
### Running the resolvers
28+
29+
On your browser, you will see a graphql playground in which you can use to run some queries
30+
31+
You can use the `searchUsers` query as follows:
32+
33+
```gpl
34+
query {
35+
users{
36+
search(queryString: "Eugene"){
37+
lastName
38+
firstName
39+
email
40+
}
41+
}
42+
}
43+
```
44+
45+
### Errors
46+
47+
Incase you run into the following error:
48+
49+
```bash
50+
= note: ld: library not found for -lpq
51+
clang: error: linker command failed with exit code 1 (use -v to see invocation)
52+
```
53+
54+
Run:
55+
56+
```bash
57+
sudo apt install libpq-dev
58+
```
59+
60+
This should fix the error
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# For documentation on how to configure this file,
2+
# see https://diesel.rs/guides/configuring-diesel-cli
3+
4+
[print_schema]
5+
file = "src/schema.rs"
6+
custom_type_derives = ["diesel::query_builder::QueryId"]
7+
8+
[migrations_directory]
9+
dir = "migrations"

examples/web_app_graphql/migrations/.keep

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- This file was automatically created by Diesel to setup helper functions
2+
-- and other internal bookkeeping. This file is safe to edit, any future
3+
-- changes will be added to existing projects as new migrations.
4+
5+
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
6+
DROP FUNCTION IF EXISTS diesel_set_updated_at();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- This file was automatically created by Diesel to setup helper functions
2+
-- and other internal bookkeeping. This file is safe to edit, any future
3+
-- changes will be added to existing projects as new migrations.
4+
5+
6+
7+
8+
-- Sets up a trigger for the given table to automatically set a column called
9+
-- `updated_at` whenever the row is modified (unless `updated_at` was included
10+
-- in the modified columns)
11+
--
12+
-- # Example
13+
--
14+
-- ```sql
15+
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
16+
--
17+
-- SELECT diesel_manage_updated_at('users');
18+
-- ```
19+
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
20+
BEGIN
21+
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
22+
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
23+
END;
24+
$$ LANGUAGE plpgsql;
25+
26+
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
27+
BEGIN
28+
IF (
29+
NEW IS DISTINCT FROM OLD AND
30+
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
31+
) THEN
32+
NEW.updated_at := current_timestamp;
33+
END IF;
34+
RETURN NEW;
35+
END;
36+
$$ LANGUAGE plpgsql;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- This file should undo anything in `up.sql`
2+
drop table if exists users;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Your SQL goes here
2+
create table if not exists users(
3+
id serial primary key,
4+
first_name varchar not null,
5+
last_name varchar not null,
6+
email varchar not null unique
7+
);

0 commit comments

Comments
 (0)