Refactor param orders of SendAsync and SubscribeAsync to reduce confu… · NavidK0/SimpleGraphQL-For-Unity@648470d · GitHub
Skip to content
This repository was archived by the owner on May 10, 2023. It is now read-only.

Commit 648470d

Browse files
committed
Refactor param orders of SendAsync and SubscribeAsync to reduce confusion and unnecessary passing of auth tokens/schemes
AuthScheme is now null by default (no need to be Bearer by default) AuthToken and AuthScheme now appear after everything Fix AuthScheme being ignored in SendAsync
1 parent 9b44fdc commit 648470d

4 files changed

Lines changed: 45 additions & 49 deletions

File tree

‎README.md‎

Lines changed: 10 additions & 4 deletions

‎Runtime/SimpleGraphQL/GraphQLClient.cs‎

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ public class GraphQLClient
1515
{
1616
public readonly List<Query> SearchableQueries;
1717
public readonly Dictionary<string, string> CustomHeaders;
18-
1918
public string Endpoint;
2019
public string AuthScheme;
2120

2221
public GraphQLClient(
2322
string endpoint,
24-
string authScheme = "Bearer",
2523
IEnumerable<Query> queries = null,
26-
Dictionary<string, string> headers = null
24+
Dictionary<string, string> headers = null,
25+
string authScheme = null
2726
)
2827
{
2928
Endpoint = endpoint;
@@ -35,36 +34,26 @@ public GraphQLClient(
3534
public GraphQLClient(GraphQLConfig config)
3635
{
3736
Endpoint = config.Endpoint;
38-
AuthScheme = config.AuthScheme;
3937
SearchableQueries = config.Files.SelectMany(x => x.Queries).ToList();
4038
CustomHeaders = config.CustomHeaders.ToDictionary(header => header.Key, header => header.Value);
41-
}
42-
43-
public async Task<string> SendAsync(
44-
Query query,
45-
string authToken = null,
46-
Dictionary<string, object> variables = null,
47-
Dictionary<string, string> headers = null
48-
)
49-
{
50-
return await SendAsync(query, AuthScheme, authToken, variables, headers);
39+
AuthScheme = config.AuthScheme;
5140
}
5241

5342
/// <summary>
5443
/// Send a query!
5544
/// </summary>
5645
/// <param name="query">The query you are sending. These should be generated from your graphQL files.</param>
57-
/// <param name="authScheme">The authScheme to be used.</param>
58-
/// <param name="authToken">The authToken</param>
5946
/// <param name="variables">Any variables you want to pass</param>
6047
/// <param name="headers">Any headers you want to pass</param>
48+
/// <param name="authToken">The authToken</param>
49+
/// <param name="authScheme">The authScheme to be used.</param>
6150
/// <returns></returns>
6251
public async Task<string> SendAsync(
6352
Query query,
64-
string authScheme = "Bearer",
65-
string authToken = null,
6653
Dictionary<string, object> variables = null,
67-
Dictionary<string, string> headers = null
54+
Dictionary<string, string> headers = null,
55+
string authToken = null,
56+
string authScheme = null
6857
)
6958
{
7059
if (query.OperationType == OperationType.Subscription)
@@ -83,12 +72,18 @@ public async Task<string> SendAsync(
8372
}
8473
}
8574

75+
if (authScheme == null)
76+
{
77+
authScheme = AuthScheme;
78+
}
79+
8680
string postQueryAsync = await HttpUtils.PostQueryAsync(
8781
Endpoint,
8882
query,
89-
AuthScheme,
83+
variables,
84+
headers,
9085
authToken,
91-
variables, headers
86+
authScheme
9287
);
9388

9489
return postQueryAsync;
@@ -102,11 +97,11 @@ public void RegisterListener(Action<string> listener)
10297
{
10398
HttpUtils.SubscriptionDataReceived += listener;
10499
}
100+
105101
/// <summary>
106102
/// Unregisters a listener for subscriptions.
107103
/// </summary>
108104
/// <param name="listener"></param>
109-
110105
public void UnregisterListener(Action<string> listener)
111106
{
112107
HttpUtils.SubscriptionDataReceived -= listener;
@@ -116,26 +111,17 @@ public void UnregisterListener(Action<string> listener)
116111
/// Subscribe to a query in GraphQL.
117112
/// </summary>
118113
/// <param name="query"></param>
119-
/// <param name="authToken"></param>
120114
/// <param name="variables"></param>
121115
/// <param name="headers"></param>
116+
/// <param name="authToken"></param>
117+
/// <param name="authScheme"></param>
122118
/// <returns>True if successful</returns>
123119
public async Task<bool> SubscribeAsync(
124120
Query query,
125-
string authToken = null,
126121
Dictionary<string, object> variables = null,
127-
Dictionary<string, string> headers = null
128-
)
129-
{
130-
return await SubscribeAsync(query, AuthScheme, authToken, variables, headers);
131-
}
132-
133-
public async Task<bool> SubscribeAsync(
134-
Query query,
135-
string authScheme = "Bearer",
122+
Dictionary<string, string> headers = null,
136123
string authToken = null,
137-
Dictionary<string, object> variables = null,
138-
Dictionary<string, string> headers = null
124+
string authScheme = null
139125
)
140126
{
141127
if (query.OperationType != OperationType.Subscription)
@@ -154,10 +140,15 @@ public async Task<bool> SubscribeAsync(
154140
}
155141
}
156142

143+
if (authScheme == null)
144+
{
145+
authScheme = AuthScheme;
146+
}
147+
157148
if (!HttpUtils.IsWebSocketReady())
158149
{
159150
// Prepare the socket before continuing.
160-
await HttpUtils.WebSocketConnect(Endpoint, authScheme, authToken, "graphql-ws", headers);
151+
await HttpUtils.WebSocketConnect(Endpoint, "graphql-ws", headers, authToken, authScheme);
161152
}
162153

163154
return await HttpUtils.WebSocketSubscribe(query.ToString(), query, variables);
@@ -191,7 +182,7 @@ public Query FindQuery(string fileName)
191182
/// <summary>
192183
/// Finds the first query located in a file.
193184
/// </summary>
194-
/// <param name="fileName"></param>
185+
/// <param name="operationName"></param>
195186
/// <returns></returns>
196187
public Query FindQueryByOperation(string operationName)
197188
{
@@ -212,7 +203,6 @@ public Query FindQuery(string fileName, string operationName)
212203
/// <summary>
213204
/// Finds a query by operationName and operationType.
214205
/// </summary>
215-
/// <param name="fileName"></param>
216206
/// <param name="operationName"></param>
217207
/// <param name="operationType"></param>
218208
/// <returns></returns>

‎Runtime/SimpleGraphQL/HttpUtils.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public static void PreInit()
4242
public static async Task<string> PostQueryAsync(
4343
string url,
4444
Query query,
45-
string authScheme = "Bearer",
46-
string authToken = null,
4745
Dictionary<string, object> variables = null,
48-
Dictionary<string, string> headers = null
46+
Dictionary<string, string> headers = null,
47+
string authToken = null,
48+
string authScheme = null
4949
)
5050
{
5151
var uri = new Uri(url);
@@ -103,10 +103,10 @@ public static bool IsWebSocketReady() =>
103103
/// <returns></returns>
104104
public static async Task WebSocketConnect(
105105
string url,
106-
string authScheme = "Bearer",
107-
string authToken = null,
108106
string protocol = "graphql-ws",
109-
Dictionary<string, string> headers = null
107+
Dictionary<string, string> headers = null,
108+
string authToken = null,
109+
string authScheme = null
110110
)
111111
{
112112
url = url.Replace("http", "ws");

‎package.json‎

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)