using System; using System.Linq; using BililiveRecorder.Core; using BililiveRecorder.Core.Config.V3; using BililiveRecorder.Web.Models; using BililiveRecorder.Web.Models.Graphql; using GraphQL; using GraphQL.Types; namespace BililiveRecorder.Web.Graphql { internal class RecorderQuery : ObjectGraphType { private readonly IRecorder recorder; public RecorderQuery(IRecorder recorder) { this.recorder = recorder ?? throw new ArgumentNullException(nameof(recorder)); this.SetupFields(); } private void SetupFields() { this.Field("version", resolve: context => RecorderVersion.Instance); this.Field("config", resolve: context => this.recorder.Config.Global); this.Field("defaultConfig", resolve: context => DefaultConfig.Instance); this.Field>("rooms", resolve: context => this.recorder.Rooms); this.Field("room", arguments: new QueryArguments( new QueryArgument { Name = "objectId" }, new QueryArgument { Name = "roomId" } ), resolve: context => { var objectId = context.GetArgument("objectId"); var roomid = context.GetArgument("roomId"); var room = objectId != default ? this.recorder.Rooms.FirstOrDefault(x => x.ObjectId == objectId) : this.recorder.Rooms.FirstOrDefault(x => x.RoomConfig.RoomId == roomid || x.ShortId == roomid); return room; } ); } } }