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

building a apollo_encoder Document from apollo_parser Document #902

Closed
yanns opened this issue Aug 29, 2024 · 3 comments
Closed

building a apollo_encoder Document from apollo_parser Document #902

yanns opened this issue Aug 29, 2024 · 3 comments

Comments

@yanns
Copy link
Contributor

yanns commented Aug 29, 2024

We were using such code to build an apollo_encoder Document from an apollo_parser Document:

use apollo_parser::cst::Document;

fn derive_public_schema(doc: Document,...) {
    let mut pub_schema = apollo_encoder::Document::new();

    for definition in doc.definitions() {
        match definition {
            Definition::SchemaDefinition(e) => pub_schema.schema(e.try_into()?),
            Definition::OperationDefinition(e) => pub_schema.operation(e.try_into()?),
            Definition::FragmentDefinition(e) => pub_schema.fragment(e.try_into()?),
...

The update of apollo_parser to 0.8.x. breaks this code.
I could not find how to deal with that.
What would be the new way?

My goal is to transform the schema based on some custom directives and to compute its new SDL (as String)

@yanns yanns added bug Something isn't working triage labels Aug 29, 2024
@SimonSapin
Copy link
Contributor

Despite it being in beta (… still, I really hope we can take care of that soon) apollo-compiler 1.0 has absorbed the functionality of apollo-encoder which is no longer in the repository.

  1. Parse an ast::Document or a Schema, depending on which level of abstraction works best
  2. Modify it as desired. When traversing a Node<Example> or Component<Example> shared-ownership pointer you'll likely need to call .make_mut() (which clones the value if it was shared, which it typically isn't after parsing) in order to get a &mut Example.
  3. Serialize back to GraphQL syntax with the Display impl: either doc.to_string() or println!("{doc}") etc. To tweak the serialization config (currently, indentation), instead call .serialize() to get something with builder methods that also implement Display / .to_string().

@SimonSapin SimonSapin removed bug Something isn't working triage labels Aug 29, 2024
@yanns
Copy link
Contributor Author

yanns commented Aug 30, 2024

thanks a lot for the help!
I could rewrite the transformation with apollo-compiler.
The new API is much easier to use. The code is much better now.
Thanks a lot for this!

@yanns yanns closed this as completed Aug 30, 2024
@SimonSapin
Copy link
Contributor

Glad to hear it went to well!

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

No branches or pull requests

2 participants