0
const resolver= {
    Query: {
        EventsQuery: async () => {
            return await db.select('*').from('events')
        },
    },
    Mutation: {
        AddEventMutation: async (_: any, input) => {
        return await db('events').returning('event_name').insert([
                {
                    event_name: input.event_name,
                    event_venue: input.event_venue
                }
            ]).then((name) => {
                console.log(name)   // value is displayed 
                return name
            })
        },  
    },
}

I have a GraphQL server that connects to the PostgreSQL DB and I am performing an INSERT SQL to add item in to the db. I am using Knex.js query builder and the above query inserts data into the table and returns event_name. Data was inserted into the db but in GraphiQL I am getting this response. I think it should return a value instead of null.

{
  "data": {
    "AddEventMutation": {
      "event_name": null
    }
  }
}

This is how I added the data into the db from GraphiQL.

mutation{
  AddEventMutation(event_name: "Mutation", event_date: "1002") {
    event_name
  } 
}

Can anyone tell me what went wrong? If getting the null value is correct, how does the client know if the mutation operation is a success or not? I am still experimenting and confused.

calvert
  • 631
  • 10
  • 33
  • 1
    `AddEventMutation`'s type is an object type with an `event_name` field. So the resolver should return an object with an `event_name` property. Instead you're returning a string, which *is* an object but does not have an `event_name` property. – Daniel Rearden May 10 '20 at 10:17
  • I don't believe this is a duplicate, those questions are not using postgresql and whatever result we get is different. But anyway, based on your comment here I did some other way and got it to work. Thanks – calvert May 10 '20 at 16:56

0 Answers0