apollo-server-azure-functions with apollo gateway federation
Use apollo gateway in Azure functions, can open the playground html page, but failed to query
return result is
{
"error": "Unexpected end of JSON input"
}
here is my azure functions code
const { gql, ApolloServer } = require("apollo-server-azure-functions");
const { ApolloGateway } = require("@apollo/gateway");
const gateway = new ApolloGateway({
serviceList: [
{ name: "accounts", url: "https://pw678w138q.sse.codesandbox.io/graphql" },
{ name: "reviews", url: "https://0yo165yq9v.sse.codesandbox.io/graphql" },
{ name: "products", url: "https://x7jn4y20pp.sse.codesandbox.io/graphql" },
{ name: "inventory", url: "https://o5oxqmn7j9.sse.codesandbox.io/graphql" }
]
});
async function abc(context, req) {
const { schema, executor } = await gateway.load();
const server = new ApolloServer({ schema, executor });
server.createHandler()(context, req);
}
module.exports = abc;
@kendeng9898 is there any more info you can provide?
@jbaxleyiii I have a similar issue. The error is thrown in the file ./node_modules/apollo-link-http-common/lib/index.js line 40.
The HTTP response is the following
{
bodyUsed: true
headers: {}
ok: true
redirected: false
status: 204
statusText: "No Content"
type: "basic"
url: "http://localhost:7071/api/gateway"
}
The error is thrown when i surf to http://localhost:7071/api/gateway or http://localhost:7071/api/gateway/graphql.
My setup is:
import { ApolloServer } from "apollo-server-azure-functions";
import { ApolloGateway } from "@apollo/gateway";
export const handler = async (...args) => {
const gateway = new ApolloGateway({
serviceList: [
{ name: "foo", url: "http://localhost:7071/api/foo" },
{ name: "bar", url: "http://localhost:7071/api/bar" }
]
});
const { schema, executor } = await gateway.load();
const server = new ApolloServer({ schema, executor: executor as any });
return server.createHandler();
};
It is possible to open the playground on the foo and bar url, and perform queries.
I am not able to open the playground from the gateway.
This seems to be an issue with how Azure handles async handlers. If you look at the docs (https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#exporting-a-function) you can see azure waits for either a context.done or if the supplied async method is completed. We have solved (rather ugly) it by leveraging the context.done;
import { ApolloGateway } from "@apollo/gateway";
import { Context, HttpRequest } from "@azure/functions";
import { ApolloServer } from "apollo-server-azure-functions";
let apolloHandler: (context: Context, req: HttpRequest) => void;
export const handler = async (context: Context, req: HttpRequest) => {
if (!apolloHandler) {
const gateway = new ApolloGateway({
serviceList: [
{ name: "foo", url: "http://localhost:7071/api/foo" },
{ name: "bar", url: "http://localhost:7071/api/bar" },
]
});
const { schema, executor } = await gateway.load();
const server = new ApolloServer({ schema, executor: executor as any });
apolloHandler = server.createHandler();
}
return new Promise((resolve, reject) => {
const originalDone = context.done;
context.done = (error, result) => {
originalDone(error, result);
resolve();
};
apolloHandler(context, req);
});
};
This seems to be an issue with how Azure handles async handlers. If you look at the docs (https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#exporting-a-function) you can see azure waits for either a context.done or if the supplied async method is completed. We have solved (rather ugly) it by leveraging the context.done;
import { ApolloGateway } from "@apollo/gateway"; import { Context, HttpRequest } from "@azure/functions"; import { ApolloServer } from "apollo-server-azure-functions"; let apolloHandler: (context: Context, req: HttpRequest) => void; export const handler = async (context: Context, req: HttpRequest) => { if (!apolloHandler) { const gateway = new ApolloGateway({ serviceList: [ { name: "foo", url: "http://localhost:7071/api/foo" }, { name: "bar", url: "http://localhost:7071/api/bar" }, ] }); const { schema, executor } = await gateway.load(); const server = new ApolloServer({ schema, executor: executor as any }); apolloHandler = server.createHandler(); } return new Promise((resolve, reject) => { const originalDone = context.done; context.done = (error, result) => { originalDone(error, result); resolve(); }; apolloHandler(context, req); }); };
@JannesV Thanks for your code. Great help! This can let the gateway works in azure functions. But failed to return the result with proper data, it will always return null in data.
Try to replace the service List to this code sand box's serviceList. It can't return the result as this codesandbox. Any idea on this? https://codesandbox.io/embed/v368r9ml47?codemirror=1
after upgrade the apollo-server-azure-functions from 2.4.8 to 2.6.7 can get the data with the same code as JannesV provide.
Thanks for reporting back @kendeng9898!
Here is my approach:
import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import { ApolloServer, gql } from "apollo-server-azure-functions";
import { ApolloGateway } from "@apollo/gateway";
const startApollo = () => {
let handler: AzureFunction | undefined;
const init = async () => {
const gateway = new ApolloGateway({
serviceList: [
{ name: "foo", url: "http://localhost:7071/api/foo" },
{ name: "bar", url: "http://localhost:7071/api/bar" },
],
});
const { schema, executor } = await gateway.load();
const server = new ApolloServer({ schema, executor: executor as any });
handler = server.createHandler()
console.log("Apollo server started.");
};
init();
return (context: Context, req: HttpRequest) => {
if (handler) handler(context, req);
};
};
exports.graphqlHandler = startApollo();