neo4j-graphql / neo4j-graphql-js

NOTE: This project is no longer actively maintained. Please consider using the official Neo4j GraphQL Library (linked in README).
Other
609 stars 147 forks source link

How to set auto-generated fields on custom mutations? #493

Open ian opened 4 years ago

ian commented 4 years ago

It seems that the params passed to the neo4jgraphql call are hard locked to only the params that are specified the schema mutation.

What is the preferred way to add additional create params in an overridden mutation?

Consider this example:

// schema.graphql 

type Thing {
  name: String!
  slug: String
  user: User
}

type Mutation {
  CreateThing(name: String!): Thing @isAuthenticated
}

// resolvers.ts

import slug from "slug"
import { neo4jgraphql } from "neo4j-graphql-js"

export default {
  Mutation: {
    CreateThing(object, params, ctx, resolveInfo) {
      Object.assign(params, {
        slug: slug(params.name),
        user: ctx.user.id,
      })

      return neo4jgraphql(object, params, ctx, resolveInfo)
    },
  },
}

// Here's what's generated

CREATE (`thing`:`Thing` {name:$params.name})
RETURN `thing` { .name , .slug } AS `thing`
ltwlf commented 4 years ago

I have the same issue. Custom resolvers in neo4j-graphql-js have very limited use.

wworrall commented 4 years ago

I agree, greater flexibility with custom mutations is needed. This issue I raised is somewhat related: 505.

Ultimately we agree it would be great to have low level functionality which can generate cypher components - node update params and selections for use in custom cypher statements would be a good starting point

wworrall commented 4 years ago

Since I have updated issue #505, I thought I would check in here with a potential solution for you @ian.

Whilst this requires writing and running your own cypher statement, I think you can achieve what you need to. Your code could include something like this:

const CreateThing = async (object, params, ctx, resolveInfo) => {
    const { driver } = ctx;
    const session = driver.session();

    const selectionCypher = generateSelectionCypher(ctx, resolveInfo); // see #505 

    Object.assign(params, {
        slug: slug(params.name),
        user: ctx.user.id,
      });

    const statement = `
CREATE (thing:Thing $params)
RETURN ${selectionCypher}
    `;

    const respData = await session
      .run(statement, params)
      .then((res) => {
        console.log(res);
        return res;
      })
      .then((res) => res.records[0].get("thing"));

    await session.close();

    return respData;
}
michaeldgraham commented 3 years ago

https://github.com/neo4j-graphql/neo4j-graphql-js/issues/608