【问题标题】:Extend SchemaDirectiveVisitor To Use Apollo Server Schema Directives in NodeJS扩展 SchemaDirectiveVisitor 以在 NodeJS 中使用 Apollo 服务器模式指令
【发布时间】:2019-05-09 16:53:20
【问题描述】:

我正在尝试扩展 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

任何帮助使指令正常工作将不胜感激。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-06
      • 2018-02-23
      • 2017-06-17
      • 2020-12-21
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多