Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the executor and validation APIs public to enable split parsing and execution #874

Merged
merged 7 commits into from
Apr 3, 2021

Conversation

jerel
Copy link
Contributor

@jerel jerel commented Feb 13, 2021

Making these APIs public is based on my understanding of #773 (comment) and #773 (comment)

This PR is related to #776, #726, and enables features similar to #843 to be implemented in user land code. In my project I'm currently doing this (pseudo code) in the first step:

fn compile() {
  let document = parse_document_source(document_source, &root_node.schema)?;
  visit_all_rules(&mut ctx, &document);
  let operation = get_operation(&document, operation_name)?;
  Ok((document.clone(), operation.clone()))
}

so I can make use of the operation and document and then in the second step I'm using the [now public] validation::validate_input_values and executor::execute_validated_query_async or resolve_validated_subscription as needed.

Thoughts on these changes?

@jerel
Copy link
Contributor Author

jerel commented Mar 4, 2021

@LegNeato @ccbrown what are your thoughts on this proposed change?

Being able to precompile queries while checking them against the schema is a big requirement of a project I'm working on and ideally we could implement it in a way that stays aligned with this project.

@ccbrown
Copy link
Contributor

ccbrown commented Mar 4, 2021

To sum up my thoughts from the linked issues:

  • This is really valuable functionality that we should have in some form.
  • Ideally parsing wouldn't require knowledge of the schema. Unfortunately eliminating the parser's dependency on the schema isn't trivial. @mirosval made a valiant effort in refactor: remove ScalarValue type parameter #782, but it fell through. Given the value of this functionality and the lack of a clear path towards eliminating the parser's schema dependency, I'm okay moving forward with exposing whatever's necessary for this functionality anyway.
  • I have a preference for exposing this functionality in the inner crates (juniper::parser, juniper::validation, etc.) rather than the top level juniper crate, just for the sake of keeping the top level API narrow.

Copy link
Member

@LegNeato LegNeato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry this took so long. Can you add an integration test that uses the API so we can be sure to not regress?

@jerel jerel requested a review from LegNeato April 2, 2021 22:10
Copy link
Member

@LegNeato LegNeato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🍻

@LegNeato LegNeato merged commit c78045c into graphql-rust:master Apr 3, 2021
@LegNeato
Copy link
Member

LegNeato commented Apr 4, 2021

This is now on crates.io. Thank you again!

@mrtnzlml
Copy link

Hey @jerel! 👋 I have a question regarding this PR. Say you want to implement persistent queries, so in the first step, you parse and validate the query and save it to DB (so it can be accessed later while skipping validations). My question is: how should the validated query be stored in DB? Do you store it as a string (meaning that you must call parse_document_source later again)? Or is there a better way to skip the parsing on future requests? Thanks! :)

@jerel
Copy link
Contributor Author

jerel commented Jul 19, 2022

@mrtnzlml good question... I think I would experiment with storing the string versus a lightweight binary approach like bincode. I forget the nuances of this query API but it's possible that something like this would work and would save the effort of parsing the string tokens a second time. Example (untested):

// store
let document = parse_document_source(document_source, &root_node.schema)?;
visit_all_rules(&mut ctx, &document);
let operation = get_operation(&document, operation_name)?;
let store_me = bincode::serialize(&(document, operation)).unwrap();

// later
let (document, operation): (Document, Operation) = bincode::deserialize(&store_me).unwrap();
validate_input_values();
execute_validated_query_async();

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