Auto-fill missing mandatory field OR Give error when missing mandatory field
Author: HaraldNordgrenCreated Nov 13, 2025Updated Sep 23, 2026
I'm running into an error after creating a stub of a new query, where most of the data is missing.
ApolloError: the requested element is null which the schema does not allowI traced the root of the error and the problem is that a field that is mandatory in my GraphQL spec is missing from the backend resolver, but gqlgen doesn't let me know about this. To solve it I had to do this:
--- a/services/hava-api/pkg/resolver/foodtracker.resolvers.go
+++ b/services/hava-api/pkg/resolver/foodtracker.resolvers.go
@@ -802,8 +802,11 @@ func (r *queryResolver) ListFavoriteFoodItems(ctx context.Context, req model.Lis
CreateTime: time.Now(),
UpdateTime: time.Now(),
FoodItem: &model.Ingredient{
- Name: "ingredients/4425770c-8f5e-4573-976d-f4b3417ca46f",
- DisplayName: pointer.Pointer("Test Food Item 1"),
+ Name: "ingredients/4425770c-8f5e-4573-976d-f4b3417ca46f",
+ DisplayName: pointer.Pointer("Test Food Item 1"),
+ Satiety: &model.SatietyData{},
+ Sweetener: &model.SweetenerData{},
+ IngredientProperties: &model.IngredientProperties{},
},
},
},gqlgen should either
- Auto-fix it by supplying a non-null zero value pointer for these fields if missing
- Give an error when running the backend endpoint (currently the API works fine as long as you don't try to access e.g. the
sweetenerfield from the frontend). - Same as option 2 but make it opt-in with a config flag.
Source: 99designs/gqlgen