Skip to content

Commit

Permalink
update apollo-server implementation to the latest schema
Browse files Browse the repository at this point in the history
Update `product` schema to allow for more granular testing of `@key` directive functionality.

Related issue:
* apollographql#166
  • Loading branch information
dariuszkuc committed Aug 11, 2022
1 parent bfbedcd commit 1e446c4
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 12 deletions.
72 changes: 68 additions & 4 deletions implementations/apollo-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import { ApolloServer, ApolloError, gql } from 'apollo-server';
import { buildSubgraphSchema } from '@apollo/subgraph';

const port = process.env.PRODUCTS_PORT || 4001;

const deprecatedProduct = {
sku: "apollo-federation-v1",
package: "@apollo/federation-v1",
reason: "Migrate to Federation V2",
};

const products = [
{
id: 'apollo-federation',
Expand All @@ -18,6 +25,27 @@ const products = [
},
];

const productResearch = [
{
study: {
caseNumber: "1234",
description: "Federation Study"
}
},
{
study: {
caseNumber: "1235",
description: "Studio Study"
}
}
]

const user = {
email: "support@apollographql.com",
name: "Jane Smith",
totalProductsCreated: 1337
};

const sdl = readFileSync('products.graphql', 'utf-8');

const typeDefs = gql(sdl);
Expand All @@ -28,6 +56,27 @@ const resolvers = {
product: (_, args, context) => {
return products.find((p) => p.id == args.id);
},
/** @type {(_: any, args: any, context: any) => any} */
deprecatedProduct: (_, args, context) => {
if (args.sku === deprecatedProduct.sku && args.package === deprecatedProduct.package) {
return deprecatedProduct;
} else {
return null;
}
}
},
DeprecatedProduct: {
createdBy: () => {
return user;
},
/** @type {(reference: any) => any} */
__resolveReference: (reference) => {
if (reference.sku === deprecatedProduct.sku && reference.package === deprecatedProduct.package) {
return deprecatedProduct;
} else {
return null;
}
}
},
Product: {
/** @type {(reference: any) => any} */
Expand All @@ -38,8 +87,18 @@ const resolvers = {
dimensions: () => {
return { size: "small", weight: 1, unit: "kg" };
},
/** @type {(reference: any) => any} */
research: (reference) => {
if (reference.id === "apollo-federation") {
return [productResearch[0]];
} else if (reference.id === "apollo-studio") {
return [productResearch[1]];
} else {
return [];
}
},
createdBy: () => {
return { email: 'support@apollographql.com', totalProductsCreated: 1337 };
return user;
},
/** @type {(reference: any) => any} */
__resolveReference: (reference) => {
Expand All @@ -54,10 +113,15 @@ const resolvers = {
);
},
},
ProductResearch: {
/** @type {(reference: any) => any} */
__resolveReference: (reference) => {
return productResearch.find(
(p) => reference.study.caseNumber === p.study.caseNumber
);
}
},
User: {
name() {
return "Jane Smith";
},
/** @type {(user: { email: String, totalProductsCreated: Number, yearsOfEmployment: Number }, args: any, context: any) => any} */
averageProductsCreatedPerYear: (user, args, context) => {
if (user.email != "support@apollographql.com") {
Expand Down
37 changes: 29 additions & 8 deletions implementations/apollo-server/products.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0",
import: ["@key", "@shareable", "@provides", "@external", "@tag", "@extends", "@override", "@inaccessible", "@requires"])
@link(
url: "https://specs.apollo.dev/federation/v2.0",
import: ["@extends", "@external", "@inaccessible", "@key", "@override", "@provides", "@requires", "@shareable", "@tag"]
)

type Product @key(fields: "id") @key(fields: "sku package") @key(fields: "sku variation { id }") {
id: ID!
Expand All @@ -10,23 +12,42 @@ type Product @key(fields: "id") @key(fields: "sku package") @key(fields: "sku va
dimensions: ProductDimension
createdBy: User @provides(fields: "totalProductsCreated")
notes: String @tag(name: "internal")
research: [ProductResearch!]!
}

type ProductDimension @shareable {
size: String
weight: Float
unit: String @inaccessible
type DeprecatedProduct @key(fields: "sku package") {
sku: String!
package: String!
reason: String
createdBy: User
}

type ProductVariation {
id: ID!
}

type Query {
type ProductResearch @key(fields: "study { caseNumber }") {
study: CaseStudy!
outcome: String
}

type CaseStudy {
caseNumber: ID!
description: String
}

type ProductDimension @shareable {
size: String
weight: Float
unit: String @inaccessible
}

extend type Query {
product(id: ID!): Product
deprecatedProduct(sku: String!, package: String!): DeprecatedProduct @deprecated(reason: "Use product query instead")
}

type User @key(fields: "email") @extends {
extend type User @key(fields: "email") {
averageProductsCreatedPerYear: Int @requires(fields: "totalProductsCreated yearsOfEmployment")
email: ID! @external
name: String @override(from: "users")
Expand Down

0 comments on commit 1e446c4

Please sign in to comment.