graphql

package
v0.42.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: MIT Imports: 3 Imported by: 0

README

graphql

This package provides helpers that simplify handling of graphql related things.

Schema parsing

Create a struct representaiton of your schema objects:

schema, err := graphql.NewSchema(schemaString)
if err != nil {
	panic(err)
}

The returned schema struct offers useful functions that can be used to inspect the schema.

Schema validation

You can use this library to validate a given graphql schema against your requirements:

Instantiationg the validators

There are validators for directives, fields and objeccts.

validator := graphql.NewSchemaValidator(&validator.ObjectConfig{
	// configure your object requirements here
}, &validator.DirectiveConfig{
	// configure your directive requirements here
}, &validator.FieldConfig{
	// configure your field requirements here
})

if err := validator.Validate(schema); err != nil {
	panic(err)
}

Schema printing

You can print your schema to a graphql schema string using:

schema, err := graphql.NewSchema(schemaString)
if err != nil {
	panic(err)
}

generatedSchemaString, err := schema.GetString()
if err != nil {
	panic(err)
}

The generatedSchemaString will be logically equal to the schama that you one provided as schemaString (It might have differences in formatting, therefore it will not be 100% equal).

Testing

For testing purposes the schema package provides mocks that can be used to mock the validators in your project:

ObjectValidator:

validator := validator.MockObjectValidator{}

FieldValidator:

validator := validator.MockFieldValidator{}

DirectiveValidator:

validator := validator.MockDirectiveValidator{}

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSchema

func NewSchema(schema string) (*schema.Schema, error)

Types

type SchemaValidator

type SchemaValidator struct {
	// contains filtered or unexported fields
}

func NewSchemaValidator

func NewSchemaValidator(
	objectConfig *validator.ObjectConfig,
	directiveConfig *validator.DirectiveConfig,
	fieldConfig *validator.FieldConfig,
) *SchemaValidator

func (*SchemaValidator) Validate

func (v *SchemaValidator) Validate(schema *schema.Schema, customScalarTypes []string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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