gql-client-gen

command
v0.0.0-...-3478735 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 18 Imported by: 0

README

gql-client-gen

This is a CLI tool that can be used to generate types for use by client code. If you wrap your queries in a "gql()" function call, the tool will validate your queries and generate types for it. For example, running the tool on this file:

package main

// Wrapper to mark queries for gql-client-gen.
func gql(s string) string {
	return s
}

func main() {
	println(gql(`query FindIssueID {
	  repository(owner:"octocat", name:"Hello-World") {
		issue(number:349) {
		  id
		}
	  }
	}`))
}

Will generate output like this:

package main

type FindIssueIDData struct {
	Repository *struct {
		Issue *struct {
			Id string
		}
	}
}

It will generate types for all named queries and mutations as well as all named fragments.

Inline Fragments and Fragment Spreads

Types can also be generated for queries that involve fragments with type conditions. In these cases, your queries must select __typename so the generated types can know which spreads to unmarshal. For example:

println(gql(`query User {
  node(id:"MDQ6VXNlcjU4MzIzMQ==") {
   __typename
   ... on User {
      name
      login
    }
  }
}`))

Will generate something like...

type selNode0 struct {
	Typename__ string `json:"__typename"`
	User       *struct {
		Name  *string
		Login string
	} `json:"-"`
}

func (s *selNode0) UnmarshalJSON(b []byte) error {
	var base struct {
		Typename__ string `json:"__typename"`
		User       *struct {
			Name  *string
			Login string
		} `json:"-"`
	}
	if err := json.Unmarshal(b, &base); err != nil {
		return err
	}
	*s = base
	switch base.Typename__ {
	case "User":
		if err := json.Unmarshal(b, &s.User); err != nil {
			return err
		}
	}
	return nil
}

type UserData struct {
	Node *selNode0
}

After unmarshaling, UserData.Node.User will be nil or non-nil depending on the type of the node returned.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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