graphql

package
v0.0.0-...-cda998f Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: Apache-2.0 Imports: 24 Imported by: 0

README

GraphQL Development

Relevant Files

*.graphql - GraphQL Schema(s)

gqlgen.yml - Configuration for gqlgen

resolver.go - Main resolver file

*_resolver.go - Namespace resolver file(s)

Schema Naming Conventions

Queries/Mutations

Queries and mutations follow GraphQL naming conventions (GraphQL Specs), but we prefix with an additional namespace. The query/mutation names are lower camel case. Namespace names should be one word and short, yet descriptive.

Spec:
<namespace><query/mutation field>

Examples:
type Query {
	query1
	query2
	query3
}

type Mutation {
	mutation1
}
Namespaces

Namespaces designate a group of related types, interfaces, and operations. Each namespace must have a corresponding file containing all related resolvers for the namespace. This will keep all related code together rather than in one giant sprawling file.

Exs.
pkg/graphql/crawl_resolver.go
pkg/graphql/user_resolver.go

resolver.go will remain and contain the base resolvers.

Special words

Words like id, url, json must be in all-caps unless it is it's on it's own. ex. userJSON, applicationID, serviceURL, id, uid.

schema.graphql organization

Namespaced types and interfaces must be grouped together in alphabetical order both in the file and within the Query and Mutation schemas.

Ex.

type Query {

	// Queries for Namespace1 in alphabetical order
	...

	// Queries for Namespace2 in alphabetical order
	...
}

type Mutation {

	// Mutations for Namespace1 in alphabetical order
	...

	// Mutations for Namespace2 in alphabetical order
	...
}

// Types/interfaces for Namespace1 in alphabetical order
...

// Types/nrsignup for Namespace2 in alphabetical order
...

// All scalars in alphabetical order
...

gqlgen

Read these before proceeding to update or add endpoints.

Github

Docs

Examples

To Update/Add To GraphQL

  1. Update models, queries, inputs, etc. in schema.graphql
  2. Write any models you want to use with the schema and put them into pkg/models (or let them autogenerate into model_gen.go).
  3. Update gqlgen.yml with the locations of the models you have written.
  4. Run the code generator as described below. This will generate model_gen.go and exec_gen.go.
  5. Implement code in resolver.go or *_resolver.go.

To Generate Code

cd pkg/graphql
go run github.com/99designs/gqlgen -v

*_resolver.go

*_resolver.go and resolver.go do not update on subsequent code generation via gqlgen, so it will need to be updated by hand. Will see if the gqlgen project will fix this in any future releases.

The options here are:

  1. If it is small updates to fields or field types, it is straightforward to update by hand.
  2. If there are new models or new endpoints, temporarily move the existing resolver.go and call the code generator again to generate a new stubbed out resolver.go. Go through the new one and copy over the new stubbed resolvers over to the original resolver.go. Implement the stubbed out resolvers in the original resolver.go

Run make test and/or make lint and check for errors and if things are not matching up.

dataloaden

Reference: https://gqlgen.com/reference/dataloaders/ and https://github.com/vektah/dataloaden

To generate a dataloader file (i.e. for a Type that creates typeloader_gen.go):

make install-dataloaden
cd pkg/graphql
// ex.
dataloaden TypeLoader int "*github.com/joincivil/id-hub/pkg/model.Type"

Then implement code in dataloaders.go and modify resolver.go or *_resolver.go.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAccessDenied is a generic error for unauthorized access
	ErrAccessDenied = errors.New("access denied")

	// ResponseOK is a generic OK response string
	ResponseOK = "ok"

	// ResponseError is a generic error response string
	ResponseError = "error"

	// ResponseNotImplemented is a generic response string for non-implemented endpoints
	ResponseNotImplemented = "not implemented"
)

Functions

func ConvertArticleMetadata

func ConvertArticleMetadata(in *ArticleMetadataInput) (*article.Metadata, error)

ConvertArticleMetadata converts incoming article metadata and converts it to core article metadata object

func ConvertCredentialSchema

func ConvertCredentialSchema(in ClaimCredentialSchemaInput) claimtypes.CredentialSchema

ConvertCredentialSchema converts input cred schema to core credential schema

func ConvertCredentialSubject

func ConvertCredentialSubject(in *ClaimCredentialSubjectInput) (
	claimtypes.ContentCredentialSubject, error)

