Skip to content

Commit

Permalink
tests: stop using undocumented __resolveObject AS feature
Browse files Browse the repository at this point in the history
Apollo Server 2.2.1 introduced an experimental feature named
__resolveObject. It was like an extra-powerful version of
__resolveReference (which did not yet exist) which also allowed you to
return references within a single subgraph and they'd be magically
converted into full objects. It was never documented and was described
multiple times as experimental.

Its implementation is entirely inside the Apollo Server schema
instrumentation, which is largely designed for enabling willResolveField
plugins (eg, usage reporting). In fact, as the developer of the main
feature in AS 3.6.0
(apollographql/apollo-server#5963) I added an
optimization to not call schema instrumentation at all if there are no
willResolveField plugin hooks... which means that this feature is broken
in that case!

We are likely to remove this feature in AS4. To get ready for that, stop
using it in tests here. This involves changing the existing ones to
`__resolveReference` and also adding some direct "object resolution" to
resolvers that return objects within the "owning" service.

This is an improvement anyway, so that our fixtures use the documented
`__resolveReference` feature instead of a different obscure feature.
  • Loading branch information
glasser committed Mar 29, 2022
1 parent 51dcda5 commit bbedca3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
53 changes: 26 additions & 27 deletions federation-integration-testsuite-js/src/fixtures/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@ export const url = `https://${name}.api.com.invalid`;
export const typeDefs = gql`
directive @stream on FIELD
directive @transform(from: String!) on FIELD
directive @tag(name: String!) repeatable on
| FIELD_DEFINITION
| INTERFACE
| OBJECT
| UNION
| ARGUMENT_DEFINITION
| SCALAR
| ENUM
| ENUM_VALUE
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
directive @tag(
name: String!
) repeatable on FIELD_DEFINITION | INTERFACE | OBJECT | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
enum CacheControlScope @tag(name: "from-reviews") {
PUBLIC @tag(name: "from-reviews")
Expand All @@ -29,7 +21,9 @@ export const typeDefs = gql`
inheritMaxAge: Boolean
) on FIELD_DEFINITION | OBJECT | INTERFACE | UNION
scalar JSON @tag(name: "from-reviews") @specifiedBy(url: "https://json-spec.dev")
scalar JSON
@tag(name: "from-reviews")
@specifiedBy(url: "https://json-spec.dev")
schema {
query: RootQuery
Expand Down Expand Up @@ -57,11 +51,16 @@ export const typeDefs = gql`
description: String
}
type User @key(fields: "id") @key(fields: "username name { first last }") @tag(name: "from-accounts") {
type User
@key(fields: "id")
@key(fields: "username name { first last }")
@tag(name: "from-accounts") {
id: ID! @tag(name: "accounts")
name: Name @cacheControl(inheritMaxAge: true)
username: String @shareable # Provided by the 'reviews' subgraph
birthDate(locale: String @tag(name: "admin")): String @tag(name: "admin") @tag(name: "dev")
birthDate(locale: String @tag(name: "admin")): String
@tag(name: "admin")
@tag(name: "dev")
account: AccountType
metadata: [UserMetadata]
ssn: String
Expand Down Expand Up @@ -130,21 +129,21 @@ const libraryUsers: { [name: string]: string[] } = {
export const resolvers: GraphQLResolverMap<any> = {
RootQuery: {
user(_, args) {
return { id: args.id };
return users.find((user) => user.id === args.id);
},

me() {
return { id: '1' };
return users.find((user) => user.id === '1');
},
},
User: {
__resolveObject(object) {
__resolveReference(object) {
// Nested key example for @key(fields: "username name { first last }")
if (object.username && object.name.first && object.name.last) {
users.find(user => user.username === object.username);
users.find((user) => user.username === object.username);
}

return users.find(user => user.id === object.id);
return users.find((user) => user.id === object.id);
},
birthDate(user, args) {
return args.locale
Expand All @@ -154,20 +153,20 @@ export const resolvers: GraphQLResolverMap<any> = {
: user.birthDate;
},
metadata(object) {
const metaIndex = metadata.findIndex(m => m.id === object.id);
return metadata[metaIndex].metadata.map(obj => ({ name: obj.name }));
const metaIndex = metadata.findIndex((m) => m.id === object.id);
return metadata[metaIndex].metadata.map((obj) => ({ name: obj.name }));
},
},
UserMetadata: {
address(object) {
const metaIndex = metadata.findIndex(m =>
m.metadata.find(o => o.name === object.name),
const metaIndex = metadata.findIndex((m) =>
m.metadata.find((o) => o.name === object.name),
);
return metadata[metaIndex].metadata[0].address;
},
description(object) {
const metaIndex = metadata.findIndex(m =>
m.metadata.find(o => o.name === object.name),
const metaIndex = metadata.findIndex((m) =>
m.metadata.find((o) => o.name === object.name),
);
return metadata[metaIndex].metadata[0].description;
},
Expand All @@ -177,13 +176,13 @@ export const resolvers: GraphQLResolverMap<any> = {
const libraryUserIds = libraryUsers[name];
return libraryUserIds &&
libraryUserIds.find((id: string) => id === userId)
? { id: userId }
? users.find((user) => user.id === userId)
: null;
},
},
Mutation: {
login(_, args) {
return users.find(user => user.username === args.username);
return users.find((user) => user.username === args.username);
},
},
};
4 changes: 2 additions & 2 deletions federation-integration-testsuite-js/src/fixtures/books.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const books = [

export const resolvers: GraphQLResolverMap<any> = {
Book: {
__resolveObject(object) {
__resolveReference(object) {
return books.find(book => book.isbn === object.isbn);
},
similarBooks(object) {
Expand All @@ -124,7 +124,7 @@ export const resolvers: GraphQLResolverMap<any> = {
},
Query: {
book(_, args) {
return { isbn: args.isbn };
return books.find(book => book.isbn === args.isbn);
},
books() {
return books;
Expand Down
7 changes: 1 addition & 6 deletions gateway-js/src/__tests__/integration/complex-key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ const userService: ServiceDefinitionModule = {
);
},
organization(user) {
return { id: user.organizationId };
},
},
Organization: {
__resolveObject(object) {
return organizations.find(org => org.id === object.id);
return organizations.find(org => org.id === user.organizationId);
},
},
},
Expand Down

0 comments on commit bbedca3

Please sign in to comment.