如何添加“ DirectiverSolvers”到Apollo-Server-Express中间件?

发布于 2025-02-12 19:18:20 字数 1397 浏览 0 评论 0原文

我正在努力从Apollo-Server过渡到Apollo-Server-express,以便我可以实现Stripe Webhooks。

以前使用Apollo-Server,我能够将DirectiverSolvers添加到模式中,以便我可以执行@Auth检查查询或突变。服务器文件看起来像这样:

apollo-server:

import { ApolloServer } from 'apollo-server';
import { ApolloServerPluginLandingPageGraphQLPlayground, ApolloServerPluginLandingPageDisabled } from 'apollo-server-core';
import { CorsOptions } from 'cors';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { applyMiddleware } from 'graphql-middleware';
import { context } from './context';
import { resolvers } from './resolvers';
import typeDefs from './typeDefs';
import directiveResolvers from '@/directives/isAuthorized';

const corsOptions = {
  origin: process.env.ORIGIN_URL,
  credentials: true,
} as CorsOptions;

const graphqlEndpoint = 'graphql';

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
  directiveResolvers,
});

const server = new ApolloServer({
  schema: applyMiddleware(schema),
  context,
  cors: corsOptions,
  introspection: true,
  plugins: [
    process.env.NODE_ENV === 'production'
      ? ApolloServerPluginLandingPageDisabled()
      : ApolloServerPluginLandingPageGraphQLPlayground()
  ],
  debug: true,
});

server
  .listen({ port: process.env.PORT || 4000 })
  .then(({ url }) => console.log(`
              

I'm working on transitioning from apollo-server to apollo-server-express, so that I can implement Stripe webhooks.

Previously with apollo-server, I was able to add directiveResolvers to the schema, so that I can perform an @auth check on queries or mutations. The server file looked like this:

apollo-server:

import { ApolloServer } from 'apollo-server';
import { ApolloServerPluginLandingPageGraphQLPlayground, ApolloServerPluginLandingPageDisabled } from 'apollo-server-core';
import { CorsOptions } from 'cors';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { applyMiddleware } from 'graphql-middleware';
import { context } from './context';
import { resolvers } from './resolvers';
import typeDefs from './typeDefs';
import directiveResolvers from '@/directives/isAuthorized';

const corsOptions = {
  origin: process.env.ORIGIN_URL,
  credentials: true,
} as CorsOptions;

const graphqlEndpoint = 'graphql';

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
  directiveResolvers,
});

const server = new ApolloServer({
  schema: applyMiddleware(schema),
  context,
  cors: corsOptions,
  introspection: true,
  plugins: [
    process.env.NODE_ENV === 'production'
      ? ApolloServerPluginLandingPageDisabled()
      : ApolloServerPluginLandingPageGraphQLPlayground()
  ],
  debug: true,
});

server
  .listen({ port: process.env.PORT || 4000 })
  .then(({ url }) => console.log(`???? Server ready at: ${url}${graphqlEndpoint}`));

With apollo-server-express though, I'm struggling to understand where to add these directiveResolvers in the configuration. Here is what the file currently looks like:

apollo-server-express:

import { ApolloServer } from 'apollo-server-express';
import express from 'express';
import http from 'http';
import {
  ApolloServerPluginLandingPageGraphQLPlayground,
  ApolloServerPluginLandingPageDisabled,
  ApolloServerPluginDrainHttpServer
} from 'apollo-server-core';
import { CorsOptions } from 'cors';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { applyMiddleware } from 'graphql-middleware';
import { context } from './context';
import { resolvers } from './resolvers';
import typeDefs from './typeDefs';
import directiveResolvers from '@/directives/isAuthorized';

const corsOptions: CorsOptions = {
  origin: process.env.ORIGIN_URL,
  credentials: true,
};

async function startApolloServer () {
  const app = express();
  const httpServer = http.createServer(app);
  const server = new ApolloServer({
    typeDefs,
    resolvers,
    context,
    introspection: true,
    plugins: [
      ApolloServerPluginDrainHttpServer({ httpServer }),
      process.env.NODE_ENV === 'production'
        ? ApolloServerPluginLandingPageDisabled()
        : ApolloServerPluginLandingPageGraphQLPlayground()
    ],
  });
  await server.start();
  server.applyMiddleware({ app, cors: corsOptions });
  await new Promise<void>(resolve => httpServer.listen({ port: process.env.PORT || 4000 }, resolve));
  console.log(`???? Server ready at: http://localhost:4000${server.graphqlPath}`);
}

startApolloServer();

How do I go about adding the directiveResolvers to the server configuration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2025-02-19 19:18:20

以经典的方式,我在发布此消息后很快就弄清楚了。我错过了我以前使用过的makeexecutableschema件。现在看起来像这样,似乎正常运行:

import { ApolloServer } from 'apollo-server-express';
import express from 'express';
import http from 'http';
import {
ApolloServerPluginLandingPageGraphQLPlayground,
ApolloServerPluginLandingPageDisabled,
ApolloServerPluginDrainHttpServer
} from 'apollo-server-core';
import { CorsOptions } from 'cors';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { context } from './context';
import { resolvers } from './resolvers';
import typeDefs from './typeDefs';
import directiveResolvers from '@/directives/isAuthorized';

const corsOptions: CorsOptions = {
origin: process.env.ORIGIN_URL,
credentials: true,
};

const schema = makeExecutableSchema({
typeDefs,
resolvers,
directiveResolvers,
});

async function startApolloServer () {
const app = express();
const httpServer = http.createServer(app);
const server = new ApolloServer({
schema,
context,
introspection: true,
plugins: [
ApolloServerPluginDrainHttpServer({ httpServer }),
process.env.NODE_ENV === 'production'
? ApolloServerPluginLandingPageDisabled()
: ApolloServerPluginLandingPageGraphQLPlayground()
],
});
await server.start();
server.applyMiddleware({ app, cors: corsOptions });
await new Promise<void>(resolve => httpServer.listen({ port: process.env.PORT || 4000 }, resolve));
console.log(`

In classic fashion, I figured it out pretty soon after posting this. I'd missed the makeExecutableSchema piece that I'd used before. It now looks like this and seems to be functioning correctly:

import { ApolloServer } from 'apollo-server-express';
import express from 'express';
import http from 'http';
import {
  ApolloServerPluginLandingPageGraphQLPlayground,
  ApolloServerPluginLandingPageDisabled,
  ApolloServerPluginDrainHttpServer
} from 'apollo-server-core';
import { CorsOptions } from 'cors';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { context } from './context';
import { resolvers } from './resolvers';
import typeDefs from './typeDefs';
import directiveResolvers from '@/directives/isAuthorized';

const corsOptions: CorsOptions = {
  origin: process.env.ORIGIN_URL,
  credentials: true,
};

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
  directiveResolvers,
});

async function startApolloServer () {
  const app = express();
  const httpServer = http.createServer(app);
  const server = new ApolloServer({
    schema,
    context,
    introspection: true,
    plugins: [
      ApolloServerPluginDrainHttpServer({ httpServer }),
      process.env.NODE_ENV === 'production'
        ? ApolloServerPluginLandingPageDisabled()
        : ApolloServerPluginLandingPageGraphQLPlayground()
    ],
  });
  await server.start();
  server.applyMiddleware({ app, cors: corsOptions });
  await new Promise<void>(resolve => httpServer.listen({ port: process.env.PORT || 4000 }, resolve));
  console.log(`???? Server ready at: http://localhost:4000${server.graphqlPath}`);
}

startApolloServer();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文