ConvertCredentialSubject converts subject input to core credential subject

func ConvertCredentialTypes

func ConvertCredentialTypes(in []string) []claimtypes.CredentialType

ConvertCredentialTypes converts strings to credential types

func ConvertInputProof

func ConvertInputProof(in *LinkedDataProofInput) (*linkeddata.Proof, error)

ConvertInputProof validates and converts linked data proof input to a core linked data proof.

func InputClaimToContentCredential

func InputClaimToContentCredential(in *ClaimSaveRequestInput) (*claimtypes.ContentCredential, error)

InputClaimToContentCredential is convenience function to convert input claim to content credential

func NewExecutableSchema

func NewExecutableSchema(cfg Config) graphql.ExecutableSchema

NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.

Types

type ArticleMetadataContributorInput

type ArticleMetadataContributorInput struct {
	Role *string `json:"role"`
	Name *string `json:"name"`
}

type ArticleMetadataImageInput

type ArticleMetadataImageInput struct {
	URL  *string `json:"url"`
	Hash *string `json:"hash"`
	H    *int    `json:"h"`
	W    *int    `json:"w"`
}

type ArticleMetadataInput

type ArticleMetadataInput struct {
	Title               *string                            `json:"title"`
	RevisionContentHash *string                            `json:"revisionContentHash"`
	RevisionContentURL  *string                            `json:"revisionContentURL"`
	CanonicalURL        *string                            `json:"canonicalURL"`
	Slug                *string                            `json:"slug"`
	Description         *string                            `json:"description"`
	Contributors        []*ArticleMetadataContributorInput `json:"contributors"`
	Images              []*ArticleMetadataImageInput       `json:"images"`
	Tags                []*string                          `json:"tags"`
	PrimaryTag          *string                            `json:"primaryTag"`
	RevisionDate        *string                            `json:"revisionDate"`
	OriginalPublishDate *string                            `json:"originalPublishDate"`
	Opinion             *bool                              `json:"opinion"`
	CivilSchemaVersion  *string                            `json:"civilSchemaVersion"`
}

type ArticleMetadataResolver

type ArticleMetadataResolver interface {
	RevisionDate(ctx context.Context, obj *article.Metadata) (*string, error)
	OriginalPublishDate(ctx context.Context, obj *article.Metadata) (*string, error)
}

type ClaimCredentialSchemaInput

type ClaimCredentialSchemaInput struct {
	ID   string `json:"id"`
	Type string `json:"type"`
}

type ClaimCredentialSubjectInput

type ClaimCredentialSubjectInput struct {
	ID       string                `json:"id"`
	Metadata *ArticleMetadataInput `json:"metadata"`
}

type ClaimGetRequestInput

type ClaimGetRequestInput struct {
	Did string `json:"did"`
}

type ClaimGetResponse

type ClaimGetResponse struct {
	Claims []*claimtypes.ContentCredential `json:"claims"`
}

ClaimGetResponse represents the GraphQL response for ClaimGet

func (*ClaimGetResponse) ClaimsRaw

func (d *ClaimGetResponse) ClaimsRaw() []string

ClaimsRaw returns the raw JSON string for the list of claims

type ClaimInput

type ClaimInput struct {
	Context           []string                     `json:"context"`
	Type              []string                     `json:"type"`
	CredentialSubject *ClaimCredentialSubjectInput `json:"credentialSubject"`
	Issuer            string                       `json:"issuer"`
	Holder            string                       `json:"holder"`
	CredentialSchema  *ClaimCredentialSchemaInput  `json:"credentialSchema"`
	IssuanceDate      string                       `json:"issuanceDate"`
	Proof             []*LinkedDataProofInput      `json:"proof"`
}

type ClaimProofRequestInput

type ClaimProofRequestInput struct {
	Claim     *ClaimInput `json:"claim"`
	ClaimJSON *string     `json:"claimJson"`
	Did       string      `json:"did"`
}

type ClaimProofResponse

type ClaimProofResponse struct {
	Claim    *claimtypes.ContentCredential `json:"claim"`
	ClaimRaw string                        `json:"claimRaw"`
}

type ClaimRegisteredProof

