Search in sources :

Example 1 with ParserOptions

use of graphql.parser.ParserOptions in project graphql-java by graphql-java.

the class ParseAndValidate method parse.

/**
 * This can be called to parse (but not validate) a graphql query.
 *
 * @param executionInput the input containing the query
 *
 * @return a result object that indicates how this operation went
 */
public static ParseAndValidateResult parse(ExecutionInput executionInput) {
    try {
        // 
        // we allow the caller to specify new parser options by context
        ParserOptions parserOptions = executionInput.getGraphQLContext().get(ParserOptions.class);
        Parser parser = new Parser();
        Document document = parser.parseDocument(executionInput.getQuery(), parserOptions);
        return ParseAndValidateResult.newResult().document(document).variables(executionInput.getVariables()).build();
    } catch (InvalidSyntaxException e) {
        return ParseAndValidateResult.newResult().syntaxException(e).variables(executionInput.getVariables()).build();
    }
}
Also used : ParserOptions(graphql.parser.ParserOptions) InvalidSyntaxException(graphql.parser.InvalidSyntaxException) Document(graphql.language.Document) Parser(graphql.parser.Parser)

Example 2 with ParserOptions

use of graphql.parser.ParserOptions in project graphql-java by graphql-java.

the class SchemaParser method parseImpl.

private TypeDefinitionRegistry parseImpl(Reader schemaInput, ParserOptions parseOptions) {
    try {
        if (parseOptions == null) {
            // for SDL we don't stop how many parser tokens there are - it's not the attack vector
            // to be prevented compared to queries
            parseOptions = ParserOptions.getDefaultParserOptions().transform(opts -> opts.maxTokens(Integer.MAX_VALUE));
        }
        Parser parser = new Parser();
        Document document = parser.parseDocument(schemaInput, parseOptions);
        return buildRegistry(document);
    } catch (InvalidSyntaxException e) {
        throw handleParseException(e.toInvalidSyntaxError());
    }
}
Also used : InvalidSyntaxError(graphql.InvalidSyntaxError) ParserOptions(graphql.parser.ParserOptions) Charset.defaultCharset(java.nio.charset.Charset.defaultCharset) InvalidSyntaxException(graphql.parser.InvalidSyntaxException) Files(java.nio.file.Files) SchemaProblem(graphql.schema.idl.errors.SchemaProblem) IOException(java.io.IOException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) File(java.io.File) ArrayList(java.util.ArrayList) NonSDLDefinitionError(graphql.schema.idl.errors.NonSDLDefinitionError) Document(graphql.language.Document) SDLDefinition(graphql.language.SDLDefinition) List(java.util.List) Parser(graphql.parser.Parser) StringReader(java.io.StringReader) Definition(graphql.language.Definition) GraphQLError(graphql.GraphQLError) PublicApi(graphql.PublicApi) Collections(java.util.Collections) InputStream(java.io.InputStream) InvalidSyntaxException(graphql.parser.InvalidSyntaxException) Document(graphql.language.Document) Parser(graphql.parser.Parser)

Example 3 with ParserOptions

use of graphql.parser.ParserOptions in project graphql-maven-plugin-project by graphql-java-generator.

the class DocumentParser method parseDocuments.

/**
 * The main method of the class: it graphqlUtils.executes the generation of the given documents
 *
 * @param documents
 *            The GraphQL definition schema, from which the code is to be generated
 * @return
 * @throws IOException
 *             When an error occurs, during the parsing of the GraphQL schemas
 */
public int parseDocuments() throws IOException {
    logger.debug("Starting documents parsing");
    // Configuration of the GraphQL schema parser, from the project configuration
    ParserOptions newDefault = ParserOptions.newParserOptions().maxTokens(configuration.getMaxTokens()).build();
    ParserOptions.setDefaultParserOptions(newDefault);
    documents.getDocuments().stream().forEach(this::parseOneDocument);
    logger.debug("Documents have been parsed. Executing internal finalizations");
    // Let's finalize some "details":
    // Init the list of the object implementing each interface. This is done last, when all objects has been read by
    // the plugin.
    logger.debug("Init list of interface implementations");
    initListOfInterfaceImplementations();
    // The types Map allows to retrieve easily a Type from its name
    logger.debug("Fill type map");
    fillTypesMap();
    // Manage ObjectTypeExtensionDefinition: add the extension to the object they belong to
    manageObjectTypeExtensionDefinition();
    // Add the Relay connection capabilities, if configured for it
    if (configuration.isAddRelayConnections()) {
        addRelayConnections.addRelayConnections();
    }
    // We're done
    int nbClasses = objectTypes.size() + enumTypes.size() + interfaceTypes.size();
    logger.debug(documents.getDocuments().size() + " document(s) parsed (" + nbClasses + ")");
    return nbClasses;
}
Also used : ParserOptions(graphql.parser.ParserOptions)

Aggregations

ParserOptions (graphql.parser.ParserOptions)3 Document (graphql.language.Document)2 InvalidSyntaxException (graphql.parser.InvalidSyntaxException)2 Parser (graphql.parser.Parser)2 GraphQLError (graphql.GraphQLError)1 InvalidSyntaxError (graphql.InvalidSyntaxError)1 PublicApi (graphql.PublicApi)1 Definition (graphql.language.Definition)1 SDLDefinition (graphql.language.SDLDefinition)1 NonSDLDefinitionError (graphql.schema.idl.errors.NonSDLDefinitionError)1 SchemaProblem (graphql.schema.idl.errors.SchemaProblem)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Charset.defaultCharset (java.nio.charset.Charset.defaultCharset)1 Files (java.nio.file.Files)1 ArrayList (java.util.ArrayList)1