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

Issue: mergeSchemas in graphql-tools v10 overwrites remote schemas instead of combining them. #5395

Closed
inPhoenix opened this issue Jul 2, 2023 Discussed in #5392 · 3 comments

Comments

@inPhoenix
Copy link

inPhoenix commented Jul 2, 2023

The mergeSchemas function in the graphql-tools package seems to have a problem when used with version 10.
Instead of combining schemas (remote executable) as expected, it ends up overwriting them.

In version 4.0, mergeSchemas properly combined executableSchema1 and executableSchema2, as shown in this screenshot:
graphqlv4vsv10

Specific part where the schemas should be merged:

const mergedSchema = mergeSchemas({
    /* executableSchema1 will be overwritten by executableSchema2
    This will happen using @graphql-tools v10 but not with the v4
    * */
    schemas: [executableSchema1, executableSchema2],
})

Full Code:

import { ApolloServer } from "@apollo/server" // v4
import { startStandaloneServer } from "@apollo/server/standalone"
import { schemaFromExecutor, wrapSchema } from "@graphql-tools/wrap"
import { buildHTTPExecutor } from "@graphql-tools/executor-http"
import { mergeSchemas } from "@graphql-tools/schema" // this version don't merge the schemas

// import { mergeSchemas } from "graphql-tools" // graphql-tools ^2.9.7 (It merge the schemas)

async function mergedSchema() {
  const starwars = "https://swapi-graphql.netlify.app/.netlify/functions/index"
  const rickandmorty = "https://rickandmortyapi.com/graphql"

  // starwars schema
  const remoteExecutor1 = buildHTTPExecutor({
    endpoint: starwars,
    headers: {
      "Content-Type": "application/json",
      Accept: "application/json",
    },
  })
  const schema1 = await schemaFromExecutor(remoteExecutor1)
  const executableSchema1 = wrapSchema({
    schema: schema1,
    executor: remoteExecutor1,
  })

  // rickandmorty schema
  const remoteExecutor2 = buildHTTPExecutor({
    endpoint: rickandmorty,
    headers: {
      "Content-Type": "application/json",
      Accept: "application/json",
    },
  })
  const schema2 = await schemaFromExecutor(remoteExecutor2)
  const executableSchema2 = wrapSchema({
    schema: schema2,
    executor: remoteExecutor2,
  })

  // Merge the schemas
  const mergedSchema = mergeSchemas({
    /* executableSchema1 will be overwritten by executableSchema2
    This will happen using @graphql-tools v10 but not with the v4
    * */
    schemas: [executableSchema1, executableSchema2],
  })

  return mergedSchema
}

const runServer = async () => {
  const server = new ApolloServer({
    schema: await mergedSchema(),
  })

  const { url } = await startStandaloneServer(server, {
    listen: { port: 4000 },
  })
  console.log(`🚀  Server ready at ${url}`)
}

try {
  runServer()
} catch (error) {
  console.error(error)
}

The full code with package.json and server.js can be found here:
https://github.com/inPhoenix/graphql-tools-testing

@ardatan
Copy link
Owner

ardatan commented Jul 2, 2023

I couldn't understand how it overwrites the previous schema. Could you create a more isolated example without http endpoints in CodeSandbox? Thanks!
Also keep in mind that remote schemas are not supposed to be merged like local schemas. Schema stitching is recommended in this case.
See the note here;
https://the-guild.dev/graphql/tools/docs/schema-merging

Schema merging (@graphql-tools/merge and @graphql-tools/schema) consolidates the type definitions and resolvers from many local schema instances into a single executable schema. This is useful for building a single local service schema from many individually-managed parts. This should not be confused with schema stitching(opens in a new tab), which builds a combined proxy schema atop numerous subservice APIs.

@inPhoenix
Copy link
Author

inPhoenix commented Jul 2, 2023

The problem might be only with remote schemas indeed. As the v4 works I was wondering if v10 should not work on the same way. I will check the schema stitching.

@inPhoenix inPhoenix changed the title Issue: mergeSchemas in graphql-tools v10 overwrites schemas instead of combining them. Issue: mergeSchemas in graphql-tools v10 overwrites remote schemas instead of combining them. Jul 2, 2023
@ardatan
Copy link
Owner

ardatan commented Jul 2, 2023

https://the-guild.dev/graphql/tools/docs/migration/migration-from-tools#schema-stitching-stitchschemas--graphql-toolsstitch-1

You can see here that mergeSchemas no longer does stitching and renamed to stitchSchemas which is part of Schema Stitching. mergeSchemas now has a different implementation that is part of Schema Merging.
Closing the issue since the changes are described in the migration guide.

@ardatan ardatan closed this as completed Jul 2, 2023
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

No branches or pull requests

2 participants