From fa2aa6cd39b6462271c2949737fc79f8f4e5982a Mon Sep 17 00:00:00 2001 From: jbaxleyiii Date: Thu, 22 Aug 2019 21:17:56 -0400 Subject: [PATCH 1/5] Support older input formats for building a schema from typeDefs where typeDefs could be either an array of DocumentNodes or a single DocumenNode attached to an object with a possible resovler map. This shouldn't be supported in the next major version of ApolloServer when we have a more structured approach to modules, but for now this makes migration to federated services much easier for teams that have an existing set of typeDefs generated from their project (often in .graphql files). --- CHANGELOG.md | 2 + .../__tests__/buildFederatedSchema.test.ts | 125 +++++++++++++++++- .../src/service/buildFederatedSchema.ts | 40 +++++- 3 files changed, 164 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd132d08fcc..b001c70b1e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ The version headers in this history reflect the versions of Apollo Server itself - _Nothing yet!_ +- `@apollo/federation`: `buildFederatedSchema` now accepts the same possible interface combination as `new ApolloServer()` for `typeDefs` and `resolvers` to make the migration from a single service into a federated service easier for teams with existing `typeDefs` arrays already built up [PR #3188](https://github.com/apollographql/apollo-server/pull/3188) + ### v2.8.2 > [See complete versioning details.](https://github.com/apollographql/apollo-server/commit/99f78c6782bce170186ba6ef311182a8c9f281b7) diff --git a/packages/apollo-federation/src/service/__tests__/buildFederatedSchema.test.ts b/packages/apollo-federation/src/service/__tests__/buildFederatedSchema.test.ts index d09f250d900..f55ef310754 100644 --- a/packages/apollo-federation/src/service/__tests__/buildFederatedSchema.test.ts +++ b/packages/apollo-federation/src/service/__tests__/buildFederatedSchema.test.ts @@ -1,5 +1,5 @@ import gql from 'graphql-tag'; -import { Kind, graphql } from 'graphql'; +import { Kind, graphql, DocumentNode, execute } from 'graphql'; import { buildFederatedSchema } from '../buildFederatedSchema'; import { typeSerializer } from '../../snapshotSerializers'; @@ -478,3 +478,126 @@ extend type User @key(fields: "email") { }); }); }); + +describe('legacy interface', () => { + const resolvers = { + Query: { + product: () => ({}), + }, + Product: { + upc: () => '1234', + price: () => 10, + }, + }; + const typeDefs: DocumentNode[] = [ + gql` + type Query { + product: Product + } + type Product @key(fields: "upc") { + upc: String! + name: String + } + `, + gql` + extend type Product { + price: Int + } + `, + ]; + it('allows legacy schema module interface as an input with an array of typeDefs and resolvers', async () => { + const schema = buildFederatedSchema({ typeDefs, resolvers }); + expect(schema.getType('_Entity')).toMatchInlineSnapshot( + `union _Entity = Product`, + ); + expect( + await execute( + schema, + gql` + { + product { + price + upc + } + } + `, + ), + ).toEqual({ + data: { + product: { upc: '1234', price: 10 }, + }, + }); + }); + it('allows legacy schema module interface as a single module', async () => { + const schema = buildFederatedSchema({ + typeDefs: gql` + type Query { + product: Product + } + type Product @key(fields: "upc") { + upc: String! + name: String + price: Int + } + `, + resolvers, + }); + expect(schema.getType('_Entity')).toMatchInlineSnapshot( + `union _Entity = Product`, + ); + expect( + await execute( + schema, + gql` + { + product { + price + upc + } + } + `, + ), + ).toEqual({ + data: { + product: { upc: '1234', price: 10 }, + }, + }); + }); + it('allows legacy schema module interface as a single module without resolvers', async () => { + const schema = buildFederatedSchema({ + typeDefs: gql` + type Query { + product: Product + } + type Product @key(fields: "upc") { + upc: String! + name: String + price: Int + } + `, + }); + expect(schema.getType('Product')).toMatchInlineSnapshot(` +type Product { + upc: String! + name: String + price: Int +} +`); + expect(schema.getType('_Entity')).toMatchInlineSnapshot( + `union _Entity = Product`, + ); + }); + it('allows legacy schema module interface as a simple array of documents', async () => { + const schema = buildFederatedSchema({ typeDefs }); + expect(schema.getType('Product')).toMatchInlineSnapshot(` +type Product { + upc: String! + name: String + price: Int +} +`); + expect(schema.getType('_Entity')).toMatchInlineSnapshot( + `union _Entity = Product`, + ); + }); +}); diff --git a/packages/apollo-federation/src/service/buildFederatedSchema.ts b/packages/apollo-federation/src/service/buildFederatedSchema.ts index 40454297668..c5ade708ef1 100644 --- a/packages/apollo-federation/src/service/buildFederatedSchema.ts +++ b/packages/apollo-federation/src/service/buildFederatedSchema.ts @@ -13,6 +13,7 @@ import { GraphQLSchemaModule, modulesFromSDL, addResolversToSchema, + GraphQLResolverMap, } from 'apollo-graphql'; import federationDirectives, { typeIncludesDirective } from '../directives'; @@ -22,10 +23,45 @@ import { printSchema } from './printFederatedSchema'; import 'apollo-server-env'; +type LegacySchemaModule = { + typeDefs: DocumentNode | DocumentNode[]; + resolvers?: GraphQLResolverMap; +}; + export function buildFederatedSchema( - modulesOrSDL: (GraphQLSchemaModule | DocumentNode)[] | DocumentNode, + modulesOrSDL: + | (GraphQLSchemaModule | DocumentNode)[] + | DocumentNode + | LegacySchemaModule, ): GraphQLSchema { - const modules = modulesFromSDL(modulesOrSDL); + // ApolloServer supports passing an array of DocumentNode along with a single + // map of resolvers to build a schema. Long term we don't want to support this + // style anymore as we move towards a more structured approach to modules, + // however, it has tripped several teams up to not support this signature + // in buildFederatedSchema. Especially as teams migrate from + // `new ApolloServer({ typeDefs: DocumentNode[], resolvers })` to + // `new ApolloServer({ schema: buildFederatedSchema({ typeDefs: DocumentNode[], resolvers }) })` + // + // The last type in the union for `modulesOrSDL` supports this "legacy" input + // style in a simple manner (by just adding the resolvers to the first typeDefs entry) + // + let shappedModulesOrSDL: + | (GraphQLSchemaModule | DocumentNode)[] + | DocumentNode; + if ('typeDefs' in modulesOrSDL) { + const { typeDefs, resolvers } = modulesOrSDL; + const augmentedTypeDefs = Array.isArray(typeDefs) ? typeDefs : [typeDefs]; + shappedModulesOrSDL = augmentedTypeDefs.map((typeDefs, i) => { + const module: GraphQLSchemaModule = { typeDefs }; + // add the resolvers to the first "module" in the array + if (i === 0 && resolvers) module.resolvers = resolvers; + return module; + }); + } else { + shappedModulesOrSDL = modulesOrSDL; + } + + const modules = modulesFromSDL(shappedModulesOrSDL); let schema = buildSchemaFromSDL( modules, From 4bd1f901dbeceb69638703cfa724576666fc2e31 Mon Sep 17 00:00:00 2001 From: Jesse Rosenberger Date: Fri, 23 Aug 2019 13:24:27 +0300 Subject: [PATCH 2/5] Adjust spelling of variable. --- .../apollo-federation/src/service/buildFederatedSchema.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/apollo-federation/src/service/buildFederatedSchema.ts b/packages/apollo-federation/src/service/buildFederatedSchema.ts index c5ade708ef1..10b6f76fe1f 100644 --- a/packages/apollo-federation/src/service/buildFederatedSchema.ts +++ b/packages/apollo-federation/src/service/buildFederatedSchema.ts @@ -45,23 +45,23 @@ export function buildFederatedSchema( // The last type in the union for `modulesOrSDL` supports this "legacy" input // style in a simple manner (by just adding the resolvers to the first typeDefs entry) // - let shappedModulesOrSDL: + let shapedModulesOrSDL: | (GraphQLSchemaModule | DocumentNode)[] | DocumentNode; if ('typeDefs' in modulesOrSDL) { const { typeDefs, resolvers } = modulesOrSDL; const augmentedTypeDefs = Array.isArray(typeDefs) ? typeDefs : [typeDefs]; - shappedModulesOrSDL = augmentedTypeDefs.map((typeDefs, i) => { + shapedModulesOrSDL = augmentedTypeDefs.map((typeDefs, i) => { const module: GraphQLSchemaModule = { typeDefs }; // add the resolvers to the first "module" in the array if (i === 0 && resolvers) module.resolvers = resolvers; return module; }); } else { - shappedModulesOrSDL = modulesOrSDL; + shapedModulesOrSDL = modulesOrSDL; } - const modules = modulesFromSDL(shappedModulesOrSDL); + const modules = modulesFromSDL(shapedModulesOrSDL); let schema = buildSchemaFromSDL( modules, From bf2f27f341d404d0bf481ac50c54716fa1c66938 Mon Sep 17 00:00:00 2001 From: Jesse Rosenberger Date: Fri, 23 Aug 2019 13:25:29 +0300 Subject: [PATCH 3/5] Add period on end of sentence in CHANGELOG.md. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b001c70b1e8..943a0676752 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ The version headers in this history reflect the versions of Apollo Server itself - _Nothing yet!_ -- `@apollo/federation`: `buildFederatedSchema` now accepts the same possible interface combination as `new ApolloServer()` for `typeDefs` and `resolvers` to make the migration from a single service into a federated service easier for teams with existing `typeDefs` arrays already built up [PR #3188](https://github.com/apollographql/apollo-server/pull/3188) +- `@apollo/federation`: `buildFederatedSchema` now accepts the same possible interface combination as `new ApolloServer()` for `typeDefs` and `resolvers` to make the migration from a single service into a federated service easier for teams with existing `typeDefs` arrays already built up. [PR #3188](https://github.com/apollographql/apollo-server/pull/3188) ### v2.8.2 From ab73a3cedd845f9db6561abe2fcc5899d7ebca41 Mon Sep 17 00:00:00 2001 From: James Baxley Date: Fri, 23 Aug 2019 06:39:47 -0400 Subject: [PATCH 4/5] Update CHANGELOG.md Co-Authored-By: Jesse Rosenberger --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 943a0676752..db7bba32b8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ The version headers in this history reflect the versions of Apollo Server itself - _Nothing yet!_ -- `@apollo/federation`: `buildFederatedSchema` now accepts the same possible interface combination as `new ApolloServer()` for `typeDefs` and `resolvers` to make the migration from a single service into a federated service easier for teams with existing `typeDefs` arrays already built up. [PR #3188](https://github.com/apollographql/apollo-server/pull/3188) +- `@apollo/federation`: `buildFederatedSchema`'s `typeDefs` parameter now accepts arrays of `DocumentNode`s (i.e. type definitions wrapped in `gql`) and `resolvers` to make the migration from a single service into a federated service easier for teams previously utilizing this pattern. [PR #3188](https://github.com/apollographql/apollo-server/pull/3188) ### v2.8.2 From d6b78d0cb93940675b193627695b04d2ceb59ae7 Mon Sep 17 00:00:00 2001 From: Jesse Rosenberger Date: Fri, 23 Aug 2019 13:39:47 +0300 Subject: [PATCH 5/5] Oh Prettier, I'm coming for you. Removing the extra letter from this variable necessitates changing all the things, apparently. --- .../apollo-federation/src/service/buildFederatedSchema.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/apollo-federation/src/service/buildFederatedSchema.ts b/packages/apollo-federation/src/service/buildFederatedSchema.ts index 10b6f76fe1f..16481e0902b 100644 --- a/packages/apollo-federation/src/service/buildFederatedSchema.ts +++ b/packages/apollo-federation/src/service/buildFederatedSchema.ts @@ -45,9 +45,7 @@ export function buildFederatedSchema( // The last type in the union for `modulesOrSDL` supports this "legacy" input // style in a simple manner (by just adding the resolvers to the first typeDefs entry) // - let shapedModulesOrSDL: - | (GraphQLSchemaModule | DocumentNode)[] - | DocumentNode; + let shapedModulesOrSDL: (GraphQLSchemaModule | DocumentNode)[] | DocumentNode; if ('typeDefs' in modulesOrSDL) { const { typeDefs, resolvers } = modulesOrSDL; const augmentedTypeDefs = Array.isArray(typeDefs) ? typeDefs : [typeDefs];