Skip to content

Commit

Permalink
Improved MediaField Graphql support (#15003)
Browse files Browse the repository at this point in the history
Co-authored-by: Hisham Bin Ateya <hishamco_2007@yahoo.com>
Co-authored-by: hyzx86 <yanzhong.han@jizhousoft.com>
Co-authored-by: Mike Alhayek <mike@crestapps.com>
Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
Co-authored-by: lampersky <lampersky@gmail.com>
  • Loading branch information
6 people authored Aug 8, 2024
1 parent af518de commit 1f00245
Showing 1 changed file with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ public MediaFieldQueryObjectType()
{
return Array.Empty<string>();
}

return x.Page(x.Source.Paths);
});

Field<ListGraphType<StringGraphType>, IEnumerable<string>>("fileNames")
.Description("the media fileNames")
.Description("the media file names")
.PagingArguments()
.Resolve(x =>
{
Expand All @@ -50,9 +51,40 @@ public MediaFieldQueryObjectType()
}
var paths = x.Page(x.Source.Paths);
var mediaFileStore = x.RequestServices.GetService<IMediaFileStore>();

return paths.Select(p => mediaFileStore.MapPathToPublicUrl(p));
});

Field<ListGraphType<MediaFileItemType>, IEnumerable<MediaFileItem>>("files")
.Description("the files of the media items")
.PagingArguments()
.Resolve(x =>
{
if (x.Source?.Paths is null)
{
return [];
}

var paths = x.Page(x.Source.Paths).ToArray();
var mediaFileStore = x.RequestServices.GetService<IMediaFileStore>();
var urls = paths.Select(p => mediaFileStore.MapPathToPublicUrl(p)).ToArray();
var fileNames = x.Page(x.Source?.GetAttachedFileNames()).ToArray();
var items = new List<MediaFileItem>();
var mediaTexts = x.Source?.MediaTexts ?? [];
for (int i = 0; i < paths.Length; i++)
{
items.Add(new MediaFileItem
{
Path = paths[i],
FileName = fileNames.Length > i ? fileNames[i] : string.Empty,
Url = urls.Length > i ? urls[i] : string.Empty,
MediaText = mediaTexts.Length > i ? mediaTexts[i] : string.Empty,
});
}

return items;
});

Field<ListGraphType<StringGraphType>, IEnumerable<string>>("mediatexts")
.Description("the media texts")
.PagingArguments()
Expand All @@ -66,4 +98,23 @@ public MediaFieldQueryObjectType()
});
}
}

public sealed class MediaFileItemType : ObjectGraphType<MediaFileItem>
{
public MediaFileItemType()
{
Field<StringGraphType>("fileName").Description("the file name of the media file item").Resolve(x => x.Source.FileName);
Field<StringGraphType>("path").Description("the path of the media file item").Resolve(x => x.Source.Path);
Field<StringGraphType>("url").Description("the url name of the media file item").Resolve(x => x.Source.Url);
Field<StringGraphType>("mediaText").Description("the media text of the file item").Resolve(x => x.Source.MediaText);
}
}

public sealed class MediaFileItem
{
public string FileName { get; set; }
public string Path { get; set; }
public string Url { get; set; }
public string MediaText { get; set; }
}
}

0 comments on commit 1f00245

Please sign in to comment.