graphqlquery

package module
v0.0.0-...-7e5bb57 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2022 License: MIT Imports: 7 Imported by: 3

README

graphqlquery

GoDoc

Package graphqlquery generates GraphQL queries from the result structs.

Examples

Build
package main

import (
    "fmt"

    "github.com/motemen/go-graphql-query"
)

// A struct simply generates GraphQL query generating the result
// suitable for the struct
type simpleExample struct {
    Hero struct {
        Name    string
        Friends []struct {
            Name string
        }
    }
}

type complexExample struct {
    Hero struct {
        Name string

        // Use embedding and "..." tag to build inline fragments query
        DroidFields `graphql:"... on Droid"`
        // or "..." for a field
        Height int `graphql:"... on Human"`

        Friends []struct {
            Name string
        }   `graphql:"@include(if: $withFriends)"` // Directives
    }   `graphql:"(episode: $ep)"` // Use "(..)" tag to specify arguments

    EmpireHero struct {
        // Arguments also can be specified by the special field GraphQLArguments
        GraphQLArguments struct {
            // Arguments in GraphQLArguments are automatically shown in the query arguments
            Episode Episode `graphql:"$ep"`
        }
        Name string
    }   `graphql:"alias=hero"` // use "alias=" tag to use alias

    // GraphQLArguments at toplevel stands for query arguments
    GraphQLArguments struct {
        // Should include arguments appeared in struct tags
        // Variables appeared in GraphQLArguments ($ep in EmpireHero) are
        // automatically shown in the query arguments
        WithFriends bool `graphql:"$withFriends,notnull"`
    }
}

type DroidFields struct {
    PrimaryFunction string
}

// Types starting with capital letter are treated as custom types
// TODO: more configurable way to assign Go types to GraphQL type names
type Episode string

func main() {
    s, _ := graphqlquery.Build(&simpleExample{})
    c, _ := graphqlquery.Build(&complexExample{})
    fmt.Println(string(s))
    fmt.Println(string(c))
}

Output:

query {
  hero {
    name
    friends {
      name
    }
  }
}
query($ep: Episode, $withFriends: Boolean!) {
  hero(episode: $ep) {
    name
    ... on Droid {
      primaryFunction
    }
    ... on Human {
      height
    }
    friends @include(if: $withFriends) {
      name
    }
  }
  empireHero: hero(episode: $ep) {
    name
  }
}

Author

motemen https://motemen.github.io/

Documentation

Overview

Package graphqlquery generates GraphQL queries from the result structs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(q interface{}, opts ...Option) ([]byte, error)

Build makes GraphQL query suitable for q, which is also a result JSON object for the GraphQL result JSON. See the example.

Example
package main

import (
	"fmt"

	"github.com/motemen/go-graphql-query"
)

// A struct simply generates GraphQL query generating the result
// suitable for the struct
type simpleExample struct {
	Hero struct {
		Name    string
		Friends []struct {
			Name string
		}
	}
}

type complexExample struct {
	Hero struct {
		Name string

		// Use embedding and "..." tag to build inline fragments query
		DroidFields `graphql:"... on Droid"`
		// or "..." for a field
		Height int `graphql:"... on Human"`

		Friends []struct {
			Name string
		} `graphql:"@include(if: $withFriends)"` // Directives
	} `graphql:"(episode: $ep)"` // Use "(..)" tag to specify arguments

	EmpireHero struct {
		// Arguments also can be specified by the special field GraphQLArguments
		GraphQLArguments struct {
			// Arguments in GraphQLArguments are automatically shown in the query arguments
			Episode Episode `graphql:"$ep"`
		}
		Name string
	} `graphql:"alias=hero"` // use "alias=" tag to use alias

	// GraphQLArguments at toplevel stands for query arguments
	GraphQLArguments struct {
		// Should include arguments appeared in struct tags
		// Variables appeared in GraphQLArguments ($ep in EmpireHero) are
		// automatically shown in the query arguments
		WithFriends bool `graphql:"$withFriends,notnull"`
	}
}

type DroidFields struct {
	PrimaryFunction string
}

// Types starting with capital letter are treated as custom types
// TODO: more configurable way to assign Go types to GraphQL type names
type Episode string

func main() {
	s, _ := graphqlquery.Build(&simpleExample{})
	c, _ := graphqlquery.Build(&complexExample{})
	fmt.Println(string(s))
	fmt.Println(string(c))
}
Output:

query {
  hero {
    name
    friends {
      name
    }
  }
}
query($ep: Episode, $withFriends: Boolean!) {
  hero(episode: $ep) {
    name
    ... on Droid {
      primaryFunction
    }
    ... on Human {
      height
    }
    friends @include(if: $withFriends) {
      name
    }
  }
  empireHero: hero(episode: $ep) {
    name
  }
}

func BuildMutation

func BuildMutation(q interface{}, opts ...Option) ([]byte, error)

func BuildQuery

func BuildQuery(q interface{}, opts ...Option) ([]byte, error)

Types

type Option

type Option func(*builder)
var OperationTypeMutation Option = func(b *builder) {
	b.operationType = "mutation"
}
var OperationTypeQuery Option = func(b *builder) {
	b.operationType = "query"
}

func OperationName

func OperationName(name string) Option

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL