Skip to content

Commit

Permalink
Non-Generic RegisterDataLoader (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
drowhunter authored and michaelstaib committed Nov 30, 2018
1 parent 3013699 commit 91e3924
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/Types/Configuration/IDataLoaderConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ namespace HotChocolate.Configuration
public interface IDataLoaderConfiguration
: IFluent
{
void RegisterDataLoader(Type type,
string key,
ExecutionScope scope,
Func<IServiceProvider, object> loaderFactory = null,
Func<object, CancellationToken, Task> triggerLoaderAsync = null);

void RegisterDataLoader<T>(
string key,
ExecutionScope scope,
Expand Down
33 changes: 24 additions & 9 deletions src/Types/Configuration/SchemaConfiguration.DataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ internal partial class SchemaConfiguration
internal IReadOnlyCollection<DataLoaderDescriptor>
DataLoaderDescriptors => _dataLoaders.Values;

public void RegisterDataLoader<T>(
public void RegisterDataLoader(Type type,
string key,
ExecutionScope scope,
Func<IServiceProvider, T> loaderFactory = null,
Func<T, CancellationToken, Task> triggerLoaderAsync = null)
{
Func<IServiceProvider, object> loaderFactory = null,
Func<object, CancellationToken, Task> triggerLoaderAsync = null){

if (key == null)
{
throw new ArgumentNullException(nameof(key));
Expand All @@ -29,22 +29,37 @@ public void RegisterDataLoader<T>(
Func<IServiceProvider, object> factory = null;
if (loaderFactory != null)
{
factory = new Func<IServiceProvider, object>(
sp => loaderFactory(sp));
factory = loaderFactory;
}

TriggerDataLoaderAsync trigger = null;
if (triggerLoaderAsync != null)
{
trigger = new TriggerDataLoaderAsync(
(o, c) => triggerLoaderAsync((T)o, c));
trigger = (o, c) => triggerLoaderAsync(o, c);
}

var descriptor = new DataLoaderDescriptor(
key, typeof(T), scope,
key, type, scope,
factory,
trigger);
_dataLoaders[key] = descriptor;
}
public void RegisterDataLoader<T>(
string key,
ExecutionScope scope,
Func<IServiceProvider, T> loaderFactory = null,
Func<T, CancellationToken, Task> triggerLoaderAsync = null)
{

Func<IServiceProvider,object> f = null;
if(loaderFactory != null)
f = s => loaderFactory(s);
Func<object,CancellationToken,Task> g = null;
if(triggerLoaderAsync != null)
g = (a,b) => triggerLoaderAsync((T)a,b);

RegisterDataLoader(typeof(T),key,scope,f,g);

}
}
}

0 comments on commit 91e3924

Please sign in to comment.