private List<Comment> toComment(String description) { if (description == null) return Collections.emptyList(); Comment comment = new Comment(description, new SourceLocation(1, 1)); return Collections.singletonList(comment); }
private SourceLocation mkLocation(int line, int column) { return new SourceLocation(line, column); }
protected List<Comment> getCommentOnChannel(List<Token> refChannel) { List<Comment> comments = new ArrayList<>(); for (Token refTok : refChannel) { String text = refTok.getText(); // we strip the leading hash # character but we don't trim because we don't // know the "comment markup". Maybe its space sensitive, maybe its not. So // consumers can decide that if (text == null) { continue; } text = text.replaceFirst("^#", ""); comments.add(new Comment(text, new SourceLocation(refTok.getLine(), refTok.getCharPositionInLine()))); } return comments; }
protected SourceLocation getSourceLocation(Token token) { String sourceName = token.getTokenSource().getSourceName(); if (IntStream.UNKNOWN_SOURCE_NAME.equals(sourceName)) { // UNKNOWN_SOURCE_NAME is Antrl's way of indicating that no source name was given during parsing -- // which is the case when queries and other operations are parsed. We don't want this hardcoded // '<unknown>' sourceName to leak to clients when the response is serialized as JSON, so we null it. sourceName = null; } return new SourceLocation(token.getLine(), token.getCharPositionInLine() + 1, sourceName); }
.map(l -> baseLocation .map(base -> new SourceLocation( base.getLine() + l.getLine(), base.getColumn() + l.getColumn()))
InvalidSyntaxException mkMoreTokensException(Token token) { SourceLocation sourceLocation = new SourceLocation(token.getLine(), token.getCharPositionInLine()); String sourcePreview = mkPreview(token.getLine()); return new InvalidSyntaxException(sourceLocation, "There are more tokens in the query that have not been consumed", sourcePreview, token.getText(), null); }
private InvalidSyntaxException mkException(Parser recognizer, RecognitionException cause) { String sourcePreview = null; String offendingToken = null; SourceLocation sourceLocation = null; Token currentToken = recognizer.getCurrentToken(); if (currentToken != null) { int line = currentToken.getLine(); int column = currentToken.getCharPositionInLine(); offendingToken = currentToken.getText(); sourcePreview = mkPreview(line); sourceLocation = new SourceLocation(line, column, sourceName); } return new InvalidSyntaxException(sourceLocation, null, sourcePreview, offendingToken, cause); }
@Override public SourceLocation getSourceLocation() { return new SourceLocation(0, 0); }
private List<Comment> toComment(String description) { if (description == null) return Collections.emptyList(); Comment comment = new Comment(description, new SourceLocation(1, 1)); return Collections.singletonList(comment); }
public ValidatedQuery validateQuery(String query) { ValidatedQuery result = new ValidatedQuery(); result.errors = validationErrors; Document document; try { document = parser.parseDocument(query); result.parsedQuery = document; } catch (Exception e) { ValidationError err = new ValidationError(ValidationErrorType.InvalidSyntax, new SourceLocation(0, 0), "Invalid query syntax."); validationErrors.add(err); result.valid = false; return result; } validationErrors.addAll(validator.validateDocument(schema, document)); if (validationErrors.size() > 0) { result.valid = false; return result; } result.valid = true; return result; }
public ValidatedQuery validateQuery(String query) { ValidatedQuery result = new ValidatedQuery(); result.errors = validationErrors; Document document; try { document = parser.parseDocument(query); result.parsedQuery = document; } catch (Exception e) { ValidationError err = new ValidationError(ValidationErrorType.InvalidSyntax, new SourceLocation(0, 0), "Invalid query syntax."); validationErrors.add(err); result.valid = false; return result; } validationErrors.addAll(validator.validateDocument(schema, document)); if (validationErrors.size() > 0) { result.valid = false; return result; } result.valid = true; return result; }
protected List<Comment> getCommentOnChannel(List<Token> refChannel) { List<Comment> comments = new ArrayList<>(); for (Token refTok : refChannel) { String text = refTok.getText(); // we strip the leading hash # character but we don't trim because we don't // know the "comment markup". Maybe its space sensitive, maybe its not. So // consumers can decide that if (text == null) { continue; } text = text.replaceFirst("^#", ""); MultiSourceReader.SourceAndLine sourceAndLine = multiSourceReader.getSourceAndLineFromOverallLine(refTok.getLine()); int column = refTok.getCharPositionInLine(); // graphql spec says line numbers start at 1 int line = sourceAndLine.getLine() + 1; comments.add(new Comment(text, new SourceLocation(line, column, sourceAndLine.getSourceName()))); } return comments; }
.map(l -> baseLocation .map(base -> new SourceLocation( base.getLine() + l.getLine(), base.getColumn() + l.getColumn()))
protected SourceLocation getSourceLocation(Token token) { MultiSourceReader.SourceAndLine sourceAndLine = multiSourceReader.getSourceAndLineFromOverallLine(token.getLine()); int column = token.getCharPositionInLine() + 1; // graphql spec says line numbers start at 1 int line = sourceAndLine.getLine() + 1; return new SourceLocation(line, column, sourceAndLine.getSourceName()); }
InvalidSyntaxException mkMoreTokensException(Token token) { SourceAndLine sourceAndLine = multiSourceReader.getSourceAndLineFromOverallLine(token.getLine()); int column = token.getCharPositionInLine(); // graphql spec says line numbers start at 1 SourceLocation sourceLocation = new SourceLocation(sourceAndLine.getLine()+1, column, sourceAndLine.getSourceName()); String sourcePreview = mkPreview(token.getLine()); return new InvalidSyntaxException(sourceLocation, "There are more tokens in the query that have not been consumed", sourcePreview, token.getText(), null); }
private InvalidSyntaxException mkException(Parser recognizer, RecognitionException cause) { String sourcePreview = null; String offendingToken = null; SourceLocation sourceLocation = null; Token currentToken = recognizer.getCurrentToken(); if (currentToken != null) { int tokenLine = currentToken.getLine(); int column = currentToken.getCharPositionInLine(); SourceAndLine sourceAndLine = multiSourceReader.getSourceAndLineFromOverallLine(tokenLine); offendingToken = currentToken.getText(); sourcePreview = mkPreview(tokenLine); // graphql spec says line numbers start at 1 sourceLocation = new SourceLocation(sourceAndLine.getLine()+1, column, sourceAndLine.getSourceName()); } return new InvalidSyntaxException(sourceLocation, null, sourcePreview, offendingToken, cause); }
public <T extends ExecutionResult> T execute() { assertNotNull(arguments, "Arguments can't be null"); LOGGER.info("Executing request. Operation name: {}. Request: {} ", operationName, requestQuery); Parser parser = new Parser(); Document document; try { document = parser.parseDocument(requestQuery); } catch (ParseCancellationException e) { RecognitionException recognitionException = (RecognitionException) e.getCause(); SourceLocation sourceLocation = new SourceLocation(recognitionException.getOffendingToken().getLine(), recognitionException.getOffendingToken().getCharPositionInLine()); InvalidSyntaxError invalidSyntaxError = new InvalidSyntaxError(sourceLocation); return (T) new GraphQLRxExecutionResult(Observable.just(null), Observable.just(Arrays.asList(invalidSyntaxError))); } Validator validator = new Validator(); List<ValidationError> validationErrors = validator.validateDocument(graphQLSchemaHolder.getGraphQLSchema(), document); if (validationErrors.size() > 0) { return (T) new GraphQLRxExecutionResult(Observable.just(null), Observable.just(validationErrors)); } if (executionStrategy == null) { if (executorService == null) { executionStrategy = new GraphQLDefaultRxExecutionStrategy(graphQLSchemaHolder, maxQueryDepth, maxQueryComplexity); } else { executionStrategy = new GraphQLExecutorServiceRxExecutionStrategy(graphQLSchemaHolder, executorService, maxQueryDepth, maxQueryComplexity); } } RxExecution execution = new RxExecution(graphQLSchemaHolder, maxQueryDepth, maxQueryComplexity, executionStrategy); ExecutionResult executionResult = execution.execute(graphQLSchemaHolder.getGraphQLSchema(), context, document, operationName, arguments); return (T) (executionResult instanceof GraphQLRxExecutionResult ? executionResult : new GraphQLRxExecutionResult(Observable.just(executionResult.getData()), Observable.just(executionResult.getErrors()))); }