type ClaimRegisteredProof struct {
	Type                   string `json:"type"`
	Did                    string `json:"did"`
	ExistsInDIDMTProof     string `json:"existsInDIDMTProof"`
	NotRevokedInDIDMTProof string `json:"notRevokedInDIDMTProof"`
	DidMTRootExistsProof   string `json:"didMTRootExistsProof"`
	DidRootExistsVersion   int    `json:"didRootExistsVersion"`
	Root                   string `json:"root"`
	DidMTRoot              string `json:"didMTRoot"`
}

func (ClaimRegisteredProof) IsProof

func (ClaimRegisteredProof) IsProof()

type ClaimResolver

type ClaimResolver interface {
	Type(ctx context.Context, obj *claimtypes.ContentCredential) ([]string, error)
	CredentialSubject(ctx context.Context, obj *claimtypes.ContentCredential) (*ContentClaimCredentialSubject, error)

	IssuanceDate(ctx context.Context, obj *claimtypes.ContentCredential) (string, error)
	Proof(ctx context.Context, obj *claimtypes.ContentCredential) ([]Proof, error)
}

type ClaimSaveRequestInput

type ClaimSaveRequestInput struct {
	Claim     *ClaimInput `json:"claim"`
	ClaimJSON *string     `json:"claimJson"`
}

type ClaimSaveResponse

type ClaimSaveResponse struct {
	Claim *claimtypes.ContentCredential `json:"claim"`
}

ClaimSaveResponse represents the GraphQL response for ClaimSave

func (*ClaimSaveResponse) ClaimRaw

func (d *ClaimSaveResponse) ClaimRaw() *string

ClaimRaw returns the raw JSON string of the claim

type ComplexityRoot

type ComplexityRoot struct {
	ArticleMetadata struct {
		CanonicalURL        func(childComplexity int) int
		CivilSchemaVersion  func(childComplexity int) int
		Contributors        func(childComplexity int) int
		Description         func(childComplexity int) int
		Images              func(childComplexity int) int
		Opinion             func(childComplexity int) int
		OriginalPublishDate func(childComplexity int) int
		PrimaryTag          func(childComplexity int) int
		RevisionContentHash func(childComplexity int) int
		RevisionContentURL  func(childComplexity int) int
		RevisionDate        func(childComplexity int) int
		Slug                func(childComplexity int) int
		Tags                func(childComplexity int) int
		Title               func(childComplexity int) int
	}

	ArticleMetadataContributor struct {
		Name func(childComplexity int) int
		Role func(childComplexity int) int
	}

	ArticleMetadataImage struct {
		H    func(childComplexity int) int
		Hash func(childComplexity int) int
		URL  func(childComplexity int) int
		W    func(childComplexity int) int
	}

	Claim struct {
		Context           func(childComplexity int) int
		CredentialSchema  func(childComplexity int) int
		CredentialSubject func(childComplexity int) int
		Holder            func(childComplexity int) int
		IssuanceDate      func(childComplexity int) int
		Issuer            func(childComplexity int) int
		Proof             func(childComplexity int) int
		Type              func(childComplexity int) int
	}

	ClaimCredentialSchema struct {
		ID   func(childComplexity int) int
		Type func(childComplexity int) int
	}

	ClaimGetResponse struct {
		Claims    func(childComplexity int) int
		ClaimsRaw func(childComplexity int) int
	}

	ClaimProofResponse struct {
		Claim    func(childComplexity int) int
		ClaimRaw func(childComplexity int) int
	}

	ClaimRegisteredProof struct {
		Did                    func(childComplexity int) int
		DidMTRoot              func(childComplexity int) int
		DidMTRootExistsProof   func(childComplexity int) int
		DidRootExistsVersion   func(childComplexity int) int
		ExistsInDIDMTProof     func(childComplexity int) int
		NotRevokedInDIDMTProof func(childComplexity int) int
		Root                   func(childComplexity int) int
		Type                   func(childComplexity int) int
	}

	ClaimSaveResponse struct {
		Claim    func(childComplexity int) int
		ClaimRaw func(childComplexity int) int
	}

	ContentClaimCredentialSubject struct {
		ID       func(childComplexity int) int
		Metadata func(childComplexity int) int
	}

	DidDocAuthentication struct {
		IDOnly    func(childComplexity int) int
		PublicKey func(childComplexity int) int
	}

	DidDocPublicKey struct {
		Controller         func(childComplexity int) int
		EthereumAddress    func(childComplexity int) int
		ID                 func(childComplexity int) int
		PublicKeyBase58    func(childComplexity int) int
		PublicKeyBase64    func(childComplexity int) int
		PublicKeyHex       func(childComplexity int) int
		PublicKeyJwk       func(childComplexity int) int
		PublicKeyMultibase func(childComplexity int) int
		PublicKeyPem       func(childComplexity int) int
		Type               func(childComplexity int) int
	}

	DidDocService struct {
		Description     func(childComplexity int) int
		ID              func(childComplexity int) int
		PublicKey       func(childComplexity int) int
		ServiceEndpoint func(childComplexity int) int
		Type            func(childComplexity int) int
	}

	DidDocument struct {
		Authentications func(childComplexity int) int
		Context         func(childComplexity int) int
		Controller      func(childComplexity int) int
		Created         func(childComplexity int) int
		ID              func(childComplexity int) int
		Proof           func(childComplexity int) int
		PublicKeys      func(childComplexity int) int
		Services        func(childComplexity int) int
		Updated         func(childComplexity int) int
	}

	DidGetResponse struct {
		Doc    func(childComplexity int) int
		DocRaw func(childComplexity int) int
	}

	DidSaveResponse struct {
		Doc    func(childComplexity int) int
		DocRaw func(childComplexity int) int
	}

	Edge struct {
		Data  func(childComplexity int) int
		From  func(childComplexity int) int
		Hash  func(childComplexity int) int
		JWT   func(childComplexity int) int
		Proof func(childComplexity int) int
		Time  func(childComplexity int) int
		To    func(childComplexity int) int
		Type  func(childComplexity int) int
	}

	LinkedDataProof struct {
		Created    func(childComplexity int) int
		Creator    func(childComplexity int) int
		Domain     func(childComplexity int) int
		Nonce      func(childComplexity int) int
		ProofValue func(childComplexity int) int
		Type       func(childComplexity int) int
	}

	Mutation struct {
		AddEdge   func(childComplexity int, edgeJwt *string) int
		ClaimSave func(childComplexity int, in *ClaimSaveRequestInput) int
		Version   func(childComplexity int) int
	}

	Query struct {
		ClaimGet   func(childComplexity int, in *ClaimGetRequestInput) int
		ClaimProof func(childComplexity int, in *ClaimProofRequestInput) int
		DidGet     func(childComplexity int, in *DidGetRequestInput) int
		FindEdges  func(childComplexity int, in *FindEdgesInput) int
		Version    func(childComplexity int) int
	}

	RootOnBlockChainProof struct {
		BlockNumber      func(childComplexity int) int
		CommitterAddress func(childComplexity int) int
		ContractAddress  func(childComplexity int) int
		Root             func(childComplexity int) int
		TxHash           func(childComplexity int) int
		Type             func(childComplexity int) int
	}
}

