How to do schema-stitching and introspection in new @apollo/server and graphql-tools?

How to do schema-stitching and introspection in new @apollo/server and graphql-tools?


0

I’m working on a Node.js project having a main-service(A central component where various remote microservices which are exposed with respective "port/graphql" are stitched together to create a unified API endpoint. It acts as a gateway or aggregator, allowing clients to access multiple functionalities provided by different microservices through a single entry point.).

Now, I want to upgrade all the npm packages to latest versions. Currently in the main service I am using below packages and versions and moving to latest versions
From TO
graphql-tools V4.0.5 V9.0.0
apollo-server-express V2.7.2 @apollo/server V4.8.0
express V4.17.1 V4.18.2
graphql V14.4.2 V16.7.1

I’m able to stitch the schema of remote schemas using old packages and now i’m currently able to stitch and unable to resolve the API’s using new versions.

I noticed that the introspectSchema and makeRemoteExecutableSchema functions are now deprecated.

I tried schema stitching using introspectionSchema with old version of packages and then again with new versions of packages:-

Old code:-

// Code snippet 1: Stitching remote schemas
`const createSchema = async () => {
  // ...
  for (let service of servicesList) {
    // ...
    retSchema = await utils.getRemoteSchema(
      url,
    );
    // ...
  }
  return mergeSchemas({
    schemas: remoteSchemas,
  });
};`

// Code snippet 2: Method to get remote schema and create executable schema

  `getRemoteSchema: async (url) => {
    const schemaHttpLink = new HttpLink({ uri, fetch });
    const httpLink = setContext((request, previousContext) => {
      return {
        headers: {
          // ...
        },
      };
    }).concat(schemaHttpLink);
    const schema = await introspectSchema(schemaHttpLink);
    const executableSchema = makeRemoteExecutableSchema({
      schema,
      link,
    });
    // ...
  }`

// Code snippet 3: Creating the ApolloServer instance
`const start = async () => {
  const schema = await createSchema();
  const server = new ApolloServer({
    schema,
    context: async ({ req, connection }) => {
      // ...
    },
  });
  // ...
};`

New Code:-

// Code snippet 1: Stitching remote schemas

import { buildHTTPExecutor } from "@graphql-tools/executor-http";
import { wrapSchema, schemaFromExecutor } from "@graphql-tools/wrap";
`const createSchema = async () => {
  // ...
  for (let service of servicesList) {
    // ...
    retSchema = await utils.getRemoteSchema(
      url,
    );
    // ...
  }
  return mergeSchemas({
    schemas: remoteSchemas,
  });
};`

// Code snippet 2: Method to get remote schema and create executable schema

  `getRemoteSchema: async (url) => {

   const remoteExecutor = buildHTTPExecutor({
      endpoint: uri,
    });

      let Subschema = {
      schema: await schemaFromExecutor(remoteExecutor),
      executor: remoteExecutor,
    };

    return Subschema;

  }`

// Code snippet 3: Creating the ApolloServer instance
` const schema = await createSchema();

  const server = new ApolloServer({
    schema,
    introspection: true,
  });

const app = express();

await server.start();

  app.use(
    `/graphql`,
    cors(),
    json(),
    expressMiddleware(server, {
    context: async ({ req, connection }) => {
    //Some manipulation of context //
    return context;
        }
        if (connection && connection.context) {
          return connection.context;
        }
     }`

With latest versions and new code i’m able to stitch schemas and able to receive introspectionQuery. But i receive ‘null’ when calling the APIs stitched in my main-service.

Could you please guide me on how to update my code to work with the latest versions of graphql-tools and apollo-server? Specifically, I need assistance with the replacement for introspectSchema and makeRemoteExecutableSchema, and any other changes necessary to successfully stitch together the remote schemas.
Thank you in advance for your help in updating my schema stitching process with the latest graphql-tools and apollo-server versions!"

Share


Load 3 more related questions


Show fewer related questions

0

Reset to default



Leave a Reply

Your email address will not be published. Required fields are marked *