Home > other >  Spring GraphQL field type was declared as non-null Error
Spring GraphQL field type was declared as non-null Error

Time:09-20

I recently started using Spring GraphQL to serve the GraphQL request incoming to my server (as opposed to the official GraphQL library).

After this transition, I'm now receiving the following error on SOME requests:

{
  "errors": [
    {
      "message": "The field at path '/projectOverview/techDebtOverTime' was declared as a non null type, but the code involved in retrieving data has wrongly returned a null value.
    The graphql specification requires that the parent field be set to null, or if that is non nullable that it bubble up null to its parent and so on. 
    The non-nullable type is '[SeriesPoint!]' within parent type 'OverviewMetrics'",
      "path": [
        "projectOverview",
        "techDebtOverTime"
      ],
      "extensions": {
        "classification": "NullValueInNonNullableField"
      }
    }
  ],
  "data": null
}

According to the error message, some of the fields returned by the query are null, but they are not allowed to be according to the schema. Yet, using the debugger I can check that none of the fields are null and that the data is fetched correctly from the repository by OverviewMetrics.

It seems to me that the underlying problem is Spring GraphQL that somehow does not invoke the methods on OverviewMetrics to retrieve the necessary data. For some reason, this error does not pop-up for all queries, only for some of them, and I cannot discern the common denominator between them.

I leave here the relevant part of the GraphQL schema:

type Query{
   projectOverview(projectName: String!, versionId: String): OverviewMetrics!
}

type OverviewMetrics {
    techDebtOverTime: [SeriesPoint!]!
}

type SeriesPoint {
    version: Version!
    group: String!
    seriesName: String!
    value: Float!
}

type Version{
    versionId: String!
    versionIndex: Int!
    versionDate: String!
}

The controller:

@Controller
public class QueryController {
   ...
    @QueryMapping
    public OverviewMetrics projectOverview(@Argument String projectName, @Argument String versionId){
        logger.info("Running `projectOverview(project, version)` query.");
        Project p = ...
        Version version = ...
        OverviewMetrics overview = new OverviewMetrics(...);
        // the `overview` variable is correctly initialized
        // invoking the techDebtOverTime() method here in the debugger produces the expected result.
        // it's like the GraphQL engine does not invoke the method and defaults to NULL!! 
        return overview;
    }
}

public class OverviewMetrics {
    // This method does not return null. All elements in the list do not have null fields.
    public List<SeriesPoint> techDebtOverTime(){...}
}

Does anyone has any idea how to fix this error? Thank you!

CodePudding user response:

@MD9's comment to my question contains the solution to the problem. It seems that Spring GraphQL requires that you prefix fields/methods with the get prefix. This was not the case with the raw graphql-java library, thus the problem arised.

  • Related