type Config

type Config struct {
	Resolvers  ResolverRoot
	Directives DirectiveRoot
	Complexity ComplexityRoot
}

type ContentClaimCredentialSubject

type ContentClaimCredentialSubject struct {
	ID       string            `json:"id"`
	Metadata *article.Metadata `json:"metadata"`
}

type DidDocAuthenticationResolver

type DidDocAuthenticationResolver interface {
	PublicKey(ctx context.Context, obj *did.DocAuthenicationWrapper) (*did.DocPublicKey, error)
}

type DidDocPublicKeyResolver

type DidDocPublicKeyResolver interface {
	ID(ctx context.Context, obj *did.DocPublicKey) (*string, error)
	Type(ctx context.Context, obj *did.DocPublicKey) (*string, error)
	Controller(ctx context.Context, obj *did.DocPublicKey) (*string, error)
}

type DidDocServiceResolver

type DidDocServiceResolver interface {
	ID(ctx context.Context, obj *did.DocService) (*string, error)

	ServiceEndpoint(ctx context.Context, obj *did.DocService) (*utils.AnyValue, error)
}

type DidDocumentResolver

type DidDocumentResolver interface {
	ID(ctx context.Context, obj *did.Document) (*string, error)

	Controller(ctx context.Context, obj *did.Document) (*string, error)
}

type DidGetRequestInput

type DidGetRequestInput struct {
	Did *string `json:"did"`
}

type DidGetResponse

type DidGetResponse struct {
	Doc *did.Document `json:"doc"`
}

DidGetResponse represents the GraphQL response for DidGet

func (*DidGetResponse) DocRaw

func (d *DidGetResponse) DocRaw() *string

DocRaw returns the raw JSON string for the docRaw field

