Neo4j-graphql-js: Feature suggestion: Add `singleItem: boolean` argument to `@cypher` directive

Created on 2 Apr 2020  路  5Comments  路  Source: neo4j-graphql/neo4j-graphql-js

Right now the @cypher schema directive takes a statement: string argument (which must be a valid cypher query string).

Unfortunately it returns a list (array), irrespective of whether there is a single matching record, or if you add LIMIT 1 to the statement cypher query string.

As a simple way of making it explicit to the library, I suggest adding an additional singleItem: boolean argument to the @cypher directive, so that an individual record or value can be returned.

All 5 comments

Hi @benjamin-rood,

Can you please share one example with its schema that leads to a list return ?

_I ask that because for me single return with Limit 1 seems to work._

@emregency When you are returning a type (node) rather than a scalar (property)
i.e. You cannot use an @relation directive (because there isn't an edge directly connecting the two nodes) and instead it involves multiple 'hops', so you have to use an @cypher statement instead.

@emregency See #354

Actually I have just written a hack/workaround for this:

# Use this directive to force return the first item from an array/list (i.e. ensure return a single record/scalar, not a list)
directive @firstOnly on FIELD_DEFINITION
import { defaultFieldResolver } from 'graphql';
import { SchemaDirectiveVisitor } from "graphql-tools";

class FirstOnlyDirective extends SchemaDirectiveVisitor {
  visitFieldDefinition(field: any) {
    const { resolve = defaultFieldResolver } = field;
    field.resolve = async function(...args) {
      const result = await resolve.apply(this, args);
      if (Array.isArray(result)) {
        return result[0];
      }
      return result;
    };
  }
}

export const schemaDirectives = {
  firstOnly: FirstOnlyDirective
};

Now it is more clear. Thanks @benjamin-rood

Sorry for the annoyance 馃榿

Was this page helpful?
0 / 5 - 0 ratings

Related issues

imkleats picture imkleats  路  5Comments

robmurtagh picture robmurtagh  路  3Comments

benpetsch picture benpetsch  路  3Comments

johnymontana picture johnymontana  路  3Comments

purplemana picture purplemana  路  3Comments