扩展SchemaDirectiveVisitor以在NodeJS中使用Apollo服务器架构指令

时间:2018-12-07 22:48:09

标签: node.js graphql directive apollo-server hapi

我正在尝试扩展SchemaDirectiveVisitor,以便在Apollo Server 2中做出自定义指令。我专门使用2.2.6 hapi节点模块。

这是我的server.js代码:

const { ApolloServer } = require('apollo-server-hapi');
const { SchemaDirectiveVisitor } = ApolloServer;

class ViewTemplateGroup extends SchemaDirectiveVisitor {
  visitFieldDefinition(field) {
    console.log('Im calling this directive!');
    return;
  }
}

启动服务器时,我立即收到以下错误消息:

TypeError: Class extends value undefined is not a constructor or null
    at Object.<anonymous> (/Users/garrett.kim/Desktop/Projects/Test Web/poc-graphQL-forms-gyk/server.js:36:33)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
    at startup (bootstrap_node.js:193:16)
    at bootstrap_node.js:617:3

据我所知,我非常关注Apollo Server 2示例。

https://www.apollographql.com/docs/apollo-server/features/creating-directives.html

任何帮助使指令正常工作的帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

ApolloServer类上没有SchemaDirectiveVisitor属性;因此,调用ApolloServer.SchemaDirectiveVisitor导致未定义,并且如错误所示,无法扩展未定义的类。只需直接从SchemaDirectiveVisitor模块导入apollo-server-hapi

const { ApolloServer, SchemaDirectiveVisitor } = require('apollo-server-hapi')