Graphql-js 自定义指令在管道中执行
我试图围绕自定义指示。我使用Neo4J的GraphQl。但是我认为这是一个普遍的问题。我需要执行以下操作:
- 查询neo4j并构建一个用作ID的字符串。因此,我在那里使用@Cypher指令。返回字符串
- ,然后使用此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:
- Query neo4j and construct a string, that serves as an ID. So I use there @cypher directive. This returns a string
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论