99designs / 99designs/gqlgen

Resolver chaining With gqlgen, mongoDB, and Apollo

Open
#2,695 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

### What happened?
I'm unable to figure out how to chain resolvers with mongoDB.
### What did you expect?
I am expecting to be able to chain resolvers for queries with mongoDB ID's and Apollo's federation. In the Mongo data base `Application` has a resources field is an array of [ObjectID's] to `Resources` Documents. but I run into an issue when trying to resolve the application document.
```
error decoding key resources.0: cannot decode objectID into a model.Resource
```

I've tried creating a explicit resolver using
```
Application:
fields:
resources:
resolver: true
```

However this creates a separate applicationResolver upon generation that's never called when trying to do my queryResolver for Application. I feel like there should be some clean way to have this work. IE Get the Application, using the Application's resource field (a list of Resource ObjectIDs) get an array of resources. return a application with a resource struct and not a ID.


### Minimal graphql.schema and models to reproduce
schema
```
type Application @key(fields: "_id") {
_id: ID!
text: String!
resources: [Resource!]!
}

type Resource @key(fields: "_id") {
_id: ID!
text: String!
link: String!
}

type Query {
applications: [Application!]!
}
```

models generated by gqlgen (I'll ad a `bson:` field tag as well.)
```
type Application struct {
ID string `json:"_id"`
Text string `json:"text"`
Resources []*Resource `json:"resources"`
}

type Resource struct {
ID string `json:"_id"`
Text string `json:"text"`
Link string `json:"link"`
}
```
### versions
- v0.17.33
- go version go1.20.1 darwin/amd64

Contributor guide

Open the contributing guide

Research direction

The issue involves gqlgen's resolver chaining with MongoDB ObjectIDs and Apollo Federation. Look at the generated resolver code and the gqlgen configuration file (gqlgen.yml). Examine how the Application type's resources field is resolved, and check the MongoDB model structs for proper bson tags. The error suggests a decoding mismatch between ObjectID and the Resource model. Start by reviewing the resolver methods for Application and Resource, and see how the data loader or manual resolver can fetch the linked resources.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, graphql, mongodb
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.