reactjsnode.jsgraphqlreact-apolloapollo-server

GraphQL query giving error, but it still updates my database


I'm making a project in React, using apollo-server-express, graphQL and postgreSQL, I've done the type definitions and some queries and mutations, but there is 3 in specific that isn't working correctly.

The three of them are giving the same error, so I'll show only one mutation. In my postgreSQL I created this table:

CREATE TABLE IF NOT EXISTS tb_evaluator(
    id SERIAL NOT NULL,
    name VARCHAR(255) NOT NULL,
    profession VARCHAR(255) NOT NULL,
    performance_exp VARCHAR(20) NOT NULL,
    app_exp VARCHAR(20) NOT NULL,
    ag_exp VARCHAR(20) NOT NULL,
    evaluation_date VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
);

Then in my project I created a js file with the type definitions, queries and mutations.

const { gql } = require('apollo-server-express')

const typedefs = gql`
type Evaluator {
  id: ID!
  name: String!
  profession: String!
  performanceExp: String!
  appExp: String!
  agExp: String!
  evaluationDate: String!
}

type Mutation {
  addEvaluator(input: AddEvaluatorInput!): Evaluator!
}

input AddEvaluatorInput {
  name: String!
  profession: String!
  performanceExp: String!
  appExp: String!
  agExp: String!
  evaluationDate: String!
}
`;

module.exports = typedefs;

In other file I created the resolvers, for this case, the resolver is:

const { Pool } = require('pg');
const pool = new Pool({...});

const resolvers = {
  Query: {...}
  Mutation: {
    ...
    addEvaluator: async (_, { input }) => {
    const { name, profession, performanceExp, appExp, agExp, evaluationDate } = input;
    const values = [name, profession, performanceExp, appExp, agExp, evaluationDate];
    const query = 'INSERT INTO tb_evaluator (name, profession, performance_exp, app_exp, ag_exp, evaluation_date) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *';

    try {
      const result = await pool.query(query,values);
      return result.rows[0];
    } catch (error) {
      console.error('Error adding evaluation:', error);
      throw new Error('Failed to add evaluation');
    }
  }
}};

When I launch the server I go to the apollo server sandbox and I do this operation:

mutation Mutation($input: AddEvaluatorInput!) {
  addEvaluator(input: $input) {
    id
    name
    profession
    performanceExp
    appExp
    agExp
    evaluationDate
  }
}

And in the variables tab I set these values:

{
  "input": {
    "agExp": "a",
    "appExp": "b",
    "evaluationDate": "c",
    "name": "d",
    "performanceExp": "e",
    "profession": "f"
  }
}

But when I run it I get this error

"errors": [
   {
     "message": "Cannot return null for non-nullable field Evaluator.performanceExp.",
     "locations": [
       {
         "line": 6,
         "column": 5
       }
     ],
     "path": [
       "addEvaluator",
       "performanceExp"
     ],

But, besides the error, the data still is inserted in my pg database, so I don't know if I just ignore this error, or if I'm doing something wrong, because I have others mutations that is VERY similar, and the only difference is the amount of parameters. Btw, when I these variables: performanceExp, appExp,agExp and evaluationDate, or when I statically set its values in the code it runs with no error,


Solution

  • At the end of your mutation you query your database for the record you just inserted with:

    const result = await pool.query(query,values);
    return result.rows[0];
    

    However you fail to convert the snake_case column names back to camelCase - as a result your performanceExp, appExp, and agExp fields will all be null because the GraphQL server will discard performance_exp etc… from the result record before creating the JSON dataset to send back to the client.