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

__resolveReference follow-ups (version-0.x) #1762

Merged
merged 1 commit into from
Apr 22, 2022
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
31 changes: 17 additions & 14 deletions federation-integration-testsuite-js/src/fixtures/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ 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
Expand Down Expand Up @@ -130,21 +133,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 +157,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 +180,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 @@ -119,7 +119,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 @@ -137,7 +137,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
2 changes: 1 addition & 1 deletion gateway-js/src/__tests__/executeQueryPlan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { buildComposedSchema, QueryPlanner } from '@apollo/query-planner';
import { ApolloGateway } from '..';
import { ApolloServerBase as ApolloServer } from 'apollo-server-core';
import { getFederatedTestingSchema } from './execution-utils';
import { addResolversToSchema, GraphQLResolverMap } from '../schema-helper';
import { addResolversToSchema, GraphQLResolverMap } from '@apollo/subgraph/src/schema-helper';

expect.addSnapshotSerializer(astSerializer);
expect.addSnapshotSerializer(queryPlanSerializer);
Expand Down
2 changes: 1 addition & 1 deletion gateway-js/src/__tests__/execution-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
GraphQLSchemaModule,
GraphQLResolverMap,
} from '../schema-helper';
} from '@apollo/subgraph/src/schema-helper';
import { GraphQLRequest, GraphQLExecutionResult } from 'apollo-server-types';
import type { Logger } from '@apollo/utils.logger';
import {
Expand Down
2 changes: 1 addition & 1 deletion gateway-js/src/__tests__/gateway/endToEnd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fetch from 'node-fetch';
import { ApolloGateway } from '../..';
import { fixtures } from 'apollo-federation-integration-testsuite';
import { ApolloServerPluginInlineTrace } from 'apollo-server-core';
import { GraphQLSchemaModule } from '../../schema-helper';
import { GraphQLSchemaModule } from '@apollo/subgraph/src/schema-helper';

async function startFederatedServer(modules: GraphQLSchemaModule[]) {
const schema = buildSubgraphSchema(modules);
Expand Down
2 changes: 1 addition & 1 deletion gateway-js/src/__tests__/gateway/reporting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Report, Trace } from 'apollo-reporting-protobuf';
import { fixtures } from 'apollo-federation-integration-testsuite';
import { nockAfterEach, nockBeforeEach } from '../nockAssertions';
import resolvable, { Resolvable } from '@josephg/resolvable';
import { GraphQLSchemaModule } from '../../schema-helper';
import { GraphQLSchemaModule } from '@apollo/subgraph/src/schema-helper';

// Normalize specific fields that change often (eg timestamps) to static values,
// to make snapshot testing viable. (If these helpers are more generally
Expand Down
4 changes: 2 additions & 2 deletions gateway-js/src/__tests__/integration/complex-key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ const userService: ServiceDefinitionModule = {
);
},
organization(user) {
return { id: user.organizationId };
return organizations.find(org => org.id === user.organizationId);
},
},
Organization: {
__resolveObject(object) {
__resolveReference(object) {
return organizations.find(org => org.id === object.id);
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LocalGraphQLDataSource } from '../LocalGraphQLDataSource';
import { buildSubgraphSchema } from '@apollo/subgraph';
import gql from 'graphql-tag';
import { GraphQLResolverMap } from '../../schema-helper';
import { GraphQLResolverMap } from '@apollo/subgraph/src/schema-helper';
import { GraphQLRequestContext } from 'apollo-server-types';
import { GraphQLDataSourceRequestKind } from '../types';

Expand Down
109 changes: 0 additions & 109 deletions gateway-js/src/schema-helper/addResolversToSchema.ts

This file was deleted.

2 changes: 0 additions & 2 deletions gateway-js/src/schema-helper/index.ts

This file was deleted.

23 changes: 0 additions & 23 deletions gateway-js/src/schema-helper/resolverMap.ts

This file was deleted.

17 changes: 9 additions & 8 deletions subgraph-js/src/schema-helper/buildSchemaFromSDL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function addResolversToSchema(

if (isAbstractType(type)) {
for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
if (fieldName === "__resolveReference") {
if (fieldName === '__resolveReference') {
type.extensions = {
...type.extensions,
apollo: {
Expand All @@ -117,11 +117,11 @@ export function addResolversToSchema(
subgraph: {
...type.extensions?.apollo?.subgraph,
resolveReference: fieldConfig,
}
},
},
};
} else if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
} else if (fieldName === '__resolveType') {
type.resolveType = fieldConfig;
}
}
}
Expand Down Expand Up @@ -166,7 +166,7 @@ export function addResolversToSchema(
const fieldMap = type.getFields();

for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
if (fieldName === "__resolveReference") {
if (fieldName === '__resolveReference') {
type.extensions = {
...type.extensions,
apollo: {
Expand All @@ -176,11 +176,12 @@ export function addResolversToSchema(
subgraph: {
...type.extensions?.apollo?.subgraph,
resolveReference: fieldConfig,
}
},
},
};
} else if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
continue;
} else if (fieldName === '__isTypeOf') {
type.isTypeOf = fieldConfig;
continue;
}

Expand Down