Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GraphQL): fix order of entities query result #7542

Merged
merged 22 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,8 @@ func RunAll(t *testing.T) {
t.Run("query only typename", queryOnlyTypename)
t.Run("query nested only typename", querynestedOnlyTypename)
t.Run("test onlytypename for interface types", onlytypenameForInterface)
t.Run("entitites Query on extended type", entitiesQuery)
t.Run("entitites Query on extended type with key field of type String", entitiesQueryWithKeyFieldOfTypeString)
t.Run("entitites Query on extended type with key field of type Int", entitiesQueryWithKeyFieldOfTypeInt)

t.Run("get state by xid", getStateByXid)
t.Run("get state without args", getStateWithoutArgs)
Expand Down
95 changes: 71 additions & 24 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,10 @@ func allPosts(t *testing.T) []*post {
return result.QueryPost
}

func entitiesQuery(t *testing.T) {
func entitiesQueryWithKeyFieldOfTypeString(t *testing.T) {
addSpaceShipParams := &GraphQLParams{
Query: `mutation addSpaceShip($id1: String!, $missionId1: String! ) {
addSpaceShip(input: [{id: $id1, missions: [{id: $missionId1, designation: "Apollo1"}]} ]) {
Query: `mutation addSpaceShip($id1: String!, $id2: String!, $id3: String!, $id4: String! ) {
addSpaceShip(input: [{id: $id1, missions: [{id: "Mission1", designation: "Apollo1"}]},{id: $id2, missions: [{id: "Mission2", designation: "Apollo2"}]},{id: $id3, missions: [{id: "Mission3", designation: "Apollo3"}]}, {id: $id4, missions: [{id: "Mission4", designation: "Apollo4"}]}]){
spaceShip {
id
missions {
Expand All @@ -395,17 +395,19 @@ func entitiesQuery(t *testing.T) {
}
}`,
Variables: map[string]interface{}{
"id1": "SpaceShip1",
"missionId1": "Mission1",
"id1": "SpaceShip1",
"id2": "SpaceShip2",
"id3": "SpaceShip3",
"id4": "SpaceShip4",
},
}

gqlResponse := addSpaceShipParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

entitiesQueryParams := &GraphQLParams{
Query: `query _entities($typeName: String!, $id1: String!){
_entities(representations: [{__typename: $typeName, id: $id1}]) {
Query: `query _entities($typeName: String!, $id1: String!, $id2: String!, $id3: String!, $id4: String!){
_entities(representations: [{__typename: $typeName, id: $id4},{__typename: $typeName, id: $id2},{__typename: $typeName, id: $id1},{__typename: $typeName, id: $id3},{__typename: $typeName, id: $id1}]) {
... on SpaceShip {
missions(order: {asc: id}){
id
Expand All @@ -417,32 +419,77 @@ func entitiesQuery(t *testing.T) {
Variables: map[string]interface{}{
"typeName": "SpaceShip",
"id1": "SpaceShip1",
"id2": "SpaceShip2",
"id3": "SpaceShip3",
"id4": "SpaceShip4",
},
}

entitiesResp := entitiesQueryParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, entitiesResp)

expectedJSON := `{
"_entities": [
{
"missions": [
{
"id": "Mission1",
"designation": "Apollo1"
}
]
}
]
}`
expectedJSON := `{"_entities":[{"missions":[{"designation":"Apollo4","id":"Mission4"}]},{"missions":[{"designation":"Apollo2","id":"Mission2"}]},{"missions":[{"designation":"Apollo1","id":"Mission1"}]},{"missions":[{"designation":"Apollo3","id":"Mission3"}]},{"missions":[{"designation":"Apollo1","id":"Mission1"}]}]}`

JSONEqGraphQL(t, expectedJSON, string(entitiesResp.Data))

spaceShipDeleteFilter := map[string]interface{}{"id": map[string]interface{}{"in": []string{"SpaceShip1", "SpaceShip2", "SpaceShip3", "SpaceShip4"}}}
DeleteGqlType(t, "SpaceShip", spaceShipDeleteFilter, 4, nil)

missionDeleteFilter := map[string]interface{}{"id": map[string]interface{}{"in": []string{"Mission1", "Mission2", "Mission3", "Mission4"}}}
DeleteGqlType(t, "Mission", missionDeleteFilter, 4, nil)

}

func entitiesQueryWithKeyFieldOfTypeInt(t *testing.T) {
addPlanetParams := &GraphQLParams{
Query: `mutation {
addPlanet(input: [{id: 1, missions: [{id: "Mission1", designation: "Apollo1"}]},{id: 2, missions: [{id: "Mission2", designation: "Apollo2"}]},{id: 3, missions: [{id: "Mission3", designation: "Apollo3"}]}, {id: 4, missions: [{id: "Mission4", designation: "Apollo4"}]}]){
planet {
id
missions {
id
designation
}
}
}
}`,
}

gqlResponse := addPlanetParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

entitiesQueryParams := &GraphQLParams{
Query: `query _entities($typeName: String!, $id1: Int!, $id2: Int!, $id3: Int!, $id4: Int!){
_entities(representations: [{__typename: $typeName, id: $id4},{__typename: $typeName, id: $id2},{__typename: $typeName, id: $id1},{__typename: $typeName, id: $id3},{__typename: $typeName, id: $id1}]) {
... on Planet {
missions(order: {asc: id}){
id
designation
}
}
}
}`,
Variables: map[string]interface{}{
"typeName": "Planet",
"id1": 1,
"id2": 2,
"id3": 3,
"id4": 4,
},
}

entitiesResp := entitiesQueryParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, entitiesResp)

expectedJSON := `{"_entities":[{"missions":[{"designation":"Apollo4","id":"Mission4"}]},{"missions":[{"designation":"Apollo2","id":"Mission2"}]},{"missions":[{"designation":"Apollo1","id":"Mission1"}]},{"missions":[{"designation":"Apollo3","id":"Mission3"}]},{"missions":[{"designation":"Apollo1","id":"Mission1"}]}]}`

testutil.CompareJSON(t, expectedJSON, string(entitiesResp.Data))
JSONEqGraphQL(t, expectedJSON, string(entitiesResp.Data))

spaceShipDeleteFilter := map[string]interface{}{"id": map[string]interface{}{"in": []string{"SpaceShip1"}}}
DeleteGqlType(t, "SpaceShip", spaceShipDeleteFilter, 1, nil)
planetDeleteFilter := map[string]interface{}{"id": map[string]interface{}{"in": []int{1, 2, 3, 4}}}
DeleteGqlType(t, "Planet", planetDeleteFilter, 4, nil)

missionDeleteFilter := map[string]interface{}{"id": map[string]interface{}{"in": []string{"Mission1"}}}
DeleteGqlType(t, "Mission", missionDeleteFilter, 1, nil)
missionDeleteFilter := map[string]interface{}{"id": map[string]interface{}{"in": []string{"Mission1", "Mission2", "Mission3", "Mission4"}}}
DeleteGqlType(t, "Mission", missionDeleteFilter, 4, nil)

}

Expand Down
5 changes: 5 additions & 0 deletions graphql/e2e/directives/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ type SpaceShip @key(fields: "id") @extends {
missions: [Mission]
}

type Planet @key(fields: "id") @extends {
id: Int! @id @external
missions: [Mission]
}

type Region {
id: String! @id
name: String!
Expand Down
25 changes: 25 additions & 0 deletions graphql/e2e/directives/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,20 @@
"type": "uid",
"list": true
},
{
"predicate": "Planet.id",
"type": "int",
"index": true,
"tokenizer": [
"int"
],
"upsert": true
},
{
"predicate": "Planet.missions",
"type": "uid",
"list": true
},
{
"predicate": "State.capital",
"type": "string"
Expand Down Expand Up @@ -1137,6 +1151,17 @@
],
"name": "SpaceShip"
},
{
"fields": [
{
"name": "Planet.id"
},
{
"name": "Planet.missions"
}
],
"name": "Planet"
},
{
"fields": [
{
Expand Down
5 changes: 5 additions & 0 deletions graphql/e2e/normal/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ type SpaceShip @key(fields: "id") @extends {
missions: [Mission]
}

type Planet @key(fields: "id") @extends {
id: Int! @id @external
missions: [Mission]
}

type Region {
id: String! @id
name: String!
Expand Down
25 changes: 25 additions & 0 deletions graphql/e2e/normal/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,20 @@
"type": "uid",
"list": true
},
{
"predicate": "Planet.id",
"type": "int",
"index": true,
"tokenizer": [
"int"
],
"upsert": true
},
{
"predicate": "Planet.missions",
"type": "uid",
"list": true
},
{
"predicate": "Starship.length",
"type": "float"
Expand Down Expand Up @@ -1359,6 +1373,17 @@
],
"name": "Zoo"
},
{
"fields": [
{
"name": "Planet.missions"
},
{
"name": "Planet.id"
}
],
"name": "Planet"
},
{
"fields": [
{
Expand Down
12 changes: 6 additions & 6 deletions graphql/resolve/auth_query_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1887,14 +1887,14 @@
USER: "user"
dgquery: |-
query {
_entities(func: uid(_EntityRoot)) {
_entities(func: uid(_EntityRoot), orderasc: Mission.id) {
dgraph.type
Mission.id : Mission.id
Mission.designation : Mission.designation
Mission.startDate : Mission.startDate
dgraph.uid : uid
}
_EntityRoot as var(func: uid(Mission_1)) @filter(uid(Mission_Auth2))
_EntityRoot as var(func: uid(Mission_1), orderasc: Mission.id) @filter(uid(Mission_Auth2))
Mission_1 as var(func: eq(Mission.id, "0x1", "0x2", "0x3")) @filter(type(Mission))
Mission_Auth2 as var(func: uid(Mission_1)) @filter(eq(Mission.supervisorName, "user")) @cascade {
Mission.id : Mission.id
Expand All @@ -1916,15 +1916,15 @@
USER: "user"
dgquery: |-
query {
_entities(func: uid(_EntityRoot)) {
_entities(func: uid(_EntityRoot), orderasc: Astronaut.id) {
dgraph.type
Astronaut.missions : Astronaut.missions @filter(uid(Mission_1)) {
Mission.designation : Mission.designation
dgraph.uid : uid
}
dgraph.uid : uid
}
_EntityRoot as var(func: uid(Astronaut_4))
_EntityRoot as var(func: uid(Astronaut_4), orderasc: Astronaut.id)
Astronaut_4 as var(func: eq(Astronaut.id, "0x1", "0x2", "0x3")) @filter(type(Astronaut))
var(func: uid(_EntityRoot)) {
Mission_2 as Astronaut.missions
Expand Down Expand Up @@ -1968,11 +1968,11 @@
ROLE: "admin"
dgquery: |-
query {
_entities(func: uid(_EntityRoot)) {
_entities(func: uid(_EntityRoot), orderasc: Astronaut.id) {
dgraph.type
dgraph.uid : uid
}
_EntityRoot as var(func: uid(Astronaut_3))
_EntityRoot as var(func: uid(Astronaut_3), orderasc: Astronaut.id)
Astronaut_3 as var(func: eq(Astronaut.id, "0x1", "0x2", "0x3")) @filter(type(Astronaut))
}

Expand Down
15 changes: 12 additions & 3 deletions graphql/resolve/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,22 @@ func (qr QueryResolverFunc) Resolve(ctx context.Context, query schema.Query) *Re
// NewQueryResolver creates a new query resolver. The resolver runs the pipeline:
// 1) rewrite the query using qr (return error if failed)
// 2) execute the rewritten query with ex (return error if failed)
// 3) process the result with rc
func NewQueryResolver(qr QueryRewriter, ex DgraphExecutor) QueryResolver {
return &queryResolver{queryRewriter: qr, executor: ex}
return &queryResolver{queryRewriter: qr, executor: ex, resultCompleter: CompletionFunc(noopCompletion)}
}

// NewEntitiesQueryResolver creates a new query resolver for `_entities` query.
// It is introduced because result completion works little different for `_entities` query.
func NewEntitiesQueryResolver(qr QueryRewriter, ex DgraphExecutor) QueryResolver {
return &queryResolver{queryRewriter: qr, executor: ex, resultCompleter: CompletionFunc(entitiesQueryCompletion)}
}

// a queryResolver can resolve a single GraphQL query field.
type queryResolver struct {
queryRewriter QueryRewriter
executor DgraphExecutor
queryRewriter QueryRewriter
executor DgraphExecutor
resultCompleter ResultCompleter
}

func (qr *queryResolver) Resolve(ctx context.Context, query schema.Query) *Resolved {
Expand All @@ -82,6 +90,7 @@ func (qr *queryResolver) Resolve(ctx context.Context, query schema.Query) *Resol
defer timer.Stop()

resolved := qr.rewriteAndExecute(ctx, query)
qr.resultCompleter.Complete(ctx, resolved)
resolverTrace.Dgraph = resolved.Extensions.Tracing.Execution.Resolvers[0].Dgraph
resolved.Extensions.Tracing.Execution.Resolvers[0] = resolverTrace
return resolved
Expand Down
Loading