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

Is it possible to support multiple subgraph schema files for one subgraph service? #2046

Open
wangmir opened this issue Aug 13, 2024 · 8 comments
Labels
feature 🎉 new commands, flags, functionality, and improved error messages triage issues and PRs that need to be triaged

Comments

@wangmir
Copy link

wangmir commented Aug 13, 2024

Description

Usually, from my team, we are splitting the schema based on the concerns, even from the same service. We are using gqlgen, which is golang version of graphql framework, and it supports multiple schema to be one service like this.

schema:
  - graph/user.graphql
  - graph/user_status.graphql
  - graph/user_permission.graphql

So, even in case of same user subgraph, we can use multiple schemas for readability. But, in case of the supergraph configuration of the rover, we can only use one single file per service like this.

federation_version: =2.3.2
subgraphs:
  films:
    routing_url: https://films.example.com
    schema:
      file: ./films.graphql
  people:
    routing_url: https://people.example.com
    schema:
      file: ./people.graphql

Can we support multiple graphql files to do the compose?

@wangmir wangmir added feature 🎉 new commands, flags, functionality, and improved error messages triage issues and PRs that need to be triaged labels Aug 13, 2024
@julioocamargoo
Copy link

Same happening here. some graphql files are going over 2k rows and becoming really hard to mantain. This feature would help a lot

@Cellule
Copy link

Cellule commented Oct 1, 2024

We're doing the same thing on our project, but we're using a custom GraphQL Codegen plugin to print the merged graph into 1 file.
Which is very simply

// copySchema.js
const { printSchema } = require("graphql");

module.exports = {
  plugin: function (schema, document, config) {
    return printSchema(schema);
  },
};

// codegen.yml for GraphQL Codegen
schema:
  - "../backend/schema.graphql"
  - "../backend/schemas/**/*.graphql"
generates:
  # Create single schema file to upload/check with apollo engine
  ../backend/schema.gen.graphql:
    plugins:
      - copySchema.js

However, we are realizing that the "printSchema" method from "graphql" doesn't actually print everything from the AST (namely directives)
So we are looking to move away from this approach and ideally have rover consume the list of .graphql files.
Since Apollo Server supports a list of "typeDefs" I was surprised to see rover doesn't

@Cellule
Copy link

Cellule commented Oct 1, 2024

update, I guess it had been a while since I checked, but I found a better AST printer for GraphQL Codegen that prints everything I need into one schema
https://the-guild.dev/graphql/codegen/plugins/other/schema-ast

So with this solution, you can keep all your schema files split however you like, and use GraphQL Codegen to generate 1 big schema.graphql file and run rover on that file

@wangmir
Copy link
Author

wangmir commented Oct 2, 2024

@Cellule Thanks for the guidence. I will check on schema ast.

@LongLiveCHIEF
Copy link
Contributor

The plugin for codegen I'd recommend above all others is https://the-guild.dev/graphql/codegen/docs/guides/graphql-server-apollo-yoga-with-server-preset

This let's you use graphql modules, and generates typescript types for you. The ast plugin is very low-level and doesn't provide much for you on top of merging.

The preset plugin helps set things up to actually run a subgraph compatible schema.

@Cellule
Copy link

Cellule commented Oct 11, 2024

@LongLiveCHIEF Interesting, I didn't know about the preset, it seem very opinionated (not necessarily a bad thing)
I'll look more into this option thanks!

@wangmir
Copy link
Author

wangmir commented Nov 4, 2024

@Cellule Since I'm using golang, so can I use this codegen too independently as a cli to only generate merged graphql too? Because the examples are all coupled with node.js graphql server generation.

@Cellule
Copy link

Cellule commented Nov 4, 2024

@Cellule Since I'm using golang, so can I use this codegen too independently as a cli to only generate merged graphql too? Because the examples are all coupled with node.js graphql server generation.

Yes, the tool does not run as part of your server so you can run it separately. As long as you have .graphql files to consume it can be used to generate the merged schema

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 🎉 new commands, flags, functionality, and improved error messages triage issues and PRs that need to be triaged
Projects
None yet
Development

No branches or pull requests

4 participants