•  


GitHub - 99designs/gqlgen: go generate based graphql server library
Skip to content

99designs/gqlgen

Repository files navigation

gqlgen

gqlgen Integration Coverage Status Go Report Card Go Reference Read the Docs

What is gqlgen?

gqlgen is a Go library for building GraphQL servers without any fuss.

  • gqlgen is based on a Schema first approach ? You get to Define your API using the GraphQL Schema Definition Language .
  • gqlgen prioritizes Type safety ? You should never see map[string]interface{} here.
  • gqlgen enables Codegen ? We generate the boring bits, so you can focus on building your app quickly.

Still not convinced enough to use gqlgen ? Compare gqlgen with other Go graphql implementations

Quick start

  1. Initialise a new go module

    mkdir example
    cd example
    go mod init example
    
  2. Add github.com/99designs/gqlgen to your project's tools.go

    printf '//go:build tools\npackage tools\nimport (_ "github.com/99designs/gqlgen"\n _ "github.com/99designs/gqlgen/graphql/introspection")' | gofmt > tools.go
    
    go mod tidy
    
  3. Initialise gqlgen config and generate models

    go run github.com/99designs/gqlgen init
    
    go mod tidy
    
  4. Start the graphql server

    go run server.go
    

More help to get started:

Reporting Issues

If you think you've found a bug, or something isn't behaving the way you think it should, please raise an issue on GitHub.

Contributing

We welcome contributions, Read our Contribution Guidelines to learn more about contributing to gqlgen

Frequently asked questions

How do I prevent fetching child objects that might not be used?

When you have nested or recursive schema like this:

type
 User
 {
  
id
: 
ID
!

  name
: 
String
!

  friends
: [
User
!
]
!

}

You need to tell gqlgen that it should only fetch friends if the user requested it. There are two ways to do this;

  • Using Custom Models

Write a custom model that omits the friends field:

type
 User
 struct
 {
  
ID
 int

  Name
 string

}

And reference the model in gqlgen.yml :

#
 gqlgen.yml

models
:
  
User
:
    
model
: 
github.com/you/pkg/model.User 
#
 go import path to the User struct above
  • Using Explicit Resolvers

If you want to keep using the generated model, mark the field as requiring a resolver explicitly in gqlgen.yml like this:

#
 gqlgen.yml

models
:
  
User
:
    
fields
:
      
friends
:
        
resolver
: 
true 
#
 force a resolver to be generated

After doing either of the above and running generate we will need to provide a resolver for friends:

func
 (
r
 *
userResolver
) 
Friends
(
ctx
 context.
Context
, 
obj
 *
User
) ([]
*
User
, 
error
) {
  
// select * from user where friendid = obj.ID

  return
 friends
,  
nil

}

You can also use inline config with directives to achieve the same result

directive
 @goModel
(
model
: 
String
, 
models
: [
String
!
]) 
on
 OBJECT

    | 
INPUT_OBJECT

    | 
SCALAR

    | 
ENUM

    | 
INTERFACE

    | 
UNION


directive
 @goField
(
forceResolver
: 
Boolean
, 
name
: 
String
, 
omittable
: 
Boolean
) 
on
 INPUT_FIELD_DEFINITION

    | 
FIELD_DEFINITION


type
 User
 @goModel
(
model
: 
"
github.com/you/pkg/model.User
"
) {
    
id
: 
ID
!
         @goField
(
name
: 
"
todoId
"
)
    
friends
: [
User
!
]
!
   @goField
(
forceResolver
: 
true
)
}

Can I change the type of the ID from type String to Type Int?

Yes! You can by remapping it in config as seen below:

models
:
  
ID
: 
#
 The GraphQL type ID is backed by

    model
:
      - 
github.com/99designs/gqlgen/graphql.IntID 
#
 a go integer

      - 
github.com/99designs/gqlgen/graphql.ID 
#
 or a go string

      - 
github.com/99designs/gqlgen/graphql.UintID 
#
 or a go uint

This means gqlgen will be able to automatically bind to strings or ints for models you have written yourself, but the first model in this list is used as the default type and it will always be used when:

  • Generating models based on schema
  • As arguments in resolvers

There isn't any way around this, gqlgen has no way to know what you want in a given context.

Why do my interfaces have getters? Can I disable these?

These were added in v0.17.14 to allow accessing common interface fields without casting to a concrete type. However, certain fields, like Relay-style Connections, cannot be implemented with simple getters.

If you'd prefer to not have getters generated in your interfaces, you can add the following in your gqlgen.yml :

#
 gqlgen.yml

omit_getters
: 
true

Other Resources

- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본