【问题标题】:GraphQL / Apollo Server Directives leading to schema error导致架构错误的 GraphQL / Apollo 服务器指令
【发布时间】:2019-04-13 00:03:15
【问题描述】:

我正在尝试将指令添加到我们的 GraphQL API 并遵循此示例 https://www.apollographql.com/docs/apollo-server/features/creating-directives.html#Uppercasing-strings,但是,我不断收到服务器错误(如果没有指令,服务器可以正常工作!):

** Uncaught Error: Duplicate schema type name Integer **
    at home/node_modules/apollo-server-core/node_modules/graphql-tools/dist/schemaVisitor.js:243:27
    at home/node_modules/apollo-server-core/node_modules/graphql-tools/dist/schemaVisitor.js:527:9
    at Array.forEach (<anonymous>)
    at each (home/node_modules/apollo-server-core/node_modules/graphql-tools/dist/schemaVisitor.js:526:32)
    at heal (home/node_modules/apollo-server-core/node_modules/graphql-tools/dist/schemaVisitor.js:234:13)
    at healSchema (home/node_modules/apollo-server-core/node_modules/graphql-tools/dist/schemaVisitor.js:225:5)
    at Function.SchemaDirectiveVisitor.visitSchemaDirectives (home/node_modules/apollo-server-core/node_modules/graphql-tools/dist/schemaVisitor.js:474:9)
    at Object.makeExecutableSchema (home/node_modules/apollo-server-core/node_modules/graphql-tools/dist/makeExecutableSchema.js:54:48)
    at new ApolloServerBase (home/node_modules/apollo-server-core/dist/ApolloServer.js:142:43)
    at new ApolloServer (home/node_modules/apollo-server-express/dist/ApolloServer.js:44:1)

架构非常大,所以我不能在这里发布,但是架构类型“整数”没有被复制。我遵循了多个不同的指南,但一直遇到此错误。

我们正在使用 Apollo Server 2.1.0 和 GraphQL 14.0.2

服务器设置:

class error_extension extends GraphQLExtension {
  willSendResponse(o) {
    const { context, graphqlResponse } = o;

    context.add_additional_error_reporting(graphqlResponse.errors);

    return o;
  }
}


// Create (or import) a custom schema directive
class UpperCaseDirective extends SchemaDirectiveVisitor {
  visitFieldDefinition(field) {
    const { resolve = defaultFieldResolver } = field;
    field.resolve = async function (...args) {
      const result = await resolve.apply(this, args);
      if (typeof result === 'string') {
        return result.toUpperCase();
      }
      return result;
    };
  }
}


// Server settings
const server = new ApolloServer({
  typeDefs,
  resolvers,
  schemaDirectives: {
    upper: UpperCaseDirective,
  },
  context: async ({ req, res }) => {
    // some auth logic here - removed for the purpose of this

    return {
      add_additional_error_reporting(errors) {
        if (!errors) return;

        errors = errors.map(item => {

          // add request data to the returned error
          if (`extensions` in item && item.extensions !== undefined)
            item.extensions.request_details = req.body;

          return item;
        });

        return errors;
      },
      req,
      res
    };
  },
  extensions: [() => new error_extension()],
  validationRules: [

    depthLimit(5),

    costAnalysis({ // GraphQL Query Cost 
      maximumCost: 100, // maximum cost of a single  query
      defaultCost: 1, // default cost of a field
      onComplete: (async request_cost => {
        // Callback function to retrieve the determined query cost. It will be invoked whether the query is rejected or not. 
        // This can be used for logging or to implement rate limiting (for example, to store the cost by session and define a max cost the user can have in a specific time).
        try { 
          cost_data = await helper.run_connector_cost_logic(request_cost);
        } catch (err) { throw err; }
      }),
      createError: (maximumCost, cost) => {
        // Function to create a custom error
        throw new ApolloError(`The maximum cost per request is ${maximumCost} however this request is greater than this limit, with a cost of ${cost}.`, `EXCEEDED_MAX_REQ_COST`);
      }
    })

  ],
  formatError: err => { 
    console.error(err); // eslint-disable-line
    return err; 
  },
  formatResponse: res => {
    res.extensions.cost = cost_data;

    // wipe the cost data for the next request
    cost_data = {};

    return res;
  },
  introspection: false,
  tracing: false,
  debug: false
});

任何帮助将不胜感激!

【问题讨论】:

  • 文档中的示例按原样工作,所以我怀疑它是别的东西。很高兴看到您的架构,但至少您应该包含完整的 apollo-server 配置。这可能会为正在发生的事情提供一些额外的见解。
  • FWIW,here 有一个问题,但看起来并没有太大的吸引力。
  • @DanielRearden 感谢您再次加入 - 现在将服务器设置添加到问题中。我也会对这个问题发表评论。谢谢!
  • 如果您有一个名为 Integer 的自定义标量,那么这就是您的错误所指的内容。标量仍然是类型。错误来自graphql-tools(请看here)。除非您包含指令,否则不会调用该验证逻辑。可能是 graphql-tools 的错误。您设置自定义标量的方式可能存在问题。
  • 很想知道您如何在 typeDefs 中包含和使用自定义标量

标签: node.js graphql apollo-server


【解决方案1】:

这是我们使用 JS 逻辑生成的标量的问题,但是一个语句存在缺陷,导致多个标量具有相同的名称值。

对于其他任何人,正如@Daniel Rearden 在 cmets 中所说的那样“错误来自 graphql-tools。除非您包含指令,否则不会调用验证逻辑”因此为什么错误才刚刚出现,即它是与指令无关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-14
    • 2019-07-26
    • 2017-11-07
    • 2019-07-31
    • 2021-07-28
    • 2021-03-18
    • 2011-01-01
    • 2020-11-26
    相关资源
    最近更新 更多