gql

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Args = graphql.FieldConfigArgument{
	"limit": &graphql.ArgumentConfig{
		Type:        graphql.Int,
		Description: "Number of comics to return in results",
	},
	"offset": &graphql.ArgumentConfig{
		Type:        graphql.Int,
		Description: "Number of comics to skip in results",
	},
}

Args : Pagination Arguments

View Source
var ComicInfoType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "comic",
		Fields: graphql.Fields{
			"about": &graphql.Field{
				Type:        graphql.String,
				Description: "About content of comic",
			},
			"name": &graphql.Field{
				Type:        graphql.String,
				Description: "Name of comic",
			},
			"logo": &graphql.Field{
				Type:        graphql.String,
				Description: "Logo of comic",
			},
			"link": &graphql.Field{
				Type:        graphql.String,
				Description: "Link of comic website",
			},
		},
	},
)

ComicInfoType : GraphQL type for basic information of comic

View Source
var PHDComicQueryResolver = func(p graphql.ResolveParams) (interface{}, error) {
	limit, ok := p.Args["limit"].(int)
	if !ok || limit > utils.Limit || limit < 1 {
		limit = utils.Limit
	}
	offset, ok := p.Args["offset"].(int)
	if !ok || offset < 1 {
		offset = 0
	}

	semaphoreChan := make(chan struct{}, limit)
	resultsChan := make(chan *model.PHDComic)
	defer func() {
		close(semaphoreChan)
		close(resultsChan)
	}()

	for num := offset + 1; num <= offset+limit; num++ {
		go func(num int) {
			semaphoreChan <- struct{}{}
			comic, _ := fetchPHDComic(num)
			resultsChan <- comic
			<-semaphoreChan
		}(num)
	}

	// create slice for comics from phd comic page responses
	var comics []*model.PHDComic
	for {
		comic := <-resultsChan
		comics = append(comics, comic)
		if len(comics) == limit {
			break
		}
	}

	sort.Slice(comics, func(i, j int) bool {
		return comics[i].ComicID < comics[j].ComicID
	})

	return &comics, nil
}

PHDComicQueryResolver : Resolver for query { phdcomic }

View Source
var PHDComicType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "phdcomic",
		Fields: graphql.Fields{
			"comicid": &graphql.Field{
				Type:        graphql.Int,
				Description: "ID for PHD comic",
			},
			"date": &graphql.Field{
				Type:        graphql.String,
				Description: "Date on which comic was released",
			},
			"link": &graphql.Field{
				Type:        graphql.String,
				Description: "Permalink of the comic",
			},
			"title": &graphql.Field{
				Type:        graphql.String,
				Description: "Title of the comic",
			},
			"image": &graphql.Field{
				Type:        graphql.String,
				Description: "Image URL of the comic",
			},
		},
	},
)

PHDComicType : GraphQL type for phd comic

View Source
var VersionQueryResolver = func(p graphql.ResolveParams) (interface{}, error) {
	return &model.Version{
		Timestamp: time.Now(),
		Name:      utils.APIName,
		Version:   utils.APIVersion,
		Comics:    utils.ComicsSupported,
	}, nil
}

VersionQueryResolver : Resolver for query { version }

View Source
var VersionType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "version",
		Fields: graphql.Fields{
			"timestamp": &graphql.Field{
				Type:        graphql.DateTime,
				Description: "Current timestamp for version",
			},
			"name": &graphql.Field{
				Type:        graphql.String,
				Description: "GraphQL Service Name",
			},
			"version": &graphql.Field{
				Type:        graphql.String,
				Description: "GraphQL Service Version",
			},
			"comics": &graphql.Field{
				Type:        graphql.NewList(ComicInfoType),
				Description: "List of comics supported by current version",
			},
		},
	},
)

VersionType : GraphQL type for version

View Source
var XKCDQueryResolver = func(p graphql.ResolveParams) (interface{}, error) {
	limit, ok := p.Args["limit"].(int)
	if !ok || limit > utils.Limit || limit < 1 {
		limit = utils.Limit
	}
	offset, ok := p.Args["offset"].(int)
	if !ok || offset < 1 {
		offset = 0
	}

	semaphoreChan := make(chan struct{}, limit)
	resultsChan := make(chan *model.XKCD)
	defer func() {
		close(semaphoreChan)
		close(resultsChan)
	}()

	for num := offset + 1; num <= offset+limit; num++ {
		go func(num int) {
			semaphoreChan <- struct{}{}
			comic, _ := fetchXKCDComic(num)
			resultsChan <- comic
			<-semaphoreChan
		}(num)
	}

	// create slice for comics from xkcd API responses
	var comics []*model.XKCD
	for {
		comic := <-resultsChan
		comics = append(comics, comic)
		if len(comics) == limit {
			break
		}
	}

	sort.Slice(comics, func(i, j int) bool {
		return comics[i].Num < comics[j].Num
	})

	return &comics, nil
}

XKCDQueryResolver : Resolver for query { xkcd }

View Source
var XKCDType = graphql.NewObject(
	graphql.ObjectConfig{
		Name: "xkcd",
		Fields: graphql.Fields{
			"num": &graphql.Field{
				Type:        graphql.Int,
				Description: "ID for XKCD comic",
			},
			"day": &graphql.Field{
				Type:        graphql.String,
				Description: "Date on which comic was released",
			},
			"month": &graphql.Field{
				Type:        graphql.String,
				Description: "Month on which comic was released",
			},
			"year": &graphql.Field{
				Type:        graphql.String,
				Description: "Year when comic was released",
			},
			"link": &graphql.Field{
				Type:        graphql.String,
				Description: "Permalink of the comic",
			},
			"title": &graphql.Field{
				Type:        graphql.String,
				Description: "Title of the comic",
			},
			"safeTitle": &graphql.Field{
				Type:        graphql.String,
				Description: "Safe title of the comic",
			},
			"image": &graphql.Field{
				Type:        graphql.String,
				Description: "Image URL of the comic",
			},
			"alt": &graphql.Field{
				Type:        graphql.String,
				Description: "Alternate text for the image of comic",
			},
			"transcript": &graphql.Field{
				Type:        graphql.String,
				Description: "Description of the comic",
			},
			"news": &graphql.Field{
				Type:        graphql.String,
				Description: "Any associated news relative to comic",
			},
		},
	},
)

XKCDType : GraphQL type for xkcd comic

Functions

func GetSchema

func GetSchema() (graphql.Schema, error)

GetSchema : Returns global schema

Types

This section is empty.

Jump to

Keyboard shortcuts

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