@@ -11,19 +11,29 @@ public class GraphQLRequest : Dictionary<string, object>, IEquatable<GraphQLRequ
11
11
public const string QUERY_KEY = "query" ;
12
12
public const string VARIABLES_KEY = "variables" ;
13
13
public const string EXTENSIONS_KEY = "extensions" ;
14
+ public const string EXTENSIONS_PERSISTED_QUERY_KEY = "persistedQuery" ;
15
+ public const int APQ_SUPPORTED_VERSION = 1 ;
16
+
17
+ private string ? _sha265Hash ;
14
18
15
19
/// <summary>
16
- /// The Query
20
+ /// The query string
17
21
/// </summary>
18
22
[ StringSyntax ( "GraphQL" ) ]
19
23
public string ? Query
20
24
{
21
25
get => TryGetValue ( QUERY_KEY , out object value ) ? ( string ) value : null ;
22
- set => this [ QUERY_KEY ] = value ;
26
+ set
27
+ {
28
+ this [ QUERY_KEY ] = value ;
29
+ // if the query string gets overwritten, reset the hash value
30
+ if ( _sha265Hash is not null )
31
+ _sha265Hash = null ;
32
+ }
23
33
}
24
34
25
35
/// <summary>
26
- /// The name of the Operation
36
+ /// The operation to execute
27
37
/// </summary>
28
38
public string ? OperationName
29
39
{
@@ -59,16 +69,28 @@ public GraphQLRequest([StringSyntax("GraphQL")] string query, object? variables
59
69
Extensions = extensions ;
60
70
}
61
71
62
- #if NET6_0_OR_GREATER
63
72
public GraphQLRequest ( GraphQLQuery query , object ? variables = null , string ? operationName = null ,
64
73
Dictionary < string , object ? > ? extensions = null )
65
74
: this ( query . Text , variables , operationName , extensions )
66
75
{
76
+ _sha265Hash = query . Sha256Hash ;
67
77
}
68
- #endif
69
78
70
79
public GraphQLRequest ( GraphQLRequest other ) : base ( other ) { }
71
80
81
+ public void GeneratePersistedQueryExtension ( )
82
+ {
83
+ if ( Query is null )
84
+ throw new InvalidOperationException ( $ "{ nameof ( Query ) } is null") ;
85
+
86
+ Extensions ??= new ( ) ;
87
+ Extensions [ EXTENSIONS_PERSISTED_QUERY_KEY ] = new Dictionary < string , object >
88
+ {
89
+ [ "version" ] = APQ_SUPPORTED_VERSION ,
90
+ [ "sha256Hash" ] = _sha265Hash ?? Hash . Compute ( Query ) ,
91
+ } ;
92
+ }
93
+
72
94
/// <summary>
73
95
/// Returns a value that indicates whether this instance is equal to a specified object
74
96
/// </summary>
0 commit comments