mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-23 08:44:48 -06:00
e45c79a619
* internal/gqlparser: Upgrade to v2.5.1+ of the lib. * internal/gqlparser: Add column offset to error messages. * topdown/graphql: Refactor error message handling for gqlparser v2.5.1. * internal/gqlparser: Remove broken tests. * internal/gqlparser: Fix style nits. Signed-off-by: Philip Conrad <philipaconrad@gmail.com>
46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package gqlparser
|
|
|
|
import (
|
|
"github.com/open-policy-agent/opa/internal/gqlparser/ast"
|
|
"github.com/open-policy-agent/opa/internal/gqlparser/gqlerror"
|
|
"github.com/open-policy-agent/opa/internal/gqlparser/parser"
|
|
"github.com/open-policy-agent/opa/internal/gqlparser/validator"
|
|
|
|
// Blank import is used to load up the validator rules.
|
|
_ "github.com/open-policy-agent/opa/internal/gqlparser/validator/rules"
|
|
)
|
|
|
|
func LoadSchema(str ...*ast.Source) (*ast.Schema, error) {
|
|
return validator.LoadSchema(append([]*ast.Source{validator.Prelude}, str...)...)
|
|
}
|
|
|
|
func MustLoadSchema(str ...*ast.Source) *ast.Schema {
|
|
s, err := validator.LoadSchema(append([]*ast.Source{validator.Prelude}, str...)...)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return s
|
|
}
|
|
|
|
func LoadQuery(schema *ast.Schema, str string) (*ast.QueryDocument, gqlerror.List) {
|
|
query, err := parser.ParseQuery(&ast.Source{Input: str})
|
|
if err != nil {
|
|
gqlErr := err.(*gqlerror.Error)
|
|
return nil, gqlerror.List{gqlErr}
|
|
}
|
|
errs := validator.Validate(schema, query)
|
|
if errs != nil {
|
|
return nil, errs
|
|
}
|
|
|
|
return query, nil
|
|
}
|
|
|
|
func MustLoadQuery(schema *ast.Schema, str string) *ast.QueryDocument {
|
|
q, err := LoadQuery(schema, str)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return q
|
|
}
|