热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Howtopartiallyresolveastruct?

SayIhaveatypeandquerylike:

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

1
a

is fetched from DB,

1
b

is fetched from a service, and

1
c

is fetched from another service, so I would not like to fetch

1
c

when a client only asks for

1
a

and

1
b

. I'd like to know what's the best practice to handle it?

该提问来源于开源项目:99designs/gqlgen

The




1
satisfies

argument is used when getting collected fields whose query include fragments. They will only collect fields from fragments who satisfy a type passed through

1
satisfies []string

. If you're not using fragments in your query, then passing




1
nil

is fine. We are going to improve this interface before

1
1.0

.

One way you could "force"

1
gqlgen

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

1
gqlgen

cannot resolve the field names to a field on your struct, it will generate a resolver interface for you to fulfil.


   



推荐阅读
author-avatar
暮迟_MCz_P
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有