如何解决覆盖预定义的 GraphQLSchema 解析器 (Apollo)
我正在为现有的 REST API 设置一个 GraphQL 包装器。我选择的工具可让您轻松定义对 REST API 的所有调用的方式,并为每个端点生成解析器。
我希望能够使用自定义逻辑覆盖某些查询或更改,并且我有一种预感,使用一些 graphql 工具很容易实现,但我还没有登陆它。
下面是设置服务器的主要代码,然后是用于查询 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
},},};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。