作者:暮迟_MCz_P | 来源:互联网 | 2023-06-26 08:37
Say I have a type and query like:
1 2 3 4 5 6 7 8 9 10
| graphql
type Example {
a: String!
b: String!
c: String!
}
type Query{
example(q: String!): Example
} |
gqlgen would generate a resolver interface like
1 2 3 4
| go
type QueryResolver interface {
Example(ctx context.Context, q string) (*graphql_model.Example, error)
} |
The problem is, maybe
is fetched from DB,
is fetched from a service, and
is fetched from another service, so I would not like to fetch
when a client only asks for
and
. I'd like to know what's the best practice to handle it?
该提问来源于开源项目:99designs/gqlgen
The
argument is used when getting collected fields whose query include fragments. They will only collect fields from fragments who satisfy a type passed through
. If you're not using fragments in your query, then passing
is fine. We are going to improve this interface before
.
One way you could "force"
to give you resolvers for all fields of a model, is to bind the type to an empty struct:
1 2 3 4 5
| graphql
type Animal {
name: String!
zoo: String!
} |
1 2
| go
type Animal struct {} |
1 2 3 4
| yml
models:
Animal:
model: "package.Animal" |
Since
cannot resolve the field names to a field on your struct, it will generate a resolver interface for you to fulfil.