forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from EasyOC/main
Main
- Loading branch information
Showing
8 changed files
with
242 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...rchardCore.Modules/OrchardCore.Apis.GraphQL/Json/GraphQLNamedQueryRequestJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using GraphQL; | ||
|
||
namespace OrchardCore.Apis.GraphQL.Json; | ||
|
||
public class GraphQLNamedQueryRequestJsonConverter : JsonConverter<GraphQLNamedQueryRequest> | ||
{ | ||
public static readonly GraphQLNamedQueryRequestJsonConverter Instance = new(); | ||
|
||
/// <summary> | ||
/// Name for the operation name parameter. | ||
/// See https://github.com/graphql/graphql-over-http/blob/master/spec/GraphQLOverHTTP.md#request-parameters | ||
/// </summary> | ||
private const string _operationNameKey = "operationName"; | ||
|
||
/// <summary> | ||
/// Name for the query parameter. | ||
/// See https://github.com/graphql/graphql-over-http/blob/master/spec/GraphQLOverHTTP.md#request-parameters | ||
/// </summary> | ||
private const string _queryKey = "query"; | ||
|
||
/// <summary> | ||
/// Name for the variables parameter. | ||
/// See https://github.com/graphql/graphql-over-http/blob/master/spec/GraphQLOverHTTP.md#request-parameters | ||
/// </summary> | ||
private const string _variablesKey = "variables"; | ||
|
||
/// <summary> | ||
/// Name for the extensions parameter. | ||
/// See https://github.com/graphql/graphql-over-http/blob/master/spec/GraphQLOverHTTP.md#request-parameters | ||
/// </summary> | ||
private const string _extensionsKey = "extensions"; | ||
|
||
private const string _namedQueryKey = "namedQuery"; | ||
|
||
|
||
public override void Write(Utf8JsonWriter writer, GraphQLNamedQueryRequest value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
if (value.Query != null) | ||
{ | ||
writer.WritePropertyName(_queryKey); | ||
writer.WriteStringValue(value.Query); | ||
} | ||
if (value.OperationName != null) | ||
{ | ||
writer.WritePropertyName(_operationNameKey); | ||
writer.WriteStringValue(value.OperationName); | ||
} | ||
if (value.Variables != null) | ||
{ | ||
writer.WritePropertyName(_variablesKey); | ||
JsonSerializer.Serialize(writer, value.Variables, options); | ||
} | ||
if (value.Extensions != null) | ||
{ | ||
writer.WritePropertyName(_extensionsKey); | ||
JsonSerializer.Serialize(writer, value.Extensions, options); | ||
} | ||
if (value.NamedQuery != null) | ||
{ | ||
writer.WritePropertyName(_namedQueryKey); | ||
JsonSerializer.Serialize(writer, value.NamedQuery, options); | ||
} | ||
writer.WriteEndObject(); | ||
} | ||
|
||
public override GraphQLNamedQueryRequest Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartObject) | ||
throw new JsonException(); | ||
|
||
var request = new GraphQLNamedQueryRequest(); | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndObject) | ||
{ | ||
return request; | ||
} | ||
|
||
if (reader.TokenType != JsonTokenType.PropertyName) | ||
{ | ||
throw new JsonException(); | ||
} | ||
|
||
string key = reader.GetString()!; | ||
|
||
//unexpected end of data | ||
if (!reader.Read()) | ||
throw new JsonException(); | ||
|
||
switch (key) | ||
{ | ||
case _queryKey: | ||
request.Query = reader.GetString()!; | ||
break; | ||
case _operationNameKey: | ||
request.OperationName = reader.GetString()!; | ||
break; | ||
case _namedQueryKey: | ||
request.NamedQuery = reader.GetString(); | ||
break; | ||
case _variablesKey: | ||
request.Variables = JsonSerializer.Deserialize<Inputs>(ref reader, options); | ||
break; | ||
case _extensionsKey: | ||
request.Extensions = JsonSerializer.Deserialize<Inputs>(ref reader, options); | ||
break; | ||
default: | ||
reader.Skip(); | ||
break; | ||
} | ||
} | ||
|
||
//unexpected end of data | ||
throw new JsonException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...ardCore.Modules/OrchardCore.ContentFields/GraphQL/Types/UserPickerFieldQueryObjectType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using GraphQL; | ||
using GraphQL.DataLoader; | ||
using GraphQL.Types; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Apis.GraphQL; | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentManagement; | ||
using OrchardCore.Users.GraphQL; | ||
using OrchardCore.Users.Indexes; | ||
using OrchardCore.Users.Models; | ||
using YesSql; | ||
using YesSql.Services; | ||
|
||
namespace OrchardCore.ContentFields.GraphQL | ||
{ | ||
public class UserPickerFieldQueryObjectType : ObjectGraphType<UserPickerField> | ||
{ | ||
public UserPickerFieldQueryObjectType(UserType userType) | ||
{ | ||
Name = nameof(UserPickerField); | ||
|
||
Field<ListGraphType<StringGraphType>, IEnumerable<string>>("userIds") | ||
.Description("user ids") | ||
.PagingArguments() | ||
.Resolve(x => | ||
{ | ||
return x.Page(x.Source.UserIds); | ||
}); | ||
|
||
Field<ListGraphType<UserType>, IEnumerable<User>>("users") | ||
.Type(new ListGraphType(userType)) | ||
.Description("the user items") | ||
.PagingArguments() | ||
.ResolveAsync(x => | ||
{ | ||
var userLoader = GetOrAddUserProfileByIdDataLoader(x); | ||
return userLoader.LoadAsync(x.Page(x.Source.UserIds)).Then(itemResultSet => | ||
{ | ||
return itemResultSet.SelectMany(x => x); | ||
}); | ||
}); | ||
Field<UserType, User>("user") | ||
.Type(userType) | ||
.Description("the first user") | ||
.ResolveAsync(x => | ||
{ | ||
var userLoader = GetOrAddUserProfileByIdDataLoader(x); | ||
return userLoader.LoadAsync(x.Source.UserIds.FirstOrDefault()).Then(itemResultSet => | ||
{ | ||
return itemResultSet.FirstOrDefault(); | ||
}); | ||
}); | ||
} | ||
|
||
public static IDataLoader<string, IEnumerable<User>> GetOrAddUserProfileByIdDataLoader<T>(IResolveFieldContext<T> context) | ||
{ | ||
IDataLoaderContextAccessor requiredService = context.RequestServices.GetRequiredService<IDataLoaderContextAccessor>(); | ||
var session = context.RequestServices.GetService<ISession>(); | ||
return requiredService.Context.GetOrAddCollectionBatchLoader("GetOrAddUserByIds", async (IEnumerable<string> userIds) => | ||
{ | ||
if (userIds == null || !userIds.Any()) | ||
{ | ||
return null; | ||
} | ||
var users = await session.Query<User, UserIndex>(y => y.UserId.IsIn(userIds)).ListAsync(); | ||
return users.ToLookup((User k) => k.UserId, (User user) => user); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters