Skip to content

reuse fragments by their selection set #437

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

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

knaeckeKami
Copy link
Collaborator

@knaeckeKami knaeckeKami commented Jan 7, 2024

This is just an idea for an alternative implementation to #417

  • Revert "[gql_code_builder] add dataClassConfig option to reuse class definitions for field selections that include only a single inline fragment spread"
  • simpler version of reuse fragments with less scope

This is a different approach to reusing fragments and reduce duplication in the code.

The idea is: Keep a Map of (TypeName ,BuiltSet<SelectionNode>) -> Reference for already generated fragments.

This will use the actual selections for figuring out whether a new class needs to be created or a fragment can be reused.

TODO: try if reusing generated subclasses for inline fragment spreads can also be optimized, without potential conflicts in types

TODO: potentially this can be extended to not only reuse fragments but all generated classes, if they have the same selections.

Drawbacks: the names of the generated classes are less stable and could change by adding new fragments which can be used

Advantage: IMO simpler to understand, easier to extend

@knaeckeKami knaeckeKami changed the title reuse classed by selection set reuse fragments by their selection set Jan 7, 2024
@LiLatee
Copy link
Contributor

LiLatee commented Jan 8, 2024

Hey again @knaeckeKami :D
I've tested it and for now, I am getting Out of memory error. I guess its because scope is much smaller than the previous solution and that's why.

And btw I had to manually modify fery_generator bcs the current version 0.8.2. expects dataClassConfig

@dav3rin
Copy link

dav3rin commented Mar 26, 2025

Hi @LiLatee and @knaeckeKami

I am currently have fatal issue in my codebase:

../../../flutter/third_party/dart/runtime/vm/zone.cc: 96: error: Out of memory.

My schema is notably quite large and union-heavy. And I believe I have now added the union that the JIT compiler can handle.

Line counts that work:

schema.ast.gql.dart -21296
schema.gql.g.dart - 23224
serializers.gql.dart - 18643
serializers.gql.g.dart - 25492

Line counts that result in failure:

schema.ast.gql.dart - 21296
schema.gql.g.dart - 23224
serializers.gql.dart - 21206
serializers.gql.g.dart - 29498

I'm struggling right now to find a good solution. Do you have any ideas on what I can do in the scenario?

Thanks

@LiLatee
Copy link
Contributor

LiLatee commented Mar 26, 2025

Hi @dav3rin
You can give a shot for my forks. Unfortunately, I still was not able to finish PRs that I opened to merge them into the package. Maybe it will work for you, but I remember that I still had a small issue, that inside serializers.gql.g.dart we were missing some records, so we've added a custom python script to add them on every build. I started working on a solution in the package, but I've never finished that.

dependency_overrides:
  ## We use our custom version of `gql_build` and `gql_code_builder` with somehow fixed `tristate_optionals` feature.
  gql_build:
    git:
      ref: 7edbf44ec1c929a1ed1a9e8e79ca8949ba1ed843
      url: https://github.com/LiLatee/flutter_gql.git
      path: codegen/gql_build
  gql_code_builder:
    git:
      ref: 7edbf44ec1c929a1ed1a9e8e79ca8949ba1ed843
      url: https://github.com/LiLatee/flutter_gql.git
      path: codegen/gql_code_builder
  gql_tristate_value:
    git:
      ref: 7edbf44ec1c929a1ed1a9e8e79ca8949ba1ed843
      url: https://github.com/LiLatee/flutter_gql.git
      path: codegen/gql_tristate_value
      
dev_dependencies:
  ## We use our custom version of ferry_generator with fixed `tristate_optional` feature.
  ferry_generator:
    git:
      ref: b3c08b68cccc0f18c59b7fc816bdacb45a7303de
      url: https://github.com/lilatee/ferry.git
      path: packages/ferry_generator

@dav3rin
Copy link

dav3rin commented Mar 26, 2025

Hey @LiLatee

Thanks for the quick reply.

I gave your fork a test.

As you predicted, some serializers are missing. The missing serializers are actually fine in my case. I just had to change my references to the fragment instead of the query subclass.

I do have a problem however with missing data classes.

Error (Xcode): packages/simulon_gql_api/lib/gql/composables/graphql/fragments/__generated__/ComposablePeekFull.data.gql.dart:221:16: Error: Type '_i12.GStandardComposableSceneType_resolution' not found.

The simplified gql schema looks like this:

fragment ComposablePeekFull on ComposablePeek {

  composable_type {
    ... on SceneComposableType {
        ...SceneComposableType
    }
    ... on AssetComposableType {
        ...AssetComposableType
    }
  }
}


fragment SceneComposableType on SceneComposableType{
  scene_type {
    ... on StandardComposableSceneType {
        ...StandardComposableSceneType
    }
    ... on BlenderComposableSceneType {
        ...BlenderComposableSceneType
    }
  }
}

fragment StandardComposableSceneType on StandardComposableSceneType{
  resolution {
      ...Resolution
  }
}

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.

3 participants