Home > Back-end >  Apollo Graphql Client - How to attach new node to existing node
Apollo Graphql Client - How to attach new node to existing node

Time:08-05

am trying to persist a new node in graphql using Apollo client. This new node should attach to an existing node. Not quite sure how to go about it. Suppose I have these type definitions:

type Person{
   personId: ID!
   name: String!
   user: User @relationship(type: "LOGS_IN_AS", direction: OUT)
}

type User{
   userId: ID!
   username: String!
   password: String!
   person: Person!
}

Now assuming I already have a person in the database as follows:

{
   personId: 1,
   name: "John Doe"
}

How do I mutate a corresponding User node for this Person and ensure the necessary relationship is created using Apollo's auto generated mutations? Am using a neo4j backend by the way.

Thanks in advance

CodePudding user response:

Don't know how you set up neo4j database but supposedly User should have a personId to link with Person.

You should probably defined a seperate schema type called UpsertUserInput or something like below for the mutation so neo4j can use it to link with the person

type UpsertUserInput{
   userId: ID!
   personId: ID!
   username: String!
   password: String!
}

the Appollo schema is not responsible for defining where data comes from or how it's stored. It is entirely implementation-agnostic. https://www.apollographql.com/docs/apollo-server/schema/schema/#the-schema-definition-language

CodePudding user response:

I see there is a solution, just needed to look a little deeper into the auto-generated mutations in the studio. Solution along the lines of...

useMutation(CREATE_USER, { 
    variables:{ 
        input:{
            userName: "[email protected]", 
            password: "jdoespassword", 
            person:{ 
                connect:{ 
                    where:{ 
                        node:{ 
                            personId: 1 
                        } 
                     } 
                } 
            } 
        }
     } 
  }) 
  • Related