Add relational fields to create mutations?

Hey guys, playing with Grandstack and loving it. Bit new to GraphQL and neo4j, so sorry if this is a stupid question, but would appreciate some help. I have a few object types in my schema with relationships. I can create them just fine and link them later too via the API. So far, so good What I'd like to do now is pass in the ID of an existing object to the relational field of of a related object when creating that related object, but GrandStack doesn't appear to create relational fields on the create mutations. Is there a way to override that globally or could someone point me to any guidance on how to do this for a given create method? Thanks!

I'm not sure I understand what you mean here by relational fields.

Can you post a snippet of your typedefs as an example, along with what kind of query you want to do that isn't created?

Hi David,

Thanks for the quick response. Sorry if I'm using the wrong terminology. Still learning the Neo4j ecosystem and nomenclature.

Here's the schema of the Security object type I want to access via GraphQl:

type Security {
  id: ID!
  class: String
  series: String
  authorized: Int
  authorized_date: Date
  notes: String
  company: Company @ relation(name: "ISSUED_BY", direction:"OUT")
  user: User @relation(name: "CREATED", direction: "IN")
}

I want to create a new Security and pass in the id of the Company the security was "ISSUED_BY" as an argument when creating a new Security. The autogenerated mutations from Grandstack don't appear to let me do this.

I try to pass in this query:

mutation {
  CreateSecurity(authorized:100000
    series:"Series A"
    class:"Preferred"
    company:{id:"e48b2eb6-48e9-4e38-85e2-4ad198483568"}) {
    id
  }
}

And GraphQl throws an error that ""Unknown argument "company" on field "CreateSecurity" of type "Mutation".""

When I look at the schema provided at my API URL, I see there is no "company" field in the CreateSecurity mutation:

image

I'm sure I am simply missing something obvious, but I feel like that shouldn't be the case? At the very least, is there a way to quickly override this behavior? Like I said, I've bypassed the issue by writing my own query using Cypher that works, but ideally this is something I can get out of the box without having to write custom queries and resolvers? That was a big part of the appeal of Grandstack for me (which is awesome, btw).

I understand. Yes when you define your security type, it does not automatically generate mutators for other items. In order to do this, you have two options.

First, you could add a relationship type as described on this page, and then do your mutation in two steps - one to create JUST a security node, and then another to create the relationship from the security node to the company:

(See the section on relationship types)

The other option is that you can write your own custom mutation (like what you've specified here with CreateSecurityWithCompany) and you can then implement the "resolver" function in javascript to back that. See these docs:

In your JS when you get to the "makeAugmentedSchema" step you'd have to have a resolver function to go with that.

What you're asking for can't be automatically generated for a lot of reasons:

  • When you specify the company ID in CreateSecurity, it isn't clear if you intend this to be MERGE'd or MATCH'd in Neo4j
  • It isn't clear whether there might be more than one relationship type, or that the other end of the relation would be a Company.
1 Like

Never thanked you for this. Much appreciated, David.