Neo4j-graphql-js: Argument "direction" has invalid value "BOTH"

Created on 11 Dec 2020  路  4Comments  路  Source: neo4j-graphql/neo4j-graphql-js

At start time, each graphql field annotated with a directive will go through all existing schema transformers.
I have a @percentage directive which throws an unrelated error on the params of a different directive @relation

GraphQLError: Argument "direction" has invalid value "BOTH".

As far as I understand, "IN", "OUT" and "BOTH" are all valid inputs for the @relation directive's direction argument.
This forces me to try/catch the getDirectives call for every directive transformer I'm creating...

schema:

directive @percentage on FIELD_DEFINITION

type User {
  friends: [User] @relation(name: "HAS_FRIEND", direction: "BOTH")
}

type Block {
  damage: Float! @percentage
}

Then I create my schema with

import percentageTransformer from "./directives/percentage";

const schema = makeAugmentedSchema({
  schemaTransforms: [
    percentageTransformer,
  ],

My directive is as follow:

import {
  DirectiveUseMap,
  MapperKind,
  getDirectives,
  mapSchema,
} from "graphql-tools";
import { GraphQLSchema, defaultFieldResolver } from "graphql";
import { ValidationError } from "apollo-server-express";

const percentageTransformer = (schema: GraphQLSchema): GraphQLSchema =>
  mapSchema(schema, {
    [MapperKind.OBJECT_FIELD]: (fieldConfig) => {
      let directives: DirectiveUseMap;
      try {
        directives = getDirectives(schema, fieldConfig);
      } catch {
        // GraphQLError [Object]: Argument "direction" has invalid value "BOTH".
        // Not sure why but I'm getting this error when getDirectives reads @relation
        return fieldConfig;
      }

      if (directives.percentage) {
        const { resolve = defaultFieldResolver } = fieldConfig;
        return {
          ...fieldConfig,
          resolve: async (source, args, context, info) => {
            const result = await resolve(source, args, context, info);
            if (result < 0 || result > 1) {
              throw new ValidationError("not_a_percentage");
            }
            return result;
          },
        };
      }
      return fieldConfig;
    },
  });

export default percentageTransformer;

Versions used:

  • neo4j-graphql-js 2.17.0
  • graphql 14.7.0

All 4 comments

@AdrienLemaire Someone mentioned in #565 you can remove the quotes from direction in the typeDefs string, which does seem to work. I'm defining my typeDefs via classes and decorators which then get parsed to build the string, so at the moment my workaround would be forking the parser and removing the quotes from only that specific argument, which feels a little extreme.

This was supposedly changed way back in 2.14.4 with c9b8b1af65ea751d060be79f8ab9d521577968de, yet I have a previous project on 2.18 that works fine with strings, and a current project on 2.18 that breaks with strings.

After playing around a bit more it seems our issue may be caused by having graphql-tools as a dependency in the project.

Direction strings started working fine for me again once I removed it on version 2.18.0.
Perhaps you can find a graphql-tools version that plays nice with your additional transformer and neo4j-graphql-js in the meantime.

Removing the quotes did the trick! Thanks @zi-nt !

Re-opening because the issue persists (I had forgotten to restart apollo server).
The error occurs on both 2.18.0 and 2.19.1
Failing with graphql-tools 6 and 7
Failing with graphql 14 and 15.

Was this page helpful?
0 / 5 - 0 ratings