Make the schema into a subgraph

This commit is contained in:
Trevor Blades
2022-02-11 11:23:25 -08:00
parent 05abb7092a
commit d5b12425c4
5 changed files with 105 additions and 90 deletions

View File

@@ -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
});

40
package-lock.json generated
View File

@@ -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",

View File

@@ -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"
},

View File

@@ -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}) {

64
schema.graphql Normal file
View File

@@ -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
}