dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.85k stars 1.33k forks source link

Optimize final introspection json schema #4841

Open frederikhors opened 4 years ago

frederikhors commented 4 years ago

Is your feature request related to a problem? Please describe.

I'm using the below code (from here) because I need an introspection schema in my SPA web app:

import { getIntrospectionQuery } from "graphql";
import fetch from "node-fetch";
import * as fs from "fs";

fetch("http://localhost:3000/graphql", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    variables: {},
    query: getIntrospectionQuery({ descriptions: false }),
  }),
})
  .then((result) => result.json())
  .then(({ data }) => {
    fs.writeFile("./schema.json", JSON.stringify(data), (err) => {
      if (err) {
        console.error("Writing failed:", err);
        return;
      }
      console.log("Schema written!");
    });
  });

The amazing team at Urql also created packages for optimization: https://formidable.com/open-source/urql/docs/graphcache/schema-awareness/#optimizing-a-schema.

Using that solution my final schema .json is 35% smaller than the one generated by code-generator. And my final schema is part of the client bundle, so any BIT is important!

Describe the solution you'd like

Can we optimize like they did?

Describe alternatives you've considered

Maybe using options?

Additional context

I can use their solution, but it is very inconvenient especially in CI/CD environments where the GraphQL server is not active and I cannot verify the correct schema.

Generating from *.graphql files is amazing!


Thanks for your amazing work!

dotansimha commented 4 years ago

That's sounds like a very interesting feature! It could be added easily with configuration flag.

@frederikhors PRs are always welcome ;)