ApolloServer не запускает пользовательские директивы - PullRequest
0 голосов
/ 13 февраля 2019

Я пытаюсь написать некоторую логику аутентификации, основанную на документации сервера 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 с консоли: enter image description here

Что я делаю не так?

1 Ответ

0 голосов
/ 13 февраля 2019

Итак, глядя на код для apollo-server, когда вы используете опцию modules, внутренне ваша схема строится с использованием buildServiceDefinition.Хотя эта функция объединяет директивы всех модулей, она не передает ваш объект schemaDirectives и не применяет его.

Другими словами, это похоже на ошибку с самим apollo-server.Вы можете подать проблему, а пока просто используйте опции typeDefs и resolvers, комбинируя необходимые файлы самостоятельно.

...