getoutreach / goql

A GraphQL client package written in Go.
Apache License 2.0
19 stars 2 forks source link
client-library go golang golang-library golang-package graphql

goql

go.dev reference Generated via Bootstrap Coverage Status

A GraphQL client package written in Go.

Contributing

Please read the CONTRIBUTING.md document for guidelines on developing and contributing changes.

High-level Overview

goql is a GraphQL client library with built-in two-way marshaling support via struct tags. This is key because it allows for strongly typed GraphQL queries as opposed to variables containing a string representation of the query. This also facilitates more advanced features, such as sparse field sets.

For complete documentation see the generated pkg.go documentation. For a complete guide on the struct tag syntax, see the documentation found below under Defining GraphQL Operations.

Installation

In the root of your project repository (same directory as your go.mod and go.sum files):

go get github.com/getoutreach/goql

After that you should be able to import it anywhere within your project.

Defining GraphQL Operations

GraphQL operations can be defined by using normal Go struct types along with the help of struct tags. For example:

type QueryUserCollection struct {
    UserCollection struct {
        Collection []struct {
            ID   string
            Name string
        } `goql:"keep"`
    } `goql:"userCollection(filter:$filter<[Filter]>,sort:$sort<[String]>,size:$size<Int>,before:$before<String>,after:$after<String>)"`
}

when passed through the GraphQL query marshaller renders the following string:

query (
  $filter: [Filter]
  $sort: [String]
  $size: Int
  $before: String
  $after: String
) {
  userCollection(
    filter: $filter
    sort: $sort
    size: $size
    before: $before
    after: $after
  ) {
    collection {
      id
      name
    }
  }
}

Here's the high-level steps to go through when first defining a GraphQL operation:

  1. Create a struct that will act as a wrapper for the entire operation. The top-level model will be the only immediate child struct field of this wrapper struct (e.g. QueryUserCollection's only immediate child is UserCollection which together represents the query($filter: [Filter], ...) { userCollection(filter: $filter, ...) { ... } } part of the output).
  2. Define all of the fields and sub-models of the top-level model as struct fields within the top-level model (e.g. UserCollection contains children fields []Collection, ID, and Name). All types should match the types described in the schema of the query. - ID in GraphQL is a string in Go. - Any type with the non-null (!) restriction in GraphQL should be a non-pointer type in Go. Conversely, any type in GraphQL without this restriction should be nullable (a pointer type) in Go. - If the field is an integral part of the operation, e.g. UserCollection, and Collection fields in the struct above, add the goql:"keep" tag to them to tell the marshaler to always include these fields. This is necessary in order for sparse field sets to work. However, in the example above the keep tag can actually be omitted from the UserCollection part of the query as it already defines an operation declaration, which the marshaler already sees as an integral part of the operation and implicitly marks it to be kept (that is why the keep tag is left off of that portion, but on Collection still).
  3. Iterate through the fields and add goql struct tags to further define the structure of the operation by modifying declarations, adding aliases, variables, or directives to each field. See the immediately proceeding section, GraphQL Struct Tag Syntax, for more information on these struct tags and how to define them.

GraphQL Struct Tag Syntax

The following components can be used alone or together, separated by a comma within in the tag, to define a goql struct tag for a field or model on an operation:

Here is an example of using multiple struct tags together:

Name string `goql:"@alias(username),@include($withName)"` -> username: name @include(if: $withName)

Rules: