Skip to content

Commit

Permalink
refactor: GQL responses (#2872)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #2869

## Description

This PR makes our GQL response types spec compliant. It also enables
running multiple selections within `queries` and `mutations`.

- [x] Update readme examples

## Tasks

- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?

`make test`

Specify the platform(s) on which this was tested:
- MacOS
  • Loading branch information
nasdf committed Jul 30, 2024
1 parent 82659f8 commit 8bd255f
Show file tree
Hide file tree
Showing 455 changed files with 13,087 additions and 10,074 deletions.
66 changes: 35 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,13 @@ Expected response:

```json
{
"data": [
{
"_docID": "bae-91171025-ed21-50e3-b0dc-e31bccdfa1ab",
}
]
"data": {
"create_User": [
{
"_docID": "bae-91171025-ed21-50e3-b0dc-e31bccdfa1ab",
}
]
}
}
```

Expand Down Expand Up @@ -223,37 +225,39 @@ It returns a structure similar to the following, which contains the update paylo

```json
{
"data": [
{
"cid": "bafybeifhtfs6vgu7cwbhkojneh7gghwwinh5xzmf7nqkqqdebw5rqino7u",
"delta": "pGNhZ2UYH2RuYW1lY0JvYmZwb2ludHMYWmh2ZXJpZmllZPU=",
"height": 1,
"links": [
{
"cid": "bafybeiet6foxcipesjurdqi4zpsgsiok5znqgw4oa5poef6qtiby5hlpzy",
"name": "age"
},
{
"cid": "bafybeielahxy3r3ulykwoi5qalvkluojta4jlg6eyxvt7lbon3yd6ignby",
"name": "name"
},
{
"cid": "bafybeia3tkpz52s3nx4uqadbm7t5tir6gagkvjkgipmxs2xcyzlkf4y4dm",
"name": "points"
},
{
"cid": "bafybeia4off4javopmxcdyvr6fgb5clo7m5bblxic5sqr2vd52s6khyksm",
"name": "verified"
}
]
}
]
"data": {
"latestCommits": [
{
"cid": "bafybeifhtfs6vgu7cwbhkojneh7gghwwinh5xzmf7nqkqqdebw5rqino7u",
"delta": "pGNhZ2UYH2RuYW1lY0JvYmZwb2ludHMYWmh2ZXJpZmllZPU=",
"height": 1,
"links": [
{
"cid": "bafybeiet6foxcipesjurdqi4zpsgsiok5znqgw4oa5poef6qtiby5hlpzy",
"name": "age"
},
{
"cid": "bafybeielahxy3r3ulykwoi5qalvkluojta4jlg6eyxvt7lbon3yd6ignby",
"name": "name"
},
{
"cid": "bafybeia3tkpz52s3nx4uqadbm7t5tir6gagkvjkgipmxs2xcyzlkf4y4dm",
"name": "points"
},
{
"cid": "bafybeia4off4javopmxcdyvr6fgb5clo7m5bblxic5sqr2vd52s6khyksm",
"name": "verified"
}
]
}
]
}
}
```

Obtain a specific commit by its content identifier (`cid`):

```gql
```shell
defradb client query '
query {
commits(cid: "bafybeifhtfs6vgu7cwbhkojneh7gghwwinh5xzmf7nqkqqdebw5rqino7u") {
Expand Down
52 changes: 27 additions & 25 deletions docs/website/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,31 +153,33 @@ It returns a structure similar to the following, which contains the update paylo

```json
{
"data": [
{
"cid": "bafybeifhtfs6vgu7cwbhkojneh7gghwwinh5xzmf7nqkqqdebw5rqino7u",
"delta": "pGNhZ2UYH2RuYW1lY0JvYmZwb2ludHMYWmh2ZXJpZmllZPU=",
"height": 1,
"links": [
{
"cid": "bafybeiet6foxcipesjurdqi4zpsgsiok5znqgw4oa5poef6qtiby5hlpzy",
"name": "age"
},
{
"cid": "bafybeielahxy3r3ulykwoi5qalvkluojta4jlg6eyxvt7lbon3yd6ignby",
"name": "name"
},
{
"cid": "bafybeia3tkpz52s3nx4uqadbm7t5tir6gagkvjkgipmxs2xcyzlkf4y4dm",
"name": "points"
},
{
"cid": "bafybeia4off4javopmxcdyvr6fgb5clo7m5bblxic5sqr2vd52s6khyksm",
"name": "verified"
}
]
}
]
"data": {
"latestCommits": [
{
"cid": "bafybeifhtfs6vgu7cwbhkojneh7gghwwinh5xzmf7nqkqqdebw5rqino7u",
"delta": "pGNhZ2UYH2RuYW1lY0JvYmZwb2ludHMYWmh2ZXJpZmllZPU=",
"height": 1,
"links": [
{
"cid": "bafybeiet6foxcipesjurdqi4zpsgsiok5znqgw4oa5poef6qtiby5hlpzy",
"name": "age"
},
{
"cid": "bafybeielahxy3r3ulykwoi5qalvkluojta4jlg6eyxvt7lbon3yd6ignby",
"name": "name"
},
{
"cid": "bafybeia3tkpz52s3nx4uqadbm7t5tir6gagkvjkgipmxs2xcyzlkf4y4dm",
"name": "points"
},
{
"cid": "bafybeia4off4javopmxcdyvr6fgb5clo7m5bblxic5sqr2vd52s6khyksm",
"name": "verified"
}
]
}
]
}
}
```

Expand Down
38 changes: 18 additions & 20 deletions docs/website/guides/explain-systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,24 @@ query @explain(type: execute) {

```json
// Response
[
{
"explain": {
"executionSuccess": true,
"sizeOfResult": 1,
"planExecutions": 2,
"selectTopNode": {
"selectNode": {
"iterations": 2,
"filterMatches": 1,
"scanNode": {
"iterations": 2,
"docFetches": 2,
"filterMatches": 1
}
}
}
}
}
]
{
"explain": {
"executionSuccess": true,
"sizeOfResult": 1,
"planExecutions": 2,
"selectTopNode": {
"selectNode": {
"iterations": 2,
"filterMatches": 1,
"scanNode": {
"iterations": 2,
"docFetches": 2,
"filterMatches": 1
}
}
}
}
}
```

Because Execute Explain actually executes the plan, it will of course take more time to complete and return results than the Simple Explain. It will actually take slightly longer to execute than the non-explain counterpart, as it has the overhead of measuring and collecting information.
Expand Down
112 changes: 60 additions & 52 deletions docs/website/guides/schema-relationship.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,23 +234,25 @@ query {

```json
// Results:
[
{
"name": "Saadi Shirazi",
"dateOfBirth": "1210-07-23T03:46:56.647Z",
"authoredBooks": [
{
"name": "Gulistan",
"genre": "Poetry",
"description": "Persian poetry of ideas"
},
{
"name": "Bustan",
"genre": "Poetry"
}
]
}
]
{
"Author": [
{
"name": "Saadi Shirazi",
"dateOfBirth": "1210-07-23T03:46:56.647Z",
"authoredBooks": [
{
"name": "Gulistan",
"genre": "Poetry",
"description": "Persian poetry of ideas"
},
{
"name": "Bustan",
"genre": "Poetry"
}
]
}
]
}
```

```graphql
Expand All @@ -268,24 +270,26 @@ query {

```json
// Results:
[
{
"name": "Gulistan",
"genre": "Poetry",
"Author": {
"name": "Saadi Shirazi",
"dateOfBirth": "1210-07-23T03:46:56.647Z",
}
},
{
"name": "Bustan",
"genre": "Poetry",
"Author": {
"name": "Saadi Shirazi",
"dateOfBirth": "1210-07-23T03:46:56.647Z",
{
"Book": [
{
"name": "Gulistan",
"genre": "Poetry",
"Author": {
"name": "Saadi Shirazi",
"dateOfBirth": "1210-07-23T03:46:56.647Z",
}
},
{
"name": "Bustan",
"genre": "Poetry",
"Author": {
"name": "Saadi Shirazi",
"dateOfBirth": "1210-07-23T03:46:56.647Z",
}
}
}
]
]
}
```

```graphql
Expand All @@ -303,18 +307,20 @@ query {

```json
// Results:
[
{
"name": "Saadi Shirazi",
"dateOfBirth": "1210-07-23T03:46:56.647Z",
"authoredBooks": [
{
"name": "Gulistan",
"genre": "Poetry"
}
]
}
]
{
"Author": [
{
"name": "Saadi Shirazi",
"dateOfBirth": "1210-07-23T03:46:56.647Z",
"authoredBooks": [
{
"name": "Gulistan",
"genre": "Poetry"
}
]
}
]
}
```

```graphql
Expand All @@ -330,12 +336,14 @@ query {

```json
// Results:
[
{
"name": "Saadi Shirazi",
"dateOfBirth": "1210-07-23T03:46:56.647Z"
}
]
{
"Author": [
{
"name": "Saadi Shirazi",
"dateOfBirth": "1210-07-23T03:46:56.647Z"
}
]
}
```

Note:
Expand Down
4 changes: 2 additions & 2 deletions http/handler_ccip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestCCIPGet_WithValidData(t *testing.T) {
resHex, err := hex.DecodeString(strings.TrimPrefix(ccipRes.Data, "0x"))
require.NoError(t, err)

assert.JSONEq(t, `{"data": [{"name": "bob"}]}`, string(resHex))
assert.JSONEq(t, `{"data": {"User": [{"name": "bob"}]}}`, string(resHex))
}

func TestCCIPGet_WithSubscription(t *testing.T) {
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestCCIPPost_WithValidData(t *testing.T) {
resHex, err := hex.DecodeString(strings.TrimPrefix(ccipRes.Data, "0x"))
require.NoError(t, err)

assert.JSONEq(t, `{"data": [{"name": "bob"}]}`, string(resHex))
assert.JSONEq(t, `{"data": {"User": [{"name": "bob"}]}}`, string(resHex))
}

func TestCCIPPost_WithInvalidGraphQLRequest(t *testing.T) {
Expand Down
15 changes: 1 addition & 14 deletions http/handler_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func (res *GraphQLResponse) UnmarshalJSON(data []byte) error {
if err := dec.Decode(&out); err != nil {
return err
}
res.Data = out["data"]

// fix errors type to match tests
switch t := out["errors"].(type) {
Expand All @@ -278,20 +279,6 @@ func (res *GraphQLResponse) UnmarshalJSON(data []byte) error {
res.Errors = nil
}

// fix data type to match tests
switch t := out["data"].(type) {
case []any:
var fixed []map[string]any
for _, v := range t {
fixed = append(fixed, v.(map[string]any))
}
res.Data = fixed
case map[string]any:
res.Data = t
default:
res.Data = []map[string]any{}
}

return nil
}

Expand Down
10 changes: 1 addition & 9 deletions internal/db/collection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,7 @@ func (c *collection) makeSelectionPlan(
txn,
)

return planner.MakePlan(&request.Request{
Queries: []*request.OperationDefinition{
{
Selections: []request.Selection{
slct,
},
},
},
})
return planner.MakeSelectionPlan(slct)
}

func (c *collection) makeSelectLocal(filter immutable.Option[request.Filter]) (*request.Select, error) {
Expand Down
Loading

0 comments on commit 8bd255f

Please sign in to comment.