From d5b12425c4181f0ee251d24b31c6a6da3fe87380 Mon Sep 17 00:00:00 2001 From: Trevor Blades Date: Fri, 11 Feb 2022 11:23:25 -0800 Subject: [PATCH] Make the schema into a subgraph --- index.js | 11 ++++-- package-lock.json | 40 +++++++++++++------- package.json | 3 +- schema.js => resolvers.js | 77 +++------------------------------------ schema.graphql | 64 ++++++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 90 deletions(-) rename schema.js => resolvers.js (64%) create mode 100644 schema.graphql diff --git a/index.js b/index.js index d45bf9f..abd25b9 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,12 @@ -import {ApolloServer} from 'apollo-server'; -import {resolvers, typeDefs} from './schema.js'; +import {ApolloServer, gql} from 'apollo-server'; +import {buildSubgraphSchema} from '@apollo/subgraph'; +import {readFileSync} from 'fs'; +import {resolvers} from './resolvers.js'; + +const typeDefs = gql(readFileSync('./schema.graphql', 'utf-8')); const server = new ApolloServer({ - typeDefs, - resolvers, + schema: buildSubgraphSchema({typeDefs, resolvers}), introspection: true, playground: true }); diff --git a/package-lock.json b/package-lock.json index 7e001d7..5217eed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,9 +5,10 @@ "packages": { "": { "dependencies": { + "@apollo/subgraph": "^0.3.1", "apollo-server": "^2.25.3", "countries-list": "^2.5.4", - "graphql": "^14.6.0", + "graphql": "^15.8.0", "provinces": "^1.11.0", "sift": "^12.0.0" }, @@ -47,6 +48,17 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.60.tgz", "integrity": "sha512-F0KIgDJfy2nA3zMLmWGKxcH2ZVEtCZXHHdOQs2gSaQ27+lNeEfGxzkIw90aXswATX7AZ33tahPbzy6KAfUreVw==" }, + "node_modules/@apollo/subgraph": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/@apollo/subgraph/-/subgraph-0.3.1.tgz", + "integrity": "sha512-MWqzfZEcvN86iwwgsgkFnkoBvv/wCdw/qyXjayuZ4wwQgVZ8NpFNbUxjRC0MwKNHwBU/ZH1oDHOnC8WbUuUMsg==", + "engines": { + "node": ">=12.13.0 <17.0" + }, + "peerDependencies": { + "graphql": "^15.8.0 || ^16.0.0" + } + }, "node_modules/@apollographql/apollo-tools": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/@apollographql/apollo-tools/-/apollo-tools-0.5.2.tgz", @@ -2901,14 +2913,11 @@ } }, "node_modules/graphql": { - "version": "14.6.0", - "resolved": "https://registry.npmjs.org/graphql/-/graphql-14.6.0.tgz", - "integrity": "sha512-VKzfvHEKybTKjQVpTFrA5yUq2S9ihcZvfJAtsDBBCuV6wauPu1xl/f9ehgVf0FcEJJs4vz6ysb/ZMkGigQZseg==", - "dependencies": { - "iterall": "^1.2.2" - }, + "version": "15.8.0", + "resolved": "https://registry.npmjs.org/graphql/-/graphql-15.8.0.tgz", + "integrity": "sha512-5gghUc24tP9HRznNpV2+FIoq3xKkj5dTQqf4v0CpdPbFVwFkWoxOM+o+2OC9ZSvjEMTjfmG9QT+gcvggTwW1zw==", "engines": { - "node": ">= 6.x" + "node": ">= 10.x" } }, "node_modules/graphql-extensions": { @@ -5571,6 +5580,12 @@ } } }, + "@apollo/subgraph": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/@apollo/subgraph/-/subgraph-0.3.1.tgz", + "integrity": "sha512-MWqzfZEcvN86iwwgsgkFnkoBvv/wCdw/qyXjayuZ4wwQgVZ8NpFNbUxjRC0MwKNHwBU/ZH1oDHOnC8WbUuUMsg==", + "requires": {} + }, "@apollographql/apollo-tools": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/@apollographql/apollo-tools/-/apollo-tools-0.5.2.tgz", @@ -7882,12 +7897,9 @@ "dev": true }, "graphql": { - "version": "14.6.0", - "resolved": "https://registry.npmjs.org/graphql/-/graphql-14.6.0.tgz", - "integrity": "sha512-VKzfvHEKybTKjQVpTFrA5yUq2S9ihcZvfJAtsDBBCuV6wauPu1xl/f9ehgVf0FcEJJs4vz6ysb/ZMkGigQZseg==", - "requires": { - "iterall": "^1.2.2" - } + "version": "15.8.0", + "resolved": "https://registry.npmjs.org/graphql/-/graphql-15.8.0.tgz", + "integrity": "sha512-5gghUc24tP9HRznNpV2+FIoq3xKkj5dTQqf4v0CpdPbFVwFkWoxOM+o+2OC9ZSvjEMTjfmG9QT+gcvggTwW1zw==" }, "graphql-extensions": { "version": "0.15.0", diff --git a/package.json b/package.json index c5d09bd..652800e 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,10 @@ "extends": "@trevorblades" }, "dependencies": { + "@apollo/subgraph": "^0.3.1", "apollo-server": "^2.25.3", "countries-list": "^2.5.4", - "graphql": "^14.6.0", + "graphql": "^15.8.0", "provinces": "^1.11.0", "sift": "^12.0.0" }, diff --git a/schema.js b/resolvers.js similarity index 64% rename from schema.js rename to resolvers.js index 415f828..dd430aa 100644 --- a/schema.js +++ b/resolvers.js @@ -1,74 +1,6 @@ import countriesList from 'countries-list'; import provinces from 'provinces'; import sift from 'sift'; -import {gql} from 'apollo-server'; - -export const typeDefs = gql` - type Continent { - code: ID! - name: String! - countries: [Country!]! - } - - type Country { - code: ID! - name: String! - native: String! - phone: String! - continent: Continent! - capital: String - currency: String - languages: [Language!]! - emoji: String! - emojiU: String! - states: [State!]! - } - - type State { - code: String - name: String! - country: Country! - } - - type Language { - code: ID! - name: String - native: String - rtl: Boolean! - } - - input StringQueryOperatorInput { - eq: String - ne: String - in: [String] - nin: [String] - regex: String - glob: String - } - - input CountryFilterInput { - code: StringQueryOperatorInput - currency: StringQueryOperatorInput - continent: StringQueryOperatorInput - } - - input ContinentFilterInput { - code: StringQueryOperatorInput - } - - input LanguageFilterInput { - code: StringQueryOperatorInput - } - - type Query { - continents(filter: ContinentFilterInput): [Continent!]! - continent(code: ID!): Continent - countries(filter: CountryFilterInput): [Country!]! - country(code: ID!): Country - languages(filter: LanguageFilterInput): [Language!]! - language(code: ID!): Language - } -`; function filterToSift(filter = {}) { return sift( @@ -111,7 +43,8 @@ export const resolvers = { }; }), states: country => - provinces.filter(province => province.country === country.code) + provinces.filter(province => province.country === country.code), + __resolveReference: country => countries[country.code] }, State: { code: state => state.short, @@ -124,10 +57,12 @@ export const resolvers = { .map(([code, country]) => ({ ...country, code - })) + })), + __resolveReference: continent => continents[continent.code] }, Language: { - rtl: language => Boolean(language.rtl) + rtl: language => Boolean(language.rtl), + __resolveReference: language => languages[language.code] }, Query: { continent(parent, {code}) { diff --git a/schema.graphql b/schema.graphql new file mode 100644 index 0000000..b148bd2 --- /dev/null +++ b/schema.graphql @@ -0,0 +1,64 @@ +type Continent @key(fields: "code") { + code: ID! + name: String! + countries: [Country!]! +} + +type Country @key(fields: "code") { + code: ID! + name: String! + native: String! + phone: String! + continent: Continent! + capital: String + currency: String + languages: [Language!]! + emoji: String! + emojiU: String! + states: [State!]! +} + +type State { + code: String + name: String! + country: Country! +} + +type Language @key(fields: "code") { + code: ID! + name: String + native: String + rtl: Boolean! +} + +input StringQueryOperatorInput { + eq: String + ne: String + in: [String] + nin: [String] + regex: String + glob: String +} + +input CountryFilterInput { + code: StringQueryOperatorInput + currency: StringQueryOperatorInput + continent: StringQueryOperatorInput +} + +input ContinentFilterInput { + code: StringQueryOperatorInput +} + +input LanguageFilterInput { + code: StringQueryOperatorInput +} + +type Query { + continents(filter: ContinentFilterInput): [Continent!]! + continent(code: ID!): Continent + countries(filter: CountryFilterInput): [Country!]! + country(code: ID!): Country + languages(filter: LanguageFilterInput): [Language!]! + language(code: ID!): Language +} \ No newline at end of file