Skip to content

Remove redundant call to validate#1393

Merged
firaskafri merged 4 commits into
graphql-python:mainfrom
shukryzablah:patch-1
May 5, 2023
Merged

Remove redundant call to validate#1393
firaskafri merged 4 commits into
graphql-python:mainfrom
shukryzablah:patch-1

Conversation

@shukryzablah

@shukryzablah shukryzablah commented Mar 14, 2023

Copy link
Copy Markdown
Contributor

The call to validate in the django view is redundant with the validation call in graphql-core. Related to #1198

The call to `validate` in the django view is redundant with the validation call in graphql-core.
@erikwrede

Copy link
Copy Markdown
Member

Thanks for opening the PR!

While this PR brings a big reduction in overhead, we still have some unnecessary overhead left due to the call of parse.
On discord, I suggested moving the execute logic into the function as well to remove that overhead. It would simply involve replacing the schema.execute call with a call of graphene's internal execute including the parsed AST.

For reference, this is how GQL-Core handles execution of a query string:

  #excerpt of graphql core's graphql_impl
    """Execute a query, return asynchronously only if necessary."""
    # Validate Schema
    schema_validation_errors = validate_schema(schema)
    if schema_validation_errors:
        return ExecutionResult(data=None, errors=schema_validation_errors)

    # Parse
    try:
        document = parse(source)
    except GraphQLError as error:
        return ExecutionResult(data=None, errors=[error])

    # Validate
    from .validation import validate

    validation_errors = validate(schema, document)
    if validation_errors:
        return ExecutionResult(data=None, errors=validation_errors)

    # Execute, just move this call into graphene-django instead of graphene
    return execute(
        schema, # replace with graphene_schema.graphql_schema
        document,
        root_value,
        context_value,
        variable_values,
        operation_name,
        field_resolver,
        type_resolver,
        None,
        middleware,
        execution_context_class,
        is_awaitable,
    )

Since 2/3 of this logic is already duplicated in the present graphene-django implementation, it might be worthwhile to move the rest there as well. Personally, I'm fine with both and believe this could also be merged as is while we evaluate my suggested addition. - cc @firaskafri @tcleonard

@firaskafri

Copy link
Copy Markdown
Collaborator

Thanks for opening the PR!

While this PR brings a big reduction in overhead, we still have some unnecessary overhead left due to the call of parse. On discord, I suggested moving the execute logic into the function as well to remove that overhead. It would simply involve replacing the schema.execute call with a call of graphene's internal execute including the parsed AST.

For reference, this is how GQL-Core handles execution of a query string:

  #excerpt of graphql core's graphql_impl
    """Execute a query, return asynchronously only if necessary."""
    # Validate Schema
    schema_validation_errors = validate_schema(schema)
    if schema_validation_errors:
        return ExecutionResult(data=None, errors=schema_validation_errors)

    # Parse
    try:
        document = parse(source)
    except GraphQLError as error:
        return ExecutionResult(data=None, errors=[error])

    # Validate
    from .validation import validate

    validation_errors = validate(schema, document)
    if validation_errors:
        return ExecutionResult(data=None, errors=validation_errors)

    # Execute, just move this call into graphene-django instead of graphene
    return execute(
        schema, # replace with graphene_schema.graphql_schema
        document,
        root_value,
        context_value,
        variable_values,
        operation_name,
        field_resolver,
        type_resolver,
        None,
        middleware,
        execution_context_class,
        is_awaitable,
    )

Since 2/3 of this logic is already duplicated in the present graphene-django implementation, it might be worthwhile to move the rest there as well. Personally, I'm fine with both and believe this could also be merged as is while we evaluate my suggested addition. - cc @firaskafri @tcleonard

Thank you for pin pointing this! Will look into it with @mohsam97

@firaskafri

Copy link
Copy Markdown
Collaborator

@shukryzablah Hello! Can you please the failing test? its just trim trailing whitespace.................................................Failed

@firaskafri firaskafri requested a review from mahdyhamad April 10, 2023 06:33
@firaskafri firaskafri requested a review from jaw9c April 21, 2023 18:32
@firaskafri firaskafri requested review from kiendang and sjdemartini May 4, 2023 19:07
@firaskafri firaskafri merged commit 09f9b6d into graphql-python:main May 5, 2023
superlevure pushed a commit to loft-orbital/graphene-django that referenced this pull request Jul 19, 2023
* Remove redundant call to validate

The call to `validate` in the django view is redundant with the validation call in graphql-core.

* Remove whitespace

---------

Co-authored-by: Firas K <3097061+firaskafri@users.noreply.github.com>
@kiendang kiendang mentioned this pull request Aug 3, 2023
This was referenced Oct 25, 2023
@firaskafri

Copy link
Copy Markdown
Collaborator

Thanks for opening the PR!
While this PR brings a big reduction in overhead, we still have some unnecessary overhead left due to the call of parse. On discord, I suggested moving the execute logic into the function as well to remove that overhead. It would simply involve replacing the schema.execute call with a call of graphene's internal execute including the parsed AST.
For reference, this is how GQL-Core handles execution of a query string:

  #excerpt of graphql core's graphql_impl
    """Execute a query, return asynchronously only if necessary."""
    # Validate Schema
    schema_validation_errors = validate_schema(schema)
    if schema_validation_errors:
        return ExecutionResult(data=None, errors=schema_validation_errors)

    # Parse
    try:
        document = parse(source)
    except GraphQLError as error:
        return ExecutionResult(data=None, errors=[error])

    # Validate
    from .validation import validate

    validation_errors = validate(schema, document)
    if validation_errors:
        return ExecutionResult(data=None, errors=validation_errors)

    # Execute, just move this call into graphene-django instead of graphene
    return execute(
        schema, # replace with graphene_schema.graphql_schema
        document,
        root_value,
        context_value,
        variable_values,
        operation_name,
        field_resolver,
        type_resolver,
        None,
        middleware,
        execution_context_class,
        is_awaitable,
    )

Since 2/3 of this logic is already duplicated in the present graphene-django implementation, it might be worthwhile to move the rest there as well. Personally, I'm fine with both and believe this could also be merged as is while we evaluate my suggested addition. - cc @firaskafri @tcleonard

Thank you for pin pointing this! Will look into it with @mohsam97

#1439

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants