@@ -11,12 +11,14 @@ internal sealed class ReadPersistedOperationMiddleware
11
11
private readonly RequestDelegate _next ;
12
12
private readonly IExecutionDiagnosticEvents _diagnosticEvents ;
13
13
private readonly IOperationDocumentStorage _operationDocumentStorage ;
14
+ private readonly IDocumentHashProvider _documentHashAlgorithm ;
14
15
private readonly PersistedOperationOptions _options ;
15
16
16
17
private ReadPersistedOperationMiddleware (
17
18
RequestDelegate next ,
18
19
[ SchemaService ] IExecutionDiagnosticEvents diagnosticEvents ,
19
20
[ SchemaService ] IOperationDocumentStorage operationDocumentStorage ,
21
+ IDocumentHashProvider documentHashAlgorithm ,
20
22
PersistedOperationOptions options )
21
23
{
22
24
_next = next ??
@@ -25,6 +27,8 @@ private ReadPersistedOperationMiddleware(
25
27
throw new ArgumentNullException ( nameof ( diagnosticEvents ) ) ;
26
28
_operationDocumentStorage = operationDocumentStorage ??
27
29
throw new ArgumentNullException ( nameof ( operationDocumentStorage ) ) ;
30
+ _documentHashAlgorithm = documentHashAlgorithm ??
31
+ throw new ArgumentNullException ( nameof ( documentHashAlgorithm ) ) ;
28
32
_options = options ;
29
33
}
30
34
@@ -53,45 +57,58 @@ await _operationDocumentStorage.TryReadAsync(
53
57
documentId . Value , context . RequestAborted )
54
58
. ConfigureAwait ( false ) ;
55
59
56
- if ( operationDocument is OperationDocument parsedDoc )
60
+ if ( operationDocument is not null )
57
61
{
58
62
context . DocumentId = documentId ;
59
- context . Document = parsedDoc . Document ;
63
+ context . Document = GetOrParseDocument ( operationDocument ) ;
64
+ context . DocumentHash = GetDocumentHash ( operationDocument ) ;
60
65
context . ValidationResult = DocumentValidatorResult . Ok ;
61
66
context . IsCachedDocument = true ;
62
67
context . IsPersistedDocument = true ;
63
- if ( _options . SkipPersistedDocumentValidation )
64
- {
65
- context . ValidationResult = DocumentValidatorResult . Ok ;
66
- }
67
- _diagnosticEvents . RetrievedDocumentFromStorage ( context ) ;
68
- }
69
68
70
- if ( operationDocument is OperationDocumentSourceText sourceTextDoc )
71
- {
72
- context . DocumentId = documentId ;
73
- context . Document = Utf8GraphQLParser . Parse ( sourceTextDoc . AsSpan ( ) ) ;
74
- context . ValidationResult = DocumentValidatorResult . Ok ;
75
- context . IsCachedDocument = true ;
76
- context . IsPersistedDocument = true ;
77
69
if ( _options . SkipPersistedDocumentValidation )
78
70
{
79
71
context . ValidationResult = DocumentValidatorResult . Ok ;
80
72
}
73
+
81
74
_diagnosticEvents . RetrievedDocumentFromStorage ( context ) ;
82
75
}
83
76
}
84
77
}
85
78
79
+ private static DocumentNode GetOrParseDocument ( IOperationDocument document )
80
+ {
81
+ if ( document is IOperationDocumentNodeProvider nodeProvider )
82
+ {
83
+ return nodeProvider . Document ;
84
+ }
85
+
86
+ return Utf8GraphQLParser . Parse ( document . AsSpan ( ) ) ;
87
+ }
88
+
89
+ private string ? GetDocumentHash ( IOperationDocument document )
90
+ {
91
+ if ( document is IOperationDocumentHashProvider hashProvider
92
+ && _documentHashAlgorithm . Name . Equals ( hashProvider . Hash . AlgorithmName )
93
+ && _documentHashAlgorithm . Format . Equals ( hashProvider . Hash . Format ) )
94
+ {
95
+ return hashProvider . Hash . Hash ;
96
+ }
97
+
98
+ return null ;
99
+ }
100
+
86
101
public static RequestCoreMiddleware Create ( )
87
102
=> ( core , next ) =>
88
103
{
89
104
var diagnosticEvents = core . SchemaServices . GetRequiredService < IExecutionDiagnosticEvents > ( ) ;
90
105
var persistedOperationStore = core . SchemaServices . GetRequiredService < IOperationDocumentStorage > ( ) ;
106
+ var documentHashAlgorithm = core . Services . GetRequiredService < IDocumentHashProvider > ( ) ;
91
107
var middleware = new ReadPersistedOperationMiddleware (
92
108
next ,
93
109
diagnosticEvents ,
94
110
persistedOperationStore ,
111
+ documentHashAlgorithm ,
95
112
core . Options . PersistedOperations ) ;
96
113
return context => middleware . InvokeAsync ( context ) ;
97
114
} ;
0 commit comments