supabase_client_graphql/lib.rs
1//! Supabase GraphQL client for `pg_graphql`.
2//!
3//! This crate provides a GraphQL client for querying Supabase's auto-generated
4//! GraphQL API powered by the `pg_graphql` PostgreSQL extension. The endpoint
5//! is at `POST /graphql/v1`.
6//!
7//! # Usage
8//!
9//! ```ignore
10//! use supabase_client_sdk::prelude::*;
11//! use serde_json::json;
12//!
13//! let client = SupabaseClient::new(config)?;
14//! let graphql = client.graphql()?;
15//!
16//! // Collection query with builder
17//! let connection = graphql.collection("blogCollection")
18//! .select(&["id", "title", "createdAt"])
19//! .filter(GqlFilter::eq("status", json!("published")))
20//! .order_by("createdAt", OrderByDirection::DescNullsLast)
21//! .first(10)
22//! .execute::<BlogRow>().await?;
23//!
24//! // Insert mutation
25//! let result = graphql.insert_into("blogCollection")
26//! .objects(vec![json!({"title": "New Post"})])
27//! .returning(&["id", "title"])
28//! .execute::<BlogRow>().await?;
29//! ```
30
31pub mod client;
32pub mod error;
33pub mod filter;
34pub mod mutation;
35pub mod order;
36pub mod query;
37pub(crate) mod render;
38pub mod types;
39
40// Re-exports for convenient access
41pub use client::GraphqlClient;
42pub use error::{GraphqlApiError, GraphqlError};
43pub use filter::{FilterOp, GqlFilter, IsValue};
44pub use mutation::{MutationBuilder, MutationKind};
45pub use order::OrderByDirection;
46pub use query::QueryBuilder;
47pub use types::*;
48
49use supabase_client_core::SupabaseClient;
50
51/// Extension trait to create a [`GraphqlClient`] from a [`SupabaseClient`].
52///
53/// # Example
54/// ```ignore
55/// use supabase_client_sdk::prelude::*;
56/// use supabase_client_graphql::SupabaseClientGraphqlExt;
57///
58/// let client = SupabaseClient::new(config)?;
59/// let graphql = client.graphql()?;
60/// let response = graphql.collection("blogCollection")
61/// .select(&["id", "title"])
62/// .first(10)
63/// .execute::<BlogRow>().await?;
64/// ```
65pub trait SupabaseClientGraphqlExt {
66 /// Create a [`GraphqlClient`] from the client's configuration.
67 ///
68 /// Requires `supabase_url` and `supabase_key` to be set in the config.
69 fn graphql(&self) -> Result<GraphqlClient, GraphqlError>;
70}
71
72impl SupabaseClientGraphqlExt for SupabaseClient {
73 fn graphql(&self) -> Result<GraphqlClient, GraphqlError> {
74 GraphqlClient::new(self.supabase_url(), self.api_key())
75 }
76}