【问题标题】:ApolloServer not firing custom directivesApolloServer 没有触发自定义指令
【发布时间】:2020-09-07 17:16:43
【问题描述】:

我正在尝试根据 apollo 服务器文档编写一些身份验证逻辑,但它似乎没有触发。这是我的代码:

// schemas/auth-schema.js
import gql from 'graphql-tag';

export const typeDefs = gql`
  directive @auth(requires: Role = ADMIN) on OBJECT | FIELD_DEFINITION
`;
// directives/auth-directive.js
import { SchemaDirectiveVisitor } from 'apollo-server';

export default class AuthDirective extends SchemaDirectiveVisitor {
  visitObject(type) {
    console.log('HERE');
  }
  visitSchema() {
    console.log('HERE');
  }
  visitFieldDefinition() {
    console.log('HERE');
  }
}
// schemas/post-schema.js
import gql from 'graphql-tag';
import { Post } from '../models';

export const typeDefs = gql`
  type Post @auth(requires: ADMIN) {
    body: String!
    description: String!
    id: ID!
    image: String!
    publishedAt: DateTime
    readingTime: Int!
    slug: String!
    title: String!
  }

  input PostInput {
    body: String!
    description: String!
    image: String!
    publishedAt: DateTime
    title: String!
  }

  extend type Query {
    posts: [Post!]! @auth(requires: ADMIN)
  }

  extend type Mutation {
    addPost(input: PostInput!): Post! @auth(requires: ADMIN)
  }
`;

export const resolvers = {
  Query: {
    posts: () => Post.find({}),
  },
  Mutation: {
    addPost: (_, { input }) => Post.create(input),
  },
};

// index.js

import { ApolloServer } from 'apollo-server';
import mongoose from 'mongoose';
import AuthDirective from './directives/auth-directive';
import * as config from './config';

mongoose.set('useNewUrlParser', true);
mongoose.set('useCreateIndex', true);
mongoose.set('debug', config.env !== 'production');

const server = new ApolloServer({
  modules: [
    require('./schema/auth-schema'),
    require('./schema/date-schema'),
    require('./schema/post-schema'),
    require('./schema/role-schema'),
    require('./schema/user-schema'),
  ],
  schemaDirectives: {
    auth: AuthDirective,
  },
});

async function boot() {
  await mongoose.connect(config.mongo.url);
  await server.listen(config.http.port);
  console.log(`server listening on port ${config.http.port}`);
}

async function shutdown() {
  await server.stop();
  await mongoose.disconnect();
  console.log(`server shutted down`);
  process.exit(0);
}

process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);

boot();

所以,我尝试在所有可能的情况下都使用 @auth 指令,但没有任何东西被触发。

type Post @auth(requires: ADMIN) { ... } // not firing

type Query {
  posts: [Post!]! @auth(requires: ADMIN) // not firing
}

这是从控制台评估的 AdminDirective:

我做错了什么?

【问题讨论】:

  • 一目了然,该设置看起来是正确的。你使用的是什么版本的 apollo-server?
  • @DanielRearden 我正在使用 apollo-server 版本 2.4.0
  • 嗯,我刚刚在本地测试了该代码,它对我来说很好。
  • 您能否更新您的问题以显示完整的auth-schema.js
  • 另外,如果您在index.js 中记录AuthDirective 的值,它是否按照定义显示?

标签: graphql apollo-server


【解决方案1】:

因此,查看apollo-server 的代码,当您使用modules 选项时,您的架构在内部是使用buildServiceDefinition 构建的。虽然这个函数确实合并了所有模块的指令,但它没有传递你的 schemaDirectives 对象,也没有应用它。

换句话说,这看起来像是apollo-server 本身的错误。您可以提出问题,与此同时,只需使用 typeDefsresolvers 选项,自行组合必要的文件。

【讨论】:

  • 好吧,看起来如果你用模式交换模块并使用 makeExecutableSchema 工作正常。你真可惜,它看起来更干净:'(感谢您的帮助和时间!
  • 好像已经打开了一个issue但是关注的不多:github.com/apollographql/apollo-server/issues/2046
  • @brielov 除了makeExecutableSchema 之外,您还可以看看使用merge-graphql-schemas。这就是我在一个项目中实际使用的。
  • 我一定会看看的。谢谢!
猜你喜欢
  • 2017-05-06
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2013-08-17
  • 2018-06-13
  • 1970-01-01
  • 2012-09-10
相关资源
最近更新 更多