github.com/shippio/gqlgen@v0.0.0-20220912092219-633ea699ef07/README.md (about)

     1  ![gqlgen](https://user-images.githubusercontent.com/980499/133180111-d064b38c-6eb9-444b-a60f-7005a6e68222.png)
     2  
     3  
     4  # gqlgen [![Integration](https://github.com/99designs/gqlgen/actions/workflows/integration.yml/badge.svg)](https://github.com/99designs/gqlgen/actions) [![Coverage Status](https://coveralls.io/repos/github/99designs/gqlgen/badge.svg?branch=master)](https://coveralls.io/github/99designs/gqlgen?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/99designs/gqlgen)](https://goreportcard.com/report/github.com/99designs/gqlgen) [![Go Reference](https://pkg.go.dev/badge/github.com/99designs/gqlgen.svg)](https://pkg.go.dev/github.com/99designs/gqlgen) [![Read the Docs](https://badgen.net/badge/docs/available/green)](http://gqlgen.com/)
     5  
     6  ## What is gqlgen?
     7  
     8  [gqlgen](https://github.com/99designs/gqlgen) is a Go library for building GraphQL servers without any fuss.<br/>
     9  
    10  - **gqlgen is based on a Schema first approach** — You get to Define your API using the GraphQL [Schema Definition Language](http://graphql.org/learn/schema/).
    11  - **gqlgen prioritizes Type safety** — You should never see `map[string]interface{}` here.
    12  - **gqlgen enables Codegen** — We generate the boring bits, so you can focus on building your app quickly.
    13  
    14  Still not convinced enough to use **gqlgen**? Compare **gqlgen** with other Go graphql [implementations](https://gqlgen.com/feature-comparison/)
    15  
    16  ## Quick start
    17  1. [Initialise a new go module](https://golang.org/doc/tutorial/create-module)
    18  
    19         mkdir example
    20         cd example
    21         go mod init example
    22  
    23  2. Add `github.com/99designs/gqlgen` to your [project's tools.go](https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module)
    24  
    25         printf '// +build tools\npackage tools\nimport _ "github.com/99designs/gqlgen"' | gofmt > tools.go
    26         go mod tidy
    27  
    28  3. Initialise gqlgen config and generate models
    29  
    30         go run github.com/99designs/gqlgen init
    31  
    32  4. Start the graphql server
    33  
    34         go run server.go
    35  
    36  More help to get started:
    37   - [Getting started tutorial](https://gqlgen.com/getting-started/) - a comprehensive guide to help you get started
    38   - [Real-world examples](https://github.com/99designs/gqlgen/tree/master/_examples) show how to create GraphQL applications
    39   - [Reference docs](https://pkg.go.dev/github.com/99designs/gqlgen) for the APIs
    40  
    41  ## Reporting Issues
    42  
    43  If you think you've found a bug, or something isn't behaving the way you think it should, please raise an [issue](https://github.com/99designs/gqlgen/issues) on GitHub.
    44  
    45  ## Contributing
    46  
    47  We welcome contributions, Read our [Contribution Guidelines](https://github.com/99designs/gqlgen/blob/master/CONTRIBUTING.md) to learn more about contributing to **gqlgen**
    48  ## Frequently asked questions
    49  
    50  ### How do I prevent fetching child objects that might not be used?
    51  
    52  When you have nested or recursive schema like this:
    53  
    54  ```graphql
    55  type User {
    56    id: ID!
    57    name: String!
    58    friends: [User!]!
    59  }
    60  ```
    61  
    62  You need to tell gqlgen that it should only fetch friends if the user requested it. There are two ways to do this;
    63  
    64  - #### Using Custom Models
    65  
    66  Write a custom model that omits the friends field:
    67  
    68  ```go
    69  type User struct {
    70    ID int
    71    Name string
    72  }
    73  ```
    74  
    75  And reference the model in `gqlgen.yml`:
    76  
    77  ```yaml
    78  # gqlgen.yml
    79  models:
    80    User:
    81      model: github.com/you/pkg/model.User # go import path to the User struct above
    82  ```
    83  
    84  - #### Using Explicit Resolvers
    85  
    86  If you want to Keep using the generated model, mark the field as requiring a resolver explicitly in `gqlgen.yml` like this:
    87  
    88  ```yaml
    89  # gqlgen.yml
    90  models:
    91    User:
    92      fields:
    93        friends:
    94          resolver: true # force a resolver to be generated
    95  ```
    96  
    97  After doing either of the above and running generate we will need to provide a resolver for friends:
    98  
    99  ```go
   100  func (r *userResolver) Friends(ctx context.Context, obj *User) ([]*User, error) {
   101    // select * from user where friendid = obj.ID
   102    return friends,  nil
   103  }
   104  ```
   105  
   106  You can also use inline config with directives to achieve the same result
   107  
   108  ```graphql
   109  directive @goModel(model: String, models: [String!]) on OBJECT
   110      | INPUT_OBJECT
   111      | SCALAR
   112      | ENUM
   113      | INTERFACE
   114      | UNION
   115  
   116  directive @goField(forceResolver: Boolean, name: String) on INPUT_FIELD_DEFINITION
   117      | FIELD_DEFINITION
   118  
   119  type User @goModel(model: "github.com/you/pkg/model.User") {
   120      id: ID!         @goField(name: "todoId")
   121      friends: [User!]!   @goField(forceResolver: true)
   122  }
   123  ```
   124  
   125  ### Can I change the type of the ID from type String to Type Int?
   126  
   127  Yes! You can by remapping it in config as seen below:
   128  
   129  ```yaml
   130  models:
   131    ID: # The GraphQL type ID is backed by
   132      model:
   133        - github.com/99designs/gqlgen/graphql.IntID # a go integer
   134        - github.com/99designs/gqlgen/graphql.ID # or a go string
   135  ```
   136  
   137  This means gqlgen will be able to automatically bind to strings or ints for models you have written yourself, but the
   138  first model in this list is used as the default type and it will always be used when:
   139  
   140  - Generating models based on schema
   141  - As arguments in resolvers
   142  
   143  There isn't any way around this, gqlgen has no way to know what you want in a given context.
   144  
   145  ### Why do my interfaces have getters? Can I disable these?
   146  These were added in v0.17.14 to allow accessing common interface fields without casting to a concrete type.
   147  However, certain fields, like Relay-style Connections, cannot be implemented with simple getters.
   148  
   149  If you'd prefer to not have getters generated in your interfaces, you can add the following in your `gqlgen.yml`:
   150  ```yaml
   151  # gqlgen.yml
   152  omit_getters: true
   153  ```
   154  
   155  ## Other Resources
   156  
   157  - [Christopher Biscardi @ Gophercon UK 2018](https://youtu.be/FdURVezcdcw)
   158  - [Introducing gqlgen: a GraphQL Server Generator for Go](https://99designs.com.au/blog/engineering/gqlgen-a-graphql-server-generator-for-go/)
   159  - [Dive into GraphQL by Iván Corrales Solera](https://medium.com/@ivan.corrales.solera/dive-into-graphql-9bfedf22e1a)
   160  - [Sample Project built on gqlgen with Postgres by Oleg Shalygin](https://github.com/oshalygin/gqlgen-pg-todo-example)
   161  - [Hackernews GraphQL Server with gqlgen by Shayegan Hooshyari](https://www.howtographql.com/graphql-go/0-introduction/)