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

Transport minified version of query. #277

Merged
merged 3 commits into from
Apr 11, 2024
Merged

Conversation

phryneas
Copy link
Member

@phryneas phryneas commented Apr 8, 2024

Right now, we transport the AST of a query over the network.

That's not optimal:

Normal Query:

  query dynamicProducts {
    products {
      id
      title
      foo {
        bar {
          baz {
            yoink
          }
        }
      }
    }
  }

is instead transported as

{
  "kind": "Document",
  "definitions": [
    {
      "kind": "OperationDefinition",
      "operation": "query",
      "name": {
        "kind": "Name",
        "value": "dynamicProducts"
      },
      "variableDefinitions": [],
      "directives": [],
      "selectionSet": {
        "kind": "SelectionSet",
        "selections": [
          {
            "kind": "Field",
            "name": {
              "kind": "Name",
              "value": "products"
            },
            "arguments": [],
            "directives": [],
            "selectionSet": {
              "kind": "SelectionSet",
              "selections": [
                {
                  "kind": "Field",
                  "name": {
                    "kind": "Name",
                    "value": "id"
                  },
                  "arguments": [],
                  "directives": []
                },
                {
                  "kind": "Field",
                  "name": {
                    "kind": "Name",
                    "value": "title"
                  },
                  "arguments": [],
                  "directives": []
                },
                {
                  "kind": "Field",
                  "name": {
                    "kind": "Name",
                    "value": "foo"
                  },
                  "arguments": [],
                  "directives": [],
                  "selectionSet": {
                    "kind": "SelectionSet",
                    "selections": [
                      {
                        "kind": "Field",
                        "name": {
                          "kind": "Name",
                          "value": "bar"
                        },
                        "arguments": [],
                        "directives": [],
                        "selectionSet": {
                          "kind": "SelectionSet",
                          "selections": [
                            {
                              "kind": "Field",
                              "name": {
                                "kind": "Name",
                                "value": "baz"
                              },
                              "arguments": [],
                              "directives": [],
                              "selectionSet": {
                                "kind": "SelectionSet",
                                "selections": [
                                  {
                                    "kind": "Field",
                                    "name": {
                                      "kind": "Name",
                                      "value": "yoink"
                                    },
                                    "arguments": [],
                                    "directives": []
                                  }
                                ]
                              }
                            }
                          ]
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        ]
      }
    }
  ],
  "loc": {
    "start": 0,
    "end": 163
  }
}

and instead we could go smaller and transport

query dynamicProducts{products{id title foo{bar{baz{yoink}}}}}

@phryneas phryneas force-pushed the pr/transport-minified-queries branch from e7a4152 to 7c88078 Compare April 8, 2024 13:07
@phryneas phryneas added this to the 0.11.0 milestone Apr 8, 2024
Copy link

relativeci bot commented Apr 8, 2024

Job #133: Bundle Size — 1.01MiB (~+0.01%).

e38de2b(current) vs 1f8cda5 main#115(baseline)

Warning

Bundle contains 1 duplicate package – View duplicate packages

Bundle metrics  Change 2 changes Regression 1 regression
                 Current
Job #133
     Baseline
Job #115
Regression  Initial JS 890.63KiB(~+0.01%) 890.58KiB
No change  Initial CSS 0B 0B
Change  Cache Invalidation 1.5% 0%
No change  Chunks 24 24
No change  Assets 45 45
No change  Modules 512 512
No change  Duplicate Modules 30 30
No change  Duplicate Code 1.29% 1.29%
No change  Packages 29 29
No change  Duplicate Packages 1 1
Bundle size by type  Change 1 change Regression 1 regression
                 Current
Job #133
     Baseline
Job #115
Regression  JS 1023.89KiB (~+0.01%) 1023.84KiB
Not changed  Other 5.99KiB 5.99KiB

View job #133 reportView pr/transport-minified-queries branch activityView project dashboard

@phryneas phryneas marked this pull request as ready for review April 9, 2024 13:04
@phryneas phryneas requested a review from a team as a code owner April 9, 2024 13:04
Comment on lines 6 to 13
print(query)
// replace multi-spaces with single space
.replace(/\s{2,}/g, " ")
// remove spaces that are preceeded by braces
.replace(/(?<=[{}])\s+/g, "")
// remove spaces that are preceeding braces
.replace(/\s+(?=[{}])/g, "")
.trim()
Copy link
Member

@jerelmiller jerelmiller Apr 9, 2024

Choose a reason for hiding this comment

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

It looks like graphql has a stripIgnoredCharacters utility that handles this. Can we use that function to handle stripping the whitespace? It looks like that utility is lexer-aware so it should be a bit more accurate.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, that pulls in about 10kb more bundle size - but since we're in SSR world here, that should be okay.
We might at some point get to the point that we pull in too much for edge functions, though (they have a limit of 1MB I believe?), so I'll make a note that that's a great place for a future size optimization.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, that pulls in about 10kb more bundle size - but since we're in SSR world here, that should be okay.
We might at some point get to the point that we pull in too much for edge functions, though (they have a limit of 1MB I believe?), so I'll make a note that that's a great place for a future size optimization.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah forget about it, most of that is included in gql already, so it's likely fine anyways.

Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

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

My only request would be to use stripIgnoredCharacters, but otherwise this looks great!

Copy link

vercel bot commented Apr 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
apollo-client-nextjs-experimental-nextjs-app-support ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 11, 2024 10:51am

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.

2 participants