private void SetUpGraphQL(IServiceCollection services) { // setup taken from https://github.com/GraphQL-dotnet/examples/blob/master/src/AspNetCore/Example/Startup.cs#L15 services.AddTransient(sc => { var validatorTypeCache = new ValidatorTypeCache(true); validatorTypeCache.AddValidatorsFromAssembly(typeof(Startup).Assembly); return(validatorTypeCache); }); services.AddSingleton <IDocumentExecuter, DocumentExecuter>(); services.AddSingleton <IDocumentWriter, DocumentWriter>(); services.AddSingleton <PropertiesQuery>(); services.AddSingleton <PropertiesMutation>(); services.AddSingleton <CreateOrUpdatePropertyNoteInputType>(); services.AddSingleton <PriceChangeType>(); services.AddSingleton <PropertyType>(); services.AddSingleton <ISchema, Schema>(); services.AddSingleton <CreateOrUpdatePropertyNoteInputValidator>(); }
static ValidatorCacheBuilder() { Instance = new(useDependencyInjection : false); Instance.AddValidatorsFromAssembly(typeof(Startup).Assembly); InstanceDI = new(useDependencyInjection : true); InstanceDI.AddValidatorsFromAssembly(typeof(Startup).Assembly); }
void ExecuteQuery(Assembly assemblyContainingValidators) { #region StartConfig var validatorTypeCache = new ValidatorTypeCache(); validatorTypeCache.AddValidatorsFromAssembly(assemblyContainingValidators); var schema = new Schema(); var executer = new DocumentExecuter(); #endregion }
public GraphQLController( ValidatorTypeCache cache, ISchema schema, IDocumentExecuter documentExecuter, ILogger <GraphQLController> logger) { _cache = cache ?? throw new ArgumentNullException(nameof(cache)); _schema = schema ?? throw new ArgumentNullException(nameof(schema)); _documentExecuter = documentExecuter ?? throw new ArgumentNullException(nameof(documentExecuter)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); }
public static void SetCache(this ExecutionOptions options, ValidatorTypeCache cache) { if (options.UserContext == null) { options.UserContext = new Dictionary <string, object> { { key, cache } }; return; } AddValidatorCache(options.UserContext, cache); }
public async Task <IActionResult> Post([FromBody] GraphQlQuery query) { var validatorTypeCache = new ValidatorTypeCache(); validatorTypeCache. AddValidatorsFromAssemblyContaining(typeof(ItemValidator)); var options = new ExecutionOptions { Schema = schema, Query = query.Query, Inputs = query.Variables.ToInputs() }; options.UseFluentValidation(validatorTypeCache); var result = await new DocumentExecuter().ExecuteAsync(options); if (result.Errors?.Count > 0) { return(BadRequest(result.Errors)); } return(Ok(result)); }
public static async Task <string> ExecuteQuery(string queryString, Inputs?inputs, ValidatorTypeCache cache) { Thread.CurrentThread.CurrentUICulture = new("en-US"); queryString = queryString.Replace("'", "\""); using var schema = new Schema(); DocumentExecuter documentExecuter = new(); ExecutionOptions executionOptions = new() { Schema = schema, Query = queryString, Inputs = inputs }; executionOptions.UseFluentValidation(cache); var result = await documentExecuter.ExecuteAsync(executionOptions); return(await new DocumentWriter(indent: true).WriteToStringAsync(result)); } }
/// <summary> /// Adds a FieldMiddleware to the GraphQL pipeline that converts a <see cref="ValidationException"/> to <see cref="ExecutionError"/>s./> /// </summary> public static ExecutionOptions UseFluentValidation(this ExecutionOptions executionOptions, ValidatorTypeCache validatorTypeCache) { Guard.AgainstNull(executionOptions, nameof(executionOptions)); Guard.AgainstNull(validatorTypeCache, nameof(validatorTypeCache)); validatorTypeCache.Freeze(); executionOptions.SetCache(validatorTypeCache); var validationMiddleware = new ValidationMiddleware(); executionOptions.FieldMiddleware.Use(validationMiddleware); return(executionOptions); }
/// <summary> /// Injects a <see cref="ValidatorTypeCache" /> instance into a user context for testing purposes. /// </summary> public static void AddCacheToContext <T>(T userContext, ValidatorTypeCache cache) where T : Dictionary <string, object> { userContext.AddValidatorCache(cache); }
static ValidatorCacheBuilder() { Instance = new ValidatorTypeCache(); Instance.AddValidatorsFromAssembly(typeof(Startup).Assembly); }
internal static void AddValidatorCache(this IDictionary <string, object> dictionary, ValidatorTypeCache cache) { dictionary.Add(key, cache); }
/// <summary> /// Adds a FieldMiddleware to the GraphQL pipeline that converts a <see cref="ValidationException"/> to <see cref="ExecutionError"/>s./> /// </summary> public static void UseFluentValidation(this ExecutionOptions executionOptions, ValidatorTypeCache validatorTypeCache) { Guard.AgainstNull(executionOptions, nameof(executionOptions)); Guard.AgainstNull(validatorTypeCache, nameof(validatorTypeCache)); validatorTypeCache.Freeze(); executionOptions.SetCache(validatorTypeCache); var validationMiddleware = new ValidationMiddleware(); executionOptions.FieldMiddleware.Use(next => { return(context => validationMiddleware.Resolve(context, next)); }); }
static IntegrationTests() { typeCache = new ValidatorTypeCache(); typeCache.AddValidatorsFromAssemblyContaining <IntegrationTests>(); }