-
Hey everyone! I'm trying to stitch together (ha!) a proxy gateway for a GraphQL microservice using schema stitching. The way I'm set up is - there's a Yoga server (with Schema Stitching) on AWS Lambda + API Gateway. It acts as a proxy for 2 sub-schemas:
The setup works perfectly when there's no authorization. But the remote sub-schema requires a valid JWT token in an The closest thread I found is #5241 but it doesn't really describe how they were able to get the header to remote executor's context. Could someone point me in the right direction, please? I'm at my wit's end. This is what I'm working with right now: import { buildHTTPExecutor } from "@graphql-tools/executor-http";
import { stitchSchemas } from "@graphql-tools/stitch";
import { schemaFromExecutor } from "@graphql-tools/wrap";
import SchemaBuilder from "@pothos/core";
import { createYoga } from "graphql-yoga";
import { GraphQLSchema } from "graphql";
// Local subschema
const builder = new SchemaBuilder({});
builder.queryType({
fields: (t) => ({
hello: t.string({
nullable: false,
resolve: () => "world",
}),
}),
});
const localSchema = builder.toSchema();
// The entire stitched schema
let stitchedSchema: GraphQLSchema;
const createStitchedSchema = async () => {
// Remote subschema
const appSyncRemoteExecutor = buildHTTPExecutor({
endpoint: "https://remoteschema.example/graphql",
headers: (executorRequest) => {
// How can I get the header into executorRequest.context ?
},
});
const appSyncRemoteSubschema = {
schema: await schemaFromExecutor(appSyncRemoteExecutor),
executor: appSyncRemoteExecutor,
};
return stitchSchemas({
subschemas: [appSyncRemoteSubschema, localSchema],
});
};
export const handler = async (event, context) => {
if (stitchedSchema === undefined) {
stitchedSchema = await createStitchedSchema();
}
const yoga = createYoga({
schema: stitchedSchema,
// This doesn't really help
context: { headers: event.headers },
});
const queryParamEntries = Object.entries(event.queryStringParameters);
const safeQueryParamEntries = queryParamEntries.map(([key, value]) => [
key,
value ?? "",
]);
const queryUrlSearchParams = new URLSearchParams(safeQueryParamEntries);
const fetchPath = event.path + "?" + queryUrlSearchParams.toString();
const body = event.body
? Buffer.from(event.body, event.isBase64Encoded ? "base64" : "utf8")
: undefined;
const fetchContext = {
method: event.httpMethod,
headers: event.headers,
body,
};
const response = await yoga.fetch(fetchPath, fetchContext, {
event,
context,
});
return {
statusCode: response.status,
body: await response.text(),
isBase64Encoded: false,
};
}; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can find |
Beta Was this translation helpful? Give feedback.
Sorry my mistake. It should be "context". In the introspection request to fetch the schema from the service, "context" is undefined because it is not done by the user request but the gateway to fetch the schema via "schemaFromExecutor". If you want to have context there as well, you can pass a context to the function, too.