Categorygithub.com/dapperlabs/graphb
modulepackage
0.0.0-20220307084719-33665e7a8ea0
Repository: https://github.com/dapperlabs/graphb.git
Documentation: pkg.go.dev

# README

graphb: A GraphQL query builder

Focus on the query, not string manipulation

Motivation

As Go developers, when there is a GraphQL server, we often build our query string like this:

// Define this in some file x.go
const queryTemplate = `
    "query": "
        query an_operation_name { 
            a_field_name (
                an_argument_name_if_any: \"%s\"
            ) {
                a_sub_field,
                another_sub_field
            }
        }"`
    
// DO this in some other file y.go
func buildQuery(value string) string {
    return fmt.Sprintf(queryTemplate, value)
}

This approach is verbose, inflexible and error prone.

It is verbose and inflexible because every time you want to query a different structure, you need to rewrite another template and another string interpolation function. Not to mention when you have argument types such lists or enums or when you want to use fragments, directives and other syntax of GraphQL.

It is error prone because there is no way to check the correctness of your syntax until the query string is send to the server.

It also wastes extra spaces and looks not beautiful.

Therefore, when it comes to GraphQL client in Go, a developer spends much of her time to fight the string manipulation war (that's easy to lose), rather than focusing on the query (aka the business logic).

This library solves the string building problem so that you focus on business logic.

Example

All code are well documented. See example dir for more examples.

The lib provides 3 ways of constructing a query.

  1. Method Chaining
  2. Functional Options
  3. Struct Literal

1. Method Chaining

See example/three_ways_to_construct_query_test.go#L10-L43

2. Functional Options

See example/three_ways_to_construct_query_test.go#L45-L74

3. Struct Literal

See example/three_ways_to_construct_query_test.go#L76-L100

Words from the author

The library catches cycles. That is, if you have a Field whose sub Fields can reach the Field itself, the library reports an error.

I hesitate to make all fields private and only allow constructing a query through NewQuery and MakeQuery. I also don't know if MakeQuery is better than NewQuery or the contrary. Please use it and give feedback.

Error Handling

All graphb errors are wrapped by pkg/errors.
All error types are defined in error.go

Test

graphb uses testify/assert.

go test

Todos

The library does not currently support:

  1. Directive
  2. Variables
  3. Fragments

I do not know how useful would them be for a user of this library. Since the library builds the string for you, you sort of get the functionality of Variable and Fragment for free. You can just reuse a Field or the values of Fields and Arguments as normal Go code. Directive might be the most useful one for this library

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ArgumentCustomType returns a custom GraphQL type's argument representation, which could be a recursive structure.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ArgumentSlice returns a slice of arguments.
No description provided by the author
No description provided by the author
No description provided by the author
Fields takes a list of strings and make them a slice of *Field.
MakeField constructs a Field of given name and return the pointer to this Field.
MakeQuery constructs a Query of the given type and returns a pointer of it.
NewField uses functional options to construct a new Field and returns the pointer to it.
NewQuery uses functional options to construct a new Query and returns the pointer to it.
No description provided by the author
OfArguments returns a FieldOption which sets the arguments of the targeting field.
OfField returns a FieldContainerOption and has the same parameter signature of NewField(name string, options ...FieldOptionInterface) (*Field, error).
OfFields returns a FieldOption which sets a list of sub fields of given names of the targeting field.
OfName returns a QueryOption which validates and sets the operation name of a query.
StringFromChan builds a string from a channel, assuming the channel has been closed.

# Constants

3 types of operation.
3 types of operation.
3 types of operation.

# Structs

No description provided by the author
ArgumentTypeNotSupportedErr is returned when user tries to pass an unsupported type to ArgumentAny.
CyclicFieldErr is returned when any field contains a loop which goes back to itself.
Field is a recursive data struct which represents a GraphQL query field.
InvalidNameErr is returned when an invalid name is used.
InvalidOperationTypeErr is returned when the operation is not one of query, mutation and subscription.
NilFieldErr is returned when any field is nil.
Query represents a GraphQL query.

# Interfaces

FieldOptionInterface implements functional options for NewField().
QueryOptionInterface implements functional options for NewQuery().

# Type aliases

FieldContainerOption implements FieldOptionInterface and QueryOptionInterface, which means, it can be used as the functional option for both NewQuery() and NewField().
FieldOption implements FieldOptionInterface.
QueryOption implements QueryOptionInterface.