apollographql / rover

The CLI for Apollo GraphOS
https://rover.apollo.dev
Other
409 stars 86 forks source link

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

Open wangmir opened 3 months ago

wangmir commented 3 months ago

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?

julioocamargoo commented 2 months ago

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

Cellule commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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

LongLiveCHIEF commented 1 month ago

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 commented 1 month ago

@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 commented 3 weeks ago

@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 commented 3 weeks ago

@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