[英]Overriding pre-defined GraphQLSchema resolvers (Apollo)
我正在为现有的 REST API 设置 GraphQL 包装器。 我决定使用的工具可让您轻松定义对 REST API 的所有调用的方式,并为每个端点生成解析器。
我希望能够用自定义逻辑覆盖某些查询或突变,我预感使用一些 graphql-tool 很容易实现,但我还没有登陆它。
下面是设置服务器的主要代码,然后是用于查询 REST API 的获取客户端,然后是我希望能够扩展某些端点的方式。
// The main server setup thus far
import express from "express";
import cors from "cors";
import { createServer } from "http";
import { callBackend } from "./connections";
import { createSchema } from "swagger-to-graphql";
import { ApolloServer } from "apollo-server-express";
import { generateTypes } from "./utils";
import { parse, printSchema } from "graphql";
const port = process.env.PORT || 4000;
(async () => {
const app = express();
const server = createServer(app);
app.use(cors({ origin: "*", credentials: true }));
const schema = await createSchema({
swaggerSchema: "URL_POINTING_TO_SWAGGER_DEFINITION",
callBackend,
});
await generateTypes(parse(printSchema(schema)));
server.listen(port, () => {
console.log("Server has launched..");
});
const apollo = new ApolloServer({
introspection: true,
debug: true,
tracing: true,
context: ({ req, res }) => ({ req, res }),
schema,
});
apollo.applyMiddleware({ app, cors: { origin: "*", credentials: false } });
})();
_________________________________________
// The callBackend function being imported in the previous code.
export async function callBackend({
context,
requestOptions,
}: CallBackendArguments<Request>) {
return axios({
method: requestOptions.method,
url: `URL${requestOptions.path}`,
headers: requestOptions.headers,
params: requestOptions.query,
data: requestOptions.body,
});
}
_________________________________________
// The way in which I would like to be able to override certain calls being generated by the
// swagger tools createSchema function. These ideally just get passed into ApolloServer's args as
// { resolvers: [CustomerResolvers, ...etc] }
export const CustomerResolvers = {
Query: {
get_customers(...args) {
// some additional logic
},
},
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.