1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! Dependency injection support for GraphQL resolvers
//!
//! This module provides extensions to integrate Reinhardt's DI system with GraphQL resolvers.
//!
//! # Overview
//!
//! The DI system for GraphQL works by storing an `InjectionContext` in the schema's data,
//! which can then be extracted from the GraphQL `Context` and used to resolve dependencies.
//!
//! # Example
//!
//! ```rust,no_run
//! # use async_graphql::{Context, Object, Result, ID, Schema, EmptyMutation, EmptySubscription, SimpleObject};
//! # use reinhardt_graphql::{GraphQLContextExt, SchemaBuilderExt, graphql_handler};
//! # use reinhardt_di::{InjectionContext, Injectable, DiResult, SingletonScope};
//! # use async_trait::async_trait;
//! # use std::sync::Arc;
//! #
//! # #[derive(Clone, SimpleObject)]
//! # struct User {
//! # id: ID,
//! # name: String,
//! # }
//! #
//! # #[derive(Clone)]
//! # struct DatabaseConnection;
//! #
//! # #[async_trait]
//! # impl Injectable for DatabaseConnection {
//! # async fn inject(_ctx: &InjectionContext) -> DiResult<Self> {
//! # Ok(DatabaseConnection)
//! # }
//! # }
//! #
//! # impl DatabaseConnection {
//! # async fn fetch_user(&self, id: &ID) -> Result<User> {
//! # Ok(User { id: id.clone(), name: "Test User".to_string() })
//! # }
//! # }
//! pub struct Query;
//!
//! #[Object]
//! impl Query {
//! async fn user(&self, ctx: &Context<'_>, id: ID) -> Result<User> {
//! user_impl(ctx, id).await
//! }
//! }
//!
//! #[graphql_handler]
//! async fn user_impl(
//! ctx: &Context<'_>,
//! id: ID,
//! #[inject] db: DatabaseConnection,
//! ) -> Result<User> {
//! let user = db.fetch_user(&id).await?;
//! Ok(user)
//! }
//!
//! let singleton = Arc::new(SingletonScope::new());
//! let injection_ctx = Arc::new(InjectionContext::builder(singleton).build());
//! let schema = Schema::build(Query, EmptyMutation, EmptySubscription)
//! .with_di_context(injection_ctx)
//! .finish();
//! ```
use Context;
use InjectionContext;
use Arc;
/// Extension trait for `async_graphql::Context` to support DI context extraction
///
/// This trait adds methods to `async_graphql::Context` for working with Reinhardt's
/// dependency injection system.
/// Extension trait for `async_graphql::SchemaBuilder` to easily add DI context
///
/// This trait provides a convenience method to add the `InjectionContext` to the schema data.
///
/// # Example
///
/// ```rust,no_run
/// # use async_graphql::{Schema, EmptyMutation, EmptySubscription, Object};
/// # use reinhardt_graphql::SchemaBuilderExt;
/// # use reinhardt_di::InjectionContext;
/// # use std::sync::Arc;
/// # struct Query;
/// # #[Object]
/// # impl Query {
/// # async fn hello(&self) -> &str { "world" }
/// # }
/// # use reinhardt_di::SingletonScope;
/// let singleton = Arc::new(SingletonScope::new());
/// let injection_ctx = Arc::new(InjectionContext::builder(singleton).build());
/// let schema = Schema::build(Query, EmptyMutation, EmptySubscription)
/// .with_di_context(injection_ctx)
/// .finish();
/// ```