type DidSaveResponse

type DidSaveResponse struct {
	Doc *did.Document `json:"doc"`
}

DidSaveResponse represents the GraphQL response for DidSave

func (*DidSaveResponse) DocRaw

func (d *DidSaveResponse) DocRaw() *string

DocRaw returns the raw JSON string for the docRaw field

type DirectiveRoot

type DirectiveRoot struct {
}

type FindEdgesInput

type FindEdgesInput struct {
	FromDid []*string `json:"fromDID"`
	ToDid   []*string `json:"toDID"`
}

type LinkedDataProofInput

type LinkedDataProofInput struct {
	Type       *string    `json:"type"`
	Creator    *string    `json:"creator"`
	Created    *time.Time `json:"created"`
	ProofValue *string    `json:"proofValue"`
	Domain     *string    `json:"domain"`
	Nonce      *string    `json:"nonce"`
}

type MutationResolver

type MutationResolver interface {
	Version(ctx context.Context) (string, error)
	ClaimSave(ctx context.Context, in *ClaimSaveRequestInput) (*ClaimSaveResponse, error)
	AddEdge(ctx context.Context, edgeJwt *string) (*claimsstore.JWTClaimPostgres, error)
}

type Proof

type Proof interface {
	IsProof()
}

type QueryResolver

type QueryResolver interface {
	Version(ctx context.Context) (string, error)
	DidGet(ctx context.Context, in *DidGetRequestInput) (*DidGetResponse, error)
	ClaimGet(ctx context.Context, in *ClaimGetRequestInput) (*ClaimGetResponse, error)
	ClaimProof(ctx context.Context, in *ClaimProofRequestInput) (*ClaimProofResponse, error)
	FindEdges(ctx context.Context, in *FindEdgesInput) ([]*claimsstore.JWTClaimPostgres, error)
}

type Resolver

type Resolver struct {
	DidService   *did.Service
	ClaimService *claims.Service
	JWTService   *claims.JWTService
}

Resolver is the main GraphQL resolver

func (*Resolver) ArticleMetadata

func (r *Resolver) ArticleMetadata() ArticleMetadataResolver

ArticleMetadata returns the resolver for article metadata

func (*Resolver) Claim

func (r *Resolver) Claim() ClaimResolver

Claim returns the resolver for claims

func (*Resolver) DidDocAuthentication

func (r *Resolver) DidDocAuthentication() DidDocAuthenticationResolver

DidDocAuthentication is the resolver for DID Authentications

func (*Resolver) DidDocPublicKey

func (r *Resolver) DidDocPublicKey() DidDocPublicKeyResolver

DidDocPublicKey is the resolver for the DID public key

func (*Resolver) DidDocService

func (r *Resolver) DidDocService() DidDocServiceResolver

DidDocService is the resolver for the DID service

func (*Resolver) DidDocument

func (r *Resolver) DidDocument() DidDocumentResolver

DidDocument is the resolver for the DID document

func (*Resolver) Edge

func (r *Resolver) Edge() EdgeResolver

Edge returns an edge resolver

func (*Resolver) Mutation

func (r *Resolver) Mutation() MutationResolver

Mutation is the resolver for the Mutation type

func (*Resolver) Query

func (r *Resolver) Query() QueryResolver

Query is the resolver for the Query type

func (*Resolver) Version

func (r *Resolver) Version(ctx context.Context) (string, error)

Version returns the version of the GraphQL API

type ResolverRoot

type ResolverRoot interface {
	ArticleMetadata() ArticleMetadataResolver
	Claim() ClaimResolver
	DidDocAuthentication() DidDocAuthenticationResolver
	DidDocPublicKey() DidDocPublicKeyResolver
	DidDocService() DidDocServiceResolver
	DidDocument() DidDocumentResolver
	Edge() EdgeResolver
	Mutation() MutationResolver
	Query() QueryResolver
}

type RootOnBlockChainProof

type RootOnBlockChainProof struct {
	Type             string `json:"type"`
	BlockNumber      string `json:"blockNumber"`
	Root             string `json:"root"`
	ContractAddress  string `json:"contractAddress"`
	CommitterAddress string `json:"committerAddress"`
	TxHash           string `json:"txHash"`
}

func (RootOnBlockChainProof) IsProof

func (RootOnBlockChainProof) IsProof()

Jump to

Keyboard shortcuts

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