Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Albert/support obj 2 #537

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
174 changes: 173 additions & 1 deletion federation/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ func createExecutorWithFederatedUser() (*Executor, *schemabuilder.Schema, *schem
}
}
*/

type Abilities struct {
Teleportation bool
BreathingFire []int
Thunder *bool
}

type Skills struct {
Sports []string
Plumbing *bool
Eating int
Abilities Abilities
AbilitiesPointer *Abilities
AllAbilities []*Abilities
}

type User struct {
Id int64
OrgId int64
Expand All @@ -62,6 +78,7 @@ func createExecutorWithFederatedUser() (*Executor, *schemabuilder.Schema, *schem
user := s1.Object("User", User{}, schemabuilder.FetchObjectFromKeys(func(args struct{ Keys []*User }) []*User {
return args.Keys
}))

user.Key("id")
type UserIds struct {
Id int64
Expand Down Expand Up @@ -156,21 +173,58 @@ func createExecutorWithFederatedUser() (*Executor, *schemabuilder.Schema, *schem
Name string
Email string
PhoneNumber string
Skills Skills
}

type UserWithContactInfoKeys struct {
Id int64
OrgId int64
Name string
Email string
PhoneNumber string
}

type UserKeysWithOrgId struct {
Id int64
OrgId int64
}
s2 := schemabuilder.NewSchemaWithName("s2")
userWithContactInfo := s2.Object("User", UserWithContactInfo{}, schemabuilder.FetchObjectFromKeys(func(args struct{ Keys []*UserWithContactInfo }) []*UserWithContactInfo {
userWithContactInfo := s2.Object("User", UserWithContactInfo{}, schemabuilder.FetchObjectFromKeys(func(args struct{ Keys []*UserWithContactInfoKeys }) []*UserWithContactInfo {
help := []*UserWithContactInfo{}
plumbing := true
thunder := true
sampleAbilities := Abilities{Teleportation: true, BreathingFire: []int{1, 2, 3}}
sampleAbilities2 := Abilities{Teleportation: true, BreathingFire: []int{4, 5, 6}, Thunder: &thunder}
sampleSkills := Skills{Eating: 1, Plumbing: &plumbing, Sports: nil, Abilities: sampleAbilities, AbilitiesPointer: &sampleAbilities2, AllAbilities: []*Abilities{&sampleAbilities, &sampleAbilities2}}
for _, i := range args.Keys {
new := &UserWithContactInfo{
Id: i.Id,
OrgId: i.OrgId,
Name: i.Name,
Email: i.Email,
PhoneNumber: i.PhoneNumber,
Skills: sampleSkills,
}
help = append(help, new)
}
return help
}))
s2.Object("Skills", Skills{}, schemabuilder.FetchObjectFromKeys(func(args struct{ Keys []*Skills }) []*Skills {
return args.Keys
}))
s2.Object("Abilities", Abilities{}, schemabuilder.FetchObjectFromKeys(func(args struct{ Keys []*Abilities }) []*Abilities {
return args.Keys
}))

userWithContactInfo.Key("id")
userWithContactInfo.FieldFunc("secret", func(ctx context.Context, user *UserWithContactInfo) (string, error) {
return "shhhhh", nil
})

userWithContactInfo.FieldFunc("skills", func(ctx context.Context, user *UserWithContactInfo) (Skills, error) {
return user.Skills, nil
})

/*
----------------------------
Pagination Endpoints/Objects
Expand Down Expand Up @@ -453,6 +507,124 @@ func TestExecutorQueriesBasic(t *testing.T) {
}
}

// This function checks that we are able to query for and receive an object that was passed between gql servers
// Specifically, it tests the functions in planner_helpers.go
func TestExecutorQueriesWithObjectKey(t *testing.T) {
e, _, _, _, err := createExecutorWithFederatedUser()
require.NoError(t, err)
testCases := []struct {
Name string
Query string
Output string
}{
{
Name: "query fields on multiple fields with object passthrough as well as nested object",
Query: `
query Foo {
users {
id
email
phoneNumber
isAdmin
secret
skills {
sports
plumbing
eating
abilities {
teleportation
breathingFire
}
abilitiesPointer{
teleportation
breathingFire
thunder
}
allAbilities {
teleportation
breathingFire
}
}
}
}`,
Output: `
{
"users":[
{
"__key":1,
"id":1,
"email": "[email protected]",
"phoneNumber": "555-5555",
"isAdmin":true,
"secret": "shhhhh",
"skills": {
"sports": [],
"plumbing": true,
"eating": 1,
"abilities": {
"teleportation": true,
"breathingFire": [1, 2, 3]
},
"abilitiesPointer": {
"teleportation": true,
"breathingFire": [4, 5, 6],
"thunder": true
},
"allAbilities": [
{
"teleportation": true,
"breathingFire": [1, 2, 3]
},
{
"teleportation": true,
"breathingFire": [4, 5, 6]
}
]
}
},{
"__key":2,
"id":2,
"email": "[email protected]",
"phoneNumber": "555-5555",
"isAdmin":true,
"secret": "shhhhh",
"skills": {
"sports": [],
"plumbing": true,
"eating": 1,
"abilities": {
"teleportation": true,
"breathingFire": [1, 2, 3]
},
"abilitiesPointer": {
"teleportation": true,
"breathingFire": [4, 5, 6],
"thunder": true
},
"allAbilities": [
{
"teleportation": true,
"breathingFire": [1, 2, 3]
},
{
"teleportation": true,
"breathingFire": [4, 5, 6]
}
]
}
}
]
}`,
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
ctx := context.Background()
runAndValidateQueryResults(t, ctx, e, testCase.Query, testCase.Output)
})
}
}

func TestExecutorQueriesPagination(t *testing.T) {
e, _, _, _, err := createExecutorWithFederatedUser()
require.NoError(t, err)
Expand Down
14 changes: 2 additions & 12 deletions federation/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,18 +298,8 @@ func (e *Planner) planObject(typ *graphql.Object, selectionSet *graphql.Selectio
// id (federatedKey)
// }
selections := make([]*graphql.Selection, 0, len(typ.Fields))
for name, field := range typ.Fields {
for service := range field.FederatedKey {
if len(selectionsByService[service]) > 0 {
selections = append(selections, &graphql.Selection{
Name: name,
Alias: name,
UnparsedArgs: map[string]interface{}{},
})
break
}
}
}
ssResult := getFederatedSelectionsForObject(typ, service, selectionsByService)
selections = ssResult.Selections

federatedSelection := &graphql.Selection{
Name: federationField,
Expand Down
Loading