Graphql-js 自定义指令在管道中执行

发布于 2025-01-18 11:39:10 字数 1900 浏览 5 评论 0原文

我试图围绕自定义指示。我使用Neo4J的GraphQl。但是我认为这是一个普遍的问题。我需要执行以下操作:

  1. 查询neo4j并构建一个用作ID的字符串。因此,我在那里使用@Cypher指令。返回字符串
  2. ,然后使用此ID并请求另一个端点使用我的自定义指令。这将返回一系列小物体。

它基本上是一个指令的结果。在编写架构的变换函数以允许我的自定义指令时,fieldConfig.resolve函数 - 函数在其GraphQl接口或类型时不会被调用。如果我将test字段的返回类型设置为字符串,则执行Resolve函数。

在解决功能中,我执行我的休息请求并返回结果。显然,结果现在是对象数组,而不是字符串。因此,GraphQL抱怨不匹配类型。

directive @Rest on FIELD_DEFINITION

type Query {
    test: ResultType @Rest @cypher(statement """ some cypher query return string """)
}

type ResultType {
  timestamp: Float
  unit: String
  value: String
}
(schema: GraphQLSchema) =>
    mapSchema(schema, {
        [MapperKind.OBJECT_FIELD]: (fieldConfig) => {
            const dirs = getDirective(schema, fieldConfig, directiveName);
            const fieldDirective = dirs?.[0];
            if (fieldDirective) {
                const resolve = fieldConfig.resolve || defaultFieldResolver;

                fieldConfig.resolve = async (source, args, context, info) => {
                    const result = await resolve(source, args, context, info);
                    const arr = await getArgumentValues(source, args, context);

                    if (typeof result === "string") {
                        try {
                            const response = await axios.get("http://some-rest-endpoint");
                            const data= response.data || [];
                            
                            return data;
                        } catch (e) {
                            console.error(e);
                            return [];
                        }
                    }
                    return [];
                };
            }

            return fieldConfig;
        },
    });

是否有解决方案或提示来管理我要实现的目标?

i try to get my head around about custom directives. I use the graphql from neo4j. But I think it is a general question. I need to do the following:

  1. Query neo4j and construct a string, that serves as an ID. So I use there @cypher directive. This returns a string
  2. then use this ID and request another endpoint use my custom directive. This will return an array of small objects.

Its basically piping one result of a directive to the other. When writing the transform function for the schema to allow my custom directive, the fieldConfig.resolve-Function does not get called, when its an graphql interface or type. If i would set the return type of my test field to String, then the resolve function is executed.

In the resolve function I do my rest request and return the result. Obviously the result is now an array of objects and not a string. Therefore graphql complains about mismatching types.

directive @Rest on FIELD_DEFINITION

type Query {
    test: ResultType @Rest @cypher(statement """ some cypher query return string """)
}

type ResultType {
  timestamp: Float
  unit: String
  value: String
}
(schema: GraphQLSchema) =>
    mapSchema(schema, {
        [MapperKind.OBJECT_FIELD]: (fieldConfig) => {
            const dirs = getDirective(schema, fieldConfig, directiveName);
            const fieldDirective = dirs?.[0];
            if (fieldDirective) {
                const resolve = fieldConfig.resolve || defaultFieldResolver;

                fieldConfig.resolve = async (source, args, context, info) => {
                    const result = await resolve(source, args, context, info);
                    const arr = await getArgumentValues(source, args, context);

                    if (typeof result === "string") {
                        try {
                            const response = await axios.get("http://some-rest-endpoint");
                            const data= response.data || [];
                            
                            return data;
                        } catch (e) {
                            console.error(e);
                            return [];
                        }
                    }
                    return [];
                };
            }

            return fieldConfig;
        },
    });

Is there a solution or a hint to manage what I try to achieve?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文