context
如果您忘记DataLoaderDocumentListener
在执行查询之前添加参数(就像我做的那样),该参数将为空:
public GraphQLController(
ISchema schema,
IDocumentExecuter documentExecuter,
DataLoaderDocumentListener dataLoaderListener)
{
_schema = schema;
_documentExecuter = documentExecuter;
_dataLoaderListener = dataLoaderListener;
}
[HttpPost]
public async Task<IActionResult> Post([FromBody]BaseGraphQuery query)
{
var inputs = query.Variables.ToInputs();
var executionOptions = new ExecutionOptions
{
Schema = _schema,
Query = query.Query,
Inputs = inputs,
};
// Important!
executionOptions.Listeners.Add(_dataLoaderListener);
// (Execute the query)
}
并且DataLoaderDocumentListener
必须存在于服务容器中:
public void ConfigureServices(IServiceCollection services)
{
// (other service configuration)
services.AddSingleton<GraphQL.DataLoader.IDataLoaderContextAccessor, GraphQL.DataLoader.DataLoaderContextAccessor>();
services.AddSingleton<GraphQL.DataLoader.DataLoaderDocumentListener>();
services.AddSingleton<GraphQL.IDocumentExecuter, GraphQL.DocumentExecuter>();
services.AddSingleton<GraphQL.Types.ISchema, RootSchema>();
}
准确但模糊的错误消息让我感到困惑,直到我想起我已经“重构”了一些代码并Listeners.Add
错误地删除了该行。