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):This PR allows to use fragments on interfaces while querying other interface. #6964

Merged
merged 6 commits into from
Nov 24, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added e2e and integration test.
JatinDev543 committed Nov 24, 2020
commit ad838e11df864b7cf5a4bac2f7268e18bfb7f9f6
25 changes: 25 additions & 0 deletions graphql/e2e/common/fragment.go
Original file line number Diff line number Diff line change
@@ -169,6 +169,20 @@ func fragmentInQueryOnInterface(t *testing.T) {
n: name
}
}
qc3: queryCharacter {
... on Droid{
__typename
primaryFunction
}
... on Employee {
__typename
ename
}
... on Human {
__typename
name
}
}
qcRep1: queryCharacter {
name
... on Human {
@@ -316,6 +330,17 @@ func fragmentInQueryOnInterface(t *testing.T) {
{
}
],
"qc3":[
{
"__typename":"Human",
"ename":"Han_employee",
"name":"Han"
},
{
"__typename":"Droid",
"primaryFunction":"Robot"
}
],
"qcRep1": [
{
"name": "Han",
27 changes: 27 additions & 0 deletions graphql/resolve/query_test.yaml
Original file line number Diff line number Diff line change
@@ -1935,6 +1935,33 @@
}
}

-
name: "queryCharacter with fragment on interface and type"
gqlquery: |
query {
queryCharacter {
id
name
... on Employee {
ename
}
... on Human {
ename
female
}
}
}
dgquery: |-
query {
queryCharacter(func: type(Character)) {
dgraph.type
id : uid
name : Character.name
ename : Employee.ename
female : Human.female
}
}

-
name: "Filter with id uses uid func at root."
gqlquery: |
28 changes: 5 additions & 23 deletions graphql/schema/request.go
Original file line number Diff line number Diff line change
@@ -275,9 +275,12 @@ func recursivelyExpandFragmentSelections(field *ast.Field, op *operation) {
satisfies := []string{typeName, ""}
var additionalTypes map[string]bool
switch typeKind {
case ast.Interface:
// expand fragments on types which implement this interface
case ast.Interface, ast.Union:
// expand fragments on types which implement this interface (for interface case)
// expand fragments on member types of this union (for Union case)
additionalTypes = getTypeNamesAsMap(op.inSchema.schema.PossibleTypes[typeName])
// also, expand fragments on interfaces which are implemented by the member types of this union
// And also on additional interfaces which also implement the same type
var interfaceFragsToExpand []*ast.Definition
for Type := range additionalTypes {
interfaceFragsToExpand = append(interfaceFragsToExpand,
@@ -293,27 +296,6 @@ func recursivelyExpandFragmentSelections(field *ast.Field, op *operation) {
getTypeNamesAsMap(op.inSchema.schema.PossibleTypes[interfaceName]), op)
}
}
case ast.Union:
// expand fragments on member types of this union
additionalTypes = getTypeNamesAsMap(op.inSchema.schema.PossibleTypes[typeName])
// also, expand fragments on interfaces which are implemented by the member types of this
// union
var interfaceFragsToExpand []*ast.Definition
for memberType := range additionalTypes {
interfaceFragsToExpand = append(interfaceFragsToExpand,
op.inSchema.schema.Implements[memberType]...)
}
additionalInterfaces := getTypeNamesAsMap(interfaceFragsToExpand)
// for fragments in the selection set of this union field, need to store a mapping from
// fields in that fragment to the fragment's type condition, for each of the additional
// interfaces, to be used later in completion.
for interfaceName := range additionalInterfaces {
additionalTypes[interfaceName] = true
for _, f := range field.SelectionSet {
addSelectionToInterfaceImplFragFields(interfaceName, f,
getTypeNamesAsMap(op.inSchema.schema.PossibleTypes[interfaceName]), op)
}
}
case ast.Object:
// expand fragments on interfaces which are implemented by this object
additionalTypes = getTypeNamesAsMap(op.inSchema.schema.Implements[typeName])