ardatan / ardatan/graphql-tools

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

Open
#5,473 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
5.4k
Forks
830
Avg merge
10h 59m
Merged PRs (30d)
45

Description

Hello, 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:-

```javascript
// 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:-

```javascript
// 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!"

Contributor guide

Open the contributing guide

Research direction

Start with createSchema and getRemoteSchema in the issue's old and new snippets, then inspect how the ApolloServer and expressMiddleware setup passes context to stitched remote APIs. Reproduce the null responses against the listed remote services and compare them with the successful introspection query. Done means stitched API operations resolve correctly with the latest packages and the deprecated functions are replaced.

Written by the indexing model from the issue text.

Assessment

Tech stack
express, javascript, node.js, typescript
Domain
api, backend, distributed-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.