<commit_before>package parser

import (
	"errors"

	"github.com/tmc/graphql"
	"github.com/tmc/graphql/internal/parser"
)

var (
	// ErrMalformedOperation means there was a parsing error with the provided query
	ErrMalformedOperation = errors.New("parser: malformed graphql operation")
	// ErrMultipleOperations
	ErrMultipleOperations = errors.New("parser: multiple graphql operations")
)

// ParseOperation attempts to parse a graphql.Operation from a byte slice.
func ParseOperation(query []byte) (graphql.Operation, error) {
	result, err := parser.Parse("", query)
	if err != nil {
		panic(err)
		// TODO(tmc))
		return graphql.Operation{}, ErrMalformedOperation
	}
	doc, ok := result.(graphql.Document)
	if !ok {
		return graphql.Operation{}, ErrMalformedOperation
	}
	switch len(doc.Operations) {
	case 1:
		return doc.Operations[0], nil
	case 0:
		return graphql.Operation{}, ErrMalformedOperation
	default:
		return graphql.Operation{}, ErrMultipleOperations
	}
}<commit_msg>return a pointer to an Operation from ParseOperation<commit_after>package parser

import (
	"errors"
	"log"

	"github.com/tmc/graphql"
	"github.com/tmc/graphql/internal/parser"
)

var (
	// ErrMalformedOperation means there was a parsing error with the provided query
	ErrMalformedOperation = errors.New("parser: malformed graphql operation")
	// ErrMultipleOperations
	ErrMultipleOperations = errors.New("parser: multiple graphql operations")
)

// ParseOperation attempts to parse a graphql.Operation from a byte slice.
func ParseOperation(query []byte) (*graphql.Operation, error) {
	result, err := parser.Parse("", query)
	if err != nil {
		log.Println("parse error:", err)
		return nil, ErrMalformedOperation
	}
	doc, ok := result.(graphql.Document)
	if !ok {
		return nil, ErrMalformedOperation
	}
	switch len(doc.Operations) {
	case 1:
		return &doc.Operations[0], nil
	case 0:
		return nil, ErrMalformedOperation
	default:
		return nil, ErrMultipleOperations
	}
}
{'fraction_non_alphanumeric': 0.1096311475409836, 'fraction_numerical': 0.0030737704918032786, 'mean_word_length': 4.829850746268657, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7413832', 'n_tokens_mistral': 616, 'n_tokens_neox': 603, 'n_words': 169}