public void MissingRequiredArgNonNullBooleanArg() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" fragment missingRequiredArg on Arguments { nonNullBooleanArgField(nonNullBooleanArg: null) } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( $"The argument `nonNullBooleanArg` is required " + "and does not allow null values.", t.Message), t => Assert.Equal( "The specified fragment `missingRequiredArg` " + "is not used within the current document.", t.Message)); }
public void OperationWithTwoVariablesThatHaveTheSameName() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" query houseTrainedQuery( $atOtherHomes: Boolean, $atOtherHomes: Boolean) { dog { isHousetrained(atOtherHomes: $atOtherHomes) } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "A document containing operations that " + "define more than one variable with the same " + "name is invalid for execution.", t.Message), t => Assert.Equal( "The field `isHousetrained` does not exist " + "on the type `Dog`.", t.Message)); }
public void DuplicateArgument() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" fragment goodNonNullArg on Arguments { nonNullBooleanArgField( nonNullBooleanArg: true, nonNullBooleanArg: true) } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( $"Arguments are not unique.", t.Message), t => Assert.Equal( "The specified fragment `goodNonNullArg` " + "is not used within the current document.", t.Message)); }
public void SkipDirectiveIsInTheWrongPlace() { // arrange DocumentNode query = Parser.Default.Parse(@" query @skip(if: $foo) { field } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "The field `field` does not exist " + "on the type `Query`.", t.Message), t => Assert.Equal( "The specified directive is not valid the " + "current location.", t.Message)); }
public void TwoQueryOperationsWithTheSameName() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" query getName { dog { name } } query getName { dog { owner { name } } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( $"The operation name `getName` is not unique.", t.Message)); }
public void MaxDepthRuleIsIncluded() { // arrange DocumentNode query = Parser.Default.Parse(@" query { catOrDog { ... on Cat { name } } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator( new QueryExecutionOptions { MaxExecutionDepth = 1 }); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => { Assert.Equal( "The query exceded the maximum allowed execution " + "depth of 1.", t.Message); }); }
private QueryValidationResult Validate( ISchema schema, DocumentNode document) { // TODO: pass options into Validate(..., _options); return(_validator.Validate(schema, document)); }
private BadRequestReason?Validate(QueryRequest queryRequest) { var queryResourceValidation = QueryResourceValidator.Validate <TResource>(queryRequest); var queryPhraseValidation = QueryValidator.Validate <TResource>(queryRequest.Query); return(queryResourceValidation ?? queryPhraseValidation); }
public void DisallowedSecondRootField() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" subscription sub { newMessage { body sender } disallowedSecondRootField } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( $"Subscription operation `sub` must " + "have exactly one root field.", t.Message), t => Assert.Equal( "The field `disallowedSecondRootField` does not exist " + "on the type `Subscription`.", t.Message)); }
public void QueryWithTypeSystemDefinitions() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" query getDogName { dog { name color } } extend type Dog { color: String } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "A document containing TypeSystemDefinition " + "is invalid for execution.", t.Message), t => Assert.Equal( "The field `color` does not exist " + "on the type `Dog`.", t.Message)); }
public virtual R Handle(T query) { var response = default(R); try { _queryValidator.Validate(query); response = Task.Run(() => _handler.Handle(query)).Result; _bus.Response(response); } catch (QueryValidationException e) { _errorEventEmitter.EmitValidationFailed(query); } catch (QueryHandlerException e) { _errorEventEmitter.EmitHandlingFailed(query); } catch (Exception e) { _logger.Log("Exception while handling query[" + e.GetType() + "]: " + e.StackTrace, query); throw new QueryHandlerException(); } return(response); }
public void ScalarSelectionsNotAllowedOnInt() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" { dog { barkVolume { sinceWhen } } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal(t.Message, "`barkVolume` is a scalar field. Selections on scalars " + "or enums are never allowed, because they are the leaf " + "nodes of any GraphQL query.")); }
public void UnsupportedDirective() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" { dog { name @foo(bar: true) } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "The specified directive `foo` " + "is not supported by the current schema.", t.Message)); }
public void NameFieldIsAmbiguous() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" { findDog(complex: { name: ""A"", name: ""B"" }) { name } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "Field `name` is ambiguous.", t.Message)); }
public void UndefinedFragment() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" { dog { ...undefinedFragment } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "The specified fragment `undefinedFragment` " + "does not exist.", t.Message)); }
public void InvalidInputObjectFieldsExist() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" { findDog(complex: { favoriteCookieFlavor: ""Bacon"" }) { name } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "The specified input object field " + "`favoriteCookieFlavor` does not exist.", t.Message)); }
public void RequiredFieldIsNull() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" { findDog2(complex: { name: null }) { name } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "`name` is a required field and cannot be null.", t.Message)); }
public void FragmentDoesNotMatchType() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" { dog { ...fragmentDoesNotMatchType } } fragment fragmentDoesNotMatchType on Human { name } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "The parent type does not match the type condition on " + "the fragment `fragmentDoesNotMatchType`.", t.Message)); }
public void NotExistingTypeOnInlineFragment() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" { dog { ...inlineNotExistingType } } fragment inlineNotExistingType on Dog { ... on NotInSchema { name } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "The specified inline fragment " + "does not exist in the current schema.", t.Message)); }
public void InlineFragOnScalar() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" { dog { ... inlineFragOnScalar } } fragment inlineFragOnScalar on Dog { ... on Boolean { somethingElse } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal(t.Message, "Fragments can only be declared on unions, interfaces, " + "and objects.")); }
public void VariableNotUsedWithinFragment() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" query variableNotUsedWithinFragment($atOtherHomes: Boolean) { dog { ...isHousetrainedWithoutVariableFragment } } fragment isHousetrainedWithoutVariableFragment on Dog { barkVolume } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "The following variables were not used: " + "atOtherHomes.", t.Message)); }
public void DuplicateFragments() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" { dog { ...fragmentOne } } fragment fragmentOne on Dog { name } fragment fragmentOne on Dog { owner { name } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "There are multiple fragments with the name `fragmentOne`.", t.Message)); }
public void StringIntoInt() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" { arguments { ...stringIntoInt } } fragment stringIntoInt on Arguments { intArgField(intArg: ""123"") } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "The specified argument value does not match the " + "argument type.\nArgument: `intArg`\nValue: `123`", t.Message)); }
public void UnusedFragment() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" fragment nameFragment on Dog { # unused name } { dog { name } } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "The specified fragment `nameFragment` " + "is not used within the current document.", t.Message)); }
public void InvalidFieldArgName() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" fragment invalidArgName on Dog { doesKnowCommand(command: CLEAN_UP_HOUSE) } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "The argument `dogCommand` is required and does not " + "allow null values.", t.Message), t => Assert.Equal( "The argument `command` does not exist.", t.Message), t => Assert.Equal( "The specified fragment `invalidArgName` " + "is not used within the current document.", t.Message)); }
public void ConflictingBecauseAlias() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" fragment conflictingBecauseAlias on Dog { name: nickname name } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "The query has non-mergable fields.", t.Message), t => Assert.Equal( "The specified fragment `conflictingBecauseAlias` " + "is not used within the current document.", t.Message)); }
public void QueriesWithInvalidVariableTypes() { // arrange DocumentNode query = Utf8GraphQLParser.Parse(@" query takesCat($cat: Cat) { # ... } query takesDogBang($dog: Dog!) { # ... } query takesListOfPet($pets: [Pet]) { # ... } query takesCatOrDog($catOrDog: CatOrDog) { # ... } "); Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act QueryValidationResult result = queryValidator.Validate(schema, query); // assert Assert.True(result.HasErrors); Assert.Collection(result.Errors, t => Assert.Equal( "The following variables were not used: cat.", t.Message), t => Assert.Equal( "The following variables were not used: dog.", t.Message), t => Assert.Equal( "The following variables were not used: pets.", t.Message), t => Assert.Equal( "The following variables were not used: catOrDog.", t.Message), t => Assert.Equal( "The type of variable `cat` is not an input type.", t.Message), t => Assert.Equal( "The type of variable `dog` is not an input type.", t.Message), t => Assert.Equal( "The type of variable `pets` is not an input type.", t.Message), t => Assert.Equal( "The type of variable `catOrDog` is not an input type.", t.Message)); }
public void QueryIsNull() { // arrange Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act Action a = () => queryValidator.Validate(schema, null); // assert Assert.Throws <ArgumentNullException>(a); }
public void SchemaIsNull() { // arrange Schema schema = ValidationUtils.CreateSchema(); IQueryValidator queryValidator = CreateValidator(); // act // act Action a = () => queryValidator.Validate(null, new DocumentNode(null, new List <IDefinitionNode>())); // assert Assert.Throws <ArgumentNullException>(a); }
#pragma warning disable 1591 // Xml Comments public QueryResult Execute(IQuery query, PagingInfo paging) { ThrowIfNoQueryPropertyOnQuery(query); var result = QueryResult.For(query); try { var authorizationResult = _fetchingSecurityManager.Authorize(query); if (!authorizationResult.IsAuthorized) { result.SecurityMessages = authorizationResult.BuildFailedAuthorizationMessages(); result.Items = new object[0]; return(result); } result.Validation = _validator.Validate(query); if (!result.Validation.Success) { result.Items = new object[0]; return(result); } var property = GetQueryPropertyFromQuery(query); var actualQuery = property.GetValue(query, null); var provider = GetQueryProvider(query, property, actualQuery); var providerResult = ExecuteOnProvider(provider, actualQuery, paging); result.TotalItems = providerResult.TotalItems; var readModels = providerResult.Items as IEnumerable <IReadModel>; result.Items = readModels != null?_filters.Filter(readModels) : providerResult.Items; } catch (TargetInvocationException ex) { _exceptionPublisher.Publish(ex.InnerException); result.Exception = ex.InnerException; } catch (Exception ex) { _exceptionPublisher.Publish(ex); result.Exception = ex; } return(result); }
private async Task<object> GetResult(IHttpContext context, IDictionary<string, string> routeArgs, object service, MethodInfo method, ParameterInfo[] methodParams, IQueryValidator argResolver) { var urlArgs = new Dictionary<string, string>(); var paramSource = await GetParamSource(context.Request); foreach (var param in paramSource.Split(new[] { '?', '&' }, StringSplitOptions.RemoveEmptyEntries)) { var tokens = param.Split('='); if (tokens.Length != 2) return Error(context, "Invalid query string"); urlArgs.Add(tokens[0].ToLower(), Uri.UnescapeDataString(tokens[1])); } var methodArgs = new object[methodParams.Length]; if (argResolver != null) { var error = await argResolver.Validate(context, urlArgs); if (error != null) return error; } for (var i = 0; i < methodArgs.Length; i++) { var param = methodParams[i]; var argResult = TryGetArg(context, param, urlArgs, routeArgs, argResolver); if (argResult is RouteError) return argResult; methodArgs[i] = argResult; } var result = method.Invoke(service, methodArgs); var task = result as Task; if (task == null) return result; await task; PropertyInfo getter; if (m_taskResultLookup.TryGetValue(task.GetType(), out getter)) return getter.GetValue(task); else return null; }