-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apollographql/apollo-server#2841 from apollographq…
…l/jackson/federation-list-type-keys Allow list-type entity @key's Apollo-Orig-Commit-AS: apollographql/apollo-server@4e4aa81
- Loading branch information
Showing
3 changed files
with
125 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import gql from 'graphql-tag'; | ||
import { execute, ServiceDefinitionModule } from '../execution-utils'; | ||
import { astSerializer, queryPlanSerializer } from '../../snapshotSerializers'; | ||
|
||
expect.addSnapshotSerializer(astSerializer); | ||
expect.addSnapshotSerializer(queryPlanSerializer); | ||
|
||
const users = [ | ||
{ id: ['1', '1'], name: 'Trevor Scheer', __typename: 'User' }, | ||
{ id: ['2', '2'], name: 'James Baxley', __typename: 'User' }, | ||
]; | ||
|
||
const reviews = [ | ||
{ id: '1', authorId: ['1', '1'], body: 'Good', __typename: 'Review' }, | ||
{ id: '2', authorId: ['2', '2'], body: 'Bad', __typename: 'Review' }, | ||
]; | ||
|
||
const reviewService: ServiceDefinitionModule = { | ||
name: 'review', | ||
typeDefs: gql` | ||
type Query { | ||
reviews: [Review!]! | ||
} | ||
type Review { | ||
id: ID! | ||
author: User! | ||
body: String! | ||
} | ||
extend type User @key(fields: "id") { | ||
id: [ID!]! @external | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
reviews() { | ||
return reviews; | ||
}, | ||
}, | ||
Review: { | ||
author(review) { | ||
return { | ||
id: review.authorId, | ||
}; | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const listsAreEqual = <T>(as: T[], bs: T[]) => | ||
as.length === bs.length && as.every((a, i) => bs[i] === as[i]); | ||
|
||
const userService: ServiceDefinitionModule = { | ||
name: 'user', | ||
typeDefs: gql` | ||
type User @key(fields: "id") { | ||
id: [ID!]! | ||
name: String! | ||
} | ||
`, | ||
resolvers: { | ||
User: { | ||
__resolveReference(reference) { | ||
return users.find(user => listsAreEqual(user.id, reference.id)); | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
it('fetches data correctly list type @key fields', async () => { | ||
const query = gql` | ||
query Reviews { | ||
reviews { | ||
body | ||
author { | ||
name | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const { data, queryPlan } = await execute([userService, reviewService], { | ||
query, | ||
}); | ||
|
||
expect(data).toEqual({ | ||
reviews: [ | ||
{ body: 'Good', author: { name: 'Trevor Scheer' } }, | ||
{ body: 'Bad', author: { name: 'James Baxley' } }, | ||
], | ||
}); | ||
expect(queryPlan).toMatchInlineSnapshot(` | ||
QueryPlan { | ||
Sequence { | ||
Fetch(service: "review") { | ||
{ | ||
reviews { | ||
body | ||
author { | ||
__typename | ||
id | ||
} | ||
} | ||
} | ||
}, | ||
Flatten(path: "reviews.@.author") { | ||
Fetch(service: "user") { | ||
{ | ||
... on User { | ||
__typename | ||
id | ||
} | ||
} => | ||
{ | ||
... on User { | ||
name | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
} | ||
`); | ||
}); |