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

Add parsing + validation support for FieldSet scalars #681

Closed
SimonSapin opened this issue Oct 9, 2023 · 4 comments
Closed

Add parsing + validation support for FieldSet scalars #681

SimonSapin opened this issue Oct 9, 2023 · 4 comments
Labels
apollo-compiler issues/PRs pertaining to semantic analysis & validation apollo-parser

Comments

@SimonSapin
Copy link
Contributor

Apollo Federation has multiple directives that take an argument of type FieldSet to designate one or more fields of an object (or interface?) type. FieldSet is a custom scalar serialized as a string. Its value is expected to be parsed as a SelectionSet with optional outer brackets. (This is GraphQL syntax inside of a string in GraphQL syntax.)

Currently apollo-rs can only parse GraphQL documents, but we’d rather Rust federation not need string concatenation hacks to create a parser input that looks like a document.

API sketch

In apollo-parser:

  • Give apollo_parser::SyntaxTree a Cst type parameter
  • apollo_parser::Parser::new(string).parse() now returns SyntaxTree<apollo_parser::cst::Document> instead of just SyntaxTree
  • Add apollo_parser::Parser::new(string).parse_fieldset() which returns SyntaxTree<cst::SelectionSet>
  • The SyntaxTree::document(self) -> cst::Document method now only exists on SyntaxTree<cst::Document>
  • Add SyntaxTree::selection_set(self) -> cst::SelectionSet for SyntaxTree<cst::SelectionSet>

In apollo-compiler:

  • Add a top-level free function:
    fn parse_fieldset(
        schema: &Schema,
        type_name: impl Into<ast::NamedType>,
        source_text: impl Into<String>,
        path: impl AsRef<Path>,
    ) -> executable::SelectionSet
  • Add a Parser method with the same signature
@SimonSapin SimonSapin added apollo-parser apollo-compiler issues/PRs pertaining to semantic analysis & validation labels Oct 9, 2023
@SimonSapin
Copy link
Contributor Author

Validation is also needed:

could parse query { __typename } fragment F on SomeObjectType { <insert FieldSet string here> } […]

in the case above with FieldSet, we’d like to skip the operation validation https://spec.graphql.org/draft/#sec-Fragments-Must-Be-Used

I propose that:

  • We add a pub struct FieldSet(pub SelectionSet); wrapper type in the executable module,
  • parse_fieldset returns it instead of SelectionSet
  • It has a validate method with the same signature as ExecutableDocument::validate
    • For compatibility with Salsa-based validation, this method may create a temporary ast::Document that looks like above
    • Eventually when they are migrated to not require Salsa, only call validation functions relevant to selection sets and skip creating a temporary document entirely

@SimonSapin SimonSapin changed the title Add parsing support for FieldSet scalars Add parsing + validation support for FieldSet scalars Oct 9, 2023
@lrlna
Copy link
Member

lrlna commented Oct 9, 2023

I was thinking to go about slightly differently, which may simplify this design. I was hoping that Parser::new(string).selection_set() would return a SyntaxNode which could then be added to an existing ExecutableDocument that's currently getting modified.

This would allow us to not have a special validation method for just the selection set - the FieldSet would be validated along with the executable document it will inserted into.

@lrlna
Copy link
Member

lrlna commented Oct 9, 2023

From some internal conversations, it seems like FieldSet validation is explicitly required without it being part of an executable document, which means we'll add a specific path for this validation in the compiler.

@SimonSapin
Copy link
Contributor Author

Fixed in #685

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apollo-compiler issues/PRs pertaining to semantic analysis & validation apollo-parser
Projects
None yet
Development

No branches or pull requests

2 participants