99designs / 99designs/gqlgen

Auto-fill missing mandatory field OR Give error when missing mandatory field

Open
#3,912 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
10.8k
Forks
1.3k
Avg merge
2d 36m
Merged PRs (30d)
26

Description

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 allow
```

I 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:

```go
--- 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

1. Auto-fix it by supplying a non-null zero value pointer for these fields if missing
2. 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 `sweetener` field from the frontend).
3. Same as option 2 but make it opt-in with a config flag.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.