模式拼接的自定义指令

时间:2018-11-29 11:59:16

标签: graphql apollo graphql-js

我正在尝试将指令添加到从远程模式扩展一个指令的类型。

   const extensionSchema = `
    directive @upper on FIELD_DEFINITION
    extend type User {
        firstname: String @upper
    }

`;
    // resolvedSchemas is an array of makeRemoteExecutableSchema coming from introspection queries.
resolvedSchemas.push(extensionSchema);

return mergeSchemas({
    schemas: resolvedSchemas,
    schemaDirectives: {
        upper: UpperCaseDirective
    }
   })

然后我得到:Error: Unknown directive "upper".。 我想念什么?

我的指令:

const { SchemaDirectiveVisitor } = require("graphql-tools");
const { defaultFieldResolver } = require("graphql");

module.exports = class UpperCaseDirective extends SchemaDirectiveVisitor {
  visitFieldDefinition(field) {
    const { resolve = defaultFieldResolver } = field;
    field.resolve = async function (...args) {
            const result = await resolve.apply(this, args);
            console.log("RESULTADOOOO", result);
      if (typeof result === "string") {
        return result.toUpperCase();
      }
      return result;
    };
  }
}

0 个答案:

没有答案