Skip to content

Commit

Permalink
Fix error with multiline input in GraphQL query (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Jun 26, 2024
1 parent 8370572 commit edca833
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Change Log

## UNRELEASED

- Increase batch size for auto-embedding collection texts [#259](https://github.com/hypermodeAI/runtime/pull/259)
- Fix error with multiline input in GraphQL query [#260](https://github.com/hypermodeAI/runtime/pull/260)

## 2024-06-25 - Version 0.9.3

Expand Down
5 changes: 5 additions & 0 deletions graphql/datasource/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type Source struct{}

func (s Source) Load(ctx context.Context, input []byte, writer io.Writer) error {

// Handle newlines in the input - this should only happen in the case of
// a GraphQL query that uses triple quotes for a string literal.
// TODO: fix this upstream in graphql-go-tools
input = bytes.ReplaceAll(input, []byte("\n"), []byte("\\n"))

// Parse the input to get the function call info
var ci callInfo
err := utils.JsonDeserialize(input, &ci)
Expand Down
13 changes: 12 additions & 1 deletion utils/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ import (

// JsonSerialize serializes the given value to JSON.
// Unlike json.Marshal, it does not escape HTML characters.
// It also returns results without an extra newline at the end.
func JsonSerialize(v any) ([]byte, error) {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(v)
return buf.Bytes(), err
if err != nil {
return nil, err
}

// Remove the newline at the end, if there is one
bytes := buf.Bytes()
if bytes[len(bytes)-1] == '\n' {
bytes = bytes[:len(bytes)-1]
}

return bytes, nil
}

// JsonDeserialize deserializes the given JSON data into the given value.
Expand Down

0 comments on commit edca833

Please sign in to comment.