Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Feb 13, 2023
1 parent 3f4ca5b commit 7edb62f
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 278 deletions.
1 change: 1 addition & 0 deletions examples/federation-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"devDependencies": {
"apollo-server": "3.11.1",
"apollo-server-caching": "3.3.0",
"concurrently": "7.6.0",
"delay-cli": "2.0.0",
"jest": "29.4.2",
Expand Down
60 changes: 4 additions & 56 deletions examples/federation-example/services/accounts/index.ts
100755 → 100644
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;
});
58 changes: 58 additions & 0 deletions examples/federation-example/services/accounts/server.ts
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 examples/federation-example/services/inventory/index.ts
100755 → 100644
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 },
];
52 changes: 52 additions & 0 deletions examples/federation-example/services/inventory/server.ts
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 examples/federation-example/services/products/index.ts
100755 → 100644
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,
},
];
69 changes: 69 additions & 0 deletions examples/federation-example/services/products/server.ts
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,
},
];
Loading

0 comments on commit 7edb62f

Please sign in to comment.