1 year ago

#364872

test-img

Manuel Baun

Graphql-js Custom Directive execute in Pipeline

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?

node.js

neo4j

graphql

graphql-js

0 Answers

Your Answer

Accepted video resources