-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
310 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 4 additions & 56 deletions
60
examples/federation-example/services/accounts/index.ts
100755 → 100644
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,6 @@ | ||
import { ApolloServer, gql } from 'apollo-server'; | ||
import { accountsServer } from './server'; | ||
|
||
const typeDefs = gql` | ||
type Query { | ||
me: User | ||
user(id: ID!): User | ||
users: [User] | ||
} | ||
type User { | ||
id: ID! | ||
name: String | ||
username: String | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
Query: { | ||
me(_root, _args, context) { | ||
return context.users[0]; | ||
}, | ||
users(_root, _args, context) { | ||
return context.users; | ||
}, | ||
user(_root, args, context) { | ||
return context.users.find(user => user.id === args.id); | ||
}, | ||
}, | ||
}; | ||
|
||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
context: { | ||
users: [ | ||
{ | ||
id: '1', | ||
name: 'Ada Lovelace', | ||
birthDate: '1815-12-10', | ||
username: '@ada', | ||
}, | ||
{ | ||
id: '2', | ||
name: 'Alan Turing', | ||
birthDate: '1912-06-23', | ||
username: '@complete', | ||
}, | ||
], | ||
}, | ||
accountsServer().catch(error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); | ||
|
||
export const accountsServer = () => | ||
server.listen({ port: 9871 }).then(({ url }) => { | ||
if (!process.env.CI) { | ||
console.log(`🚀 Server ready at ${url}`); | ||
} | ||
return server; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { ApolloServer, gql } from 'apollo-server'; | ||
|
||
const typeDefs = gql` | ||
type Query { | ||
me: User | ||
user(id: ID!): User | ||
users: [User] | ||
} | ||
type User { | ||
id: ID! | ||
name: String | ||
username: String | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
Query: { | ||
me(_root, _args, context) { | ||
return context.users[0]; | ||
}, | ||
users(_root, _args, context) { | ||
return context.users; | ||
}, | ||
user(_root, args, context) { | ||
return context.users.find(user => user.id === args.id); | ||
}, | ||
}, | ||
}; | ||
|
||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
context: { | ||
users: [ | ||
{ | ||
id: '1', | ||
name: 'Ada Lovelace', | ||
birthDate: '1815-12-10', | ||
username: '@ada', | ||
}, | ||
{ | ||
id: '2', | ||
name: 'Alan Turing', | ||
birthDate: '1912-06-23', | ||
username: '@complete', | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
export const accountsServer = () => | ||
server.listen({ port: 9871 }).then(({ url }) => { | ||
if (!process.env.CI) { | ||
console.log(`🚀 Server ready at ${url}`); | ||
} | ||
return server; | ||
}); |
54 changes: 4 additions & 50 deletions
54
examples/federation-example/services/inventory/index.ts
100755 → 100644
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,6 @@ | ||
import { ApolloServer, gql } from 'apollo-server'; | ||
import { buildSubgraphSchema } from '@apollo/subgraph'; | ||
import { inventoryServer } from './server'; | ||
|
||
const typeDefs = gql` | ||
extend type Product @key(fields: "upc") { | ||
upc: String! @external | ||
weight: Int @external | ||
price: Int @external | ||
inStock: Boolean | ||
shippingEstimate: Int @requires(fields: "price weight") | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
Product: { | ||
__resolveReference(object) { | ||
return { | ||
...object, | ||
...inventory.find(product => product.upc === object.upc), | ||
}; | ||
}, | ||
shippingEstimate(object) { | ||
// free for expensive items | ||
if (object.price > 1000) return 0; | ||
// estimate is based on weight | ||
return object.weight * 0.5; | ||
}, | ||
}, | ||
}; | ||
|
||
const server = new ApolloServer({ | ||
schema: buildSubgraphSchema([ | ||
{ | ||
typeDefs, | ||
resolvers, | ||
}, | ||
]), | ||
inventoryServer().catch(error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); | ||
|
||
export const inventoryServer = () => | ||
server.listen({ port: 9872 }).then(({ url }) => { | ||
if (!process.env.CI) { | ||
console.log(`🚀 Server ready at ${url}`); | ||
} | ||
return server; | ||
}); | ||
|
||
const inventory = [ | ||
{ upc: '1', inStock: true }, | ||
{ upc: '2', inStock: false }, | ||
{ upc: '3', inStock: true }, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { ApolloServer, gql } from 'apollo-server'; | ||
import { buildSubgraphSchema } from '@apollo/subgraph'; | ||
|
||
const typeDefs = gql` | ||
extend type Product @key(fields: "upc") { | ||
upc: String! @external | ||
weight: Int @external | ||
price: Int @external | ||
inStock: Boolean | ||
shippingEstimate: Int @requires(fields: "price weight") | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
Product: { | ||
__resolveReference(object) { | ||
return { | ||
...object, | ||
...inventory.find(product => product.upc === object.upc), | ||
}; | ||
}, | ||
shippingEstimate(object) { | ||
// free for expensive items | ||
if (object.price > 1000) return 0; | ||
// estimate is based on weight | ||
return object.weight * 0.5; | ||
}, | ||
}, | ||
}; | ||
|
||
const server = new ApolloServer({ | ||
schema: buildSubgraphSchema([ | ||
{ | ||
typeDefs, | ||
resolvers, | ||
}, | ||
]), | ||
}); | ||
|
||
export const inventoryServer = () => | ||
server.listen({ port: 9872 }).then(({ url }) => { | ||
if (!process.env.CI) { | ||
console.log(`🚀 Server ready at ${url}`); | ||
} | ||
return server; | ||
}); | ||
|
||
const inventory = [ | ||
{ upc: '1', inStock: true }, | ||
{ upc: '2', inStock: false }, | ||
{ upc: '3', inStock: true }, | ||
]; |
71 changes: 4 additions & 67 deletions
71
examples/federation-example/services/products/index.ts
100755 → 100644
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,6 @@ | ||
import { ApolloServer, gql } from 'apollo-server'; | ||
import { buildSubgraphSchema } from '@apollo/subgraph'; | ||
import { productsServer } from './server'; | ||
|
||
const typeDefs = gql` | ||
extend type Query { | ||
topProducts(first: Int = 5): [Product] | ||
} | ||
type Product @key(fields: "upc") { | ||
upc: String! | ||
name: String | ||
price: Int | ||
weight: Int | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
Product: { | ||
__resolveReference(object) { | ||
return { | ||
...object, | ||
...products.find(product => product.upc === object.upc), | ||
}; | ||
}, | ||
}, | ||
Query: { | ||
topProducts(_, args) { | ||
return products.slice(0, args.first); | ||
}, | ||
}, | ||
}; | ||
|
||
const server = new ApolloServer({ | ||
schema: buildSubgraphSchema([ | ||
{ | ||
typeDefs, | ||
resolvers, | ||
}, | ||
]), | ||
productsServer().catch(error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); | ||
|
||
export const productsServer = () => | ||
server.listen({ port: 9873 }).then(({ url }) => { | ||
if (!process.env.CI) { | ||
console.log(`🚀 Server ready at ${url}`); | ||
} | ||
return server; | ||
}); | ||
|
||
const products = [ | ||
{ | ||
upc: '1', | ||
name: 'Table', | ||
price: 899, | ||
weight: 100, | ||
}, | ||
{ | ||
upc: '2', | ||
name: 'Couch', | ||
price: 1299, | ||
weight: 1000, | ||
}, | ||
{ | ||
upc: '3', | ||
name: 'Chair', | ||
price: 54, | ||
weight: 50, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { ApolloServer, gql } from 'apollo-server'; | ||
import { buildSubgraphSchema } from '@apollo/subgraph'; | ||
|
||
const typeDefs = gql` | ||
extend type Query { | ||
topProducts(first: Int = 5): [Product] | ||
} | ||
type Product @key(fields: "upc") { | ||
upc: String! | ||
name: String | ||
price: Int | ||
weight: Int | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
Product: { | ||
__resolveReference(object) { | ||
return { | ||
...object, | ||
...products.find(product => product.upc === object.upc), | ||
}; | ||
}, | ||
}, | ||
Query: { | ||
topProducts(_, args) { | ||
return products.slice(0, args.first); | ||
}, | ||
}, | ||
}; | ||
|
||
const server = new ApolloServer({ | ||
schema: buildSubgraphSchema([ | ||
{ | ||
typeDefs, | ||
resolvers, | ||
}, | ||
]), | ||
}); | ||
|
||
export const productsServer = () => | ||
server.listen({ port: 9873 }).then(({ url }) => { | ||
if (!process.env.CI) { | ||
console.log(`🚀 Server ready at ${url}`); | ||
} | ||
return server; | ||
}); | ||
|
||
const products = [ | ||
{ | ||
upc: '1', | ||
name: 'Table', | ||
price: 899, | ||
weight: 100, | ||
}, | ||
{ | ||
upc: '2', | ||
name: 'Couch', | ||
price: 1299, | ||
weight: 1000, | ||
}, | ||
{ | ||
upc: '3', | ||
name: 'Chair', | ||
price: 54, | ||
weight: 50, | ||
}, | ||
]; |
Oops, something went wrong.