如何添加“ DirectiverSolvers”到Apollo-Server-Express中间件?
我正在努力从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以经典的方式,我在发布此消息后很快就弄清楚了。我错过了我以前使用过的
makeexecutableschema
件。现在看起来像这样,似乎正常运行: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: