Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Mercurius to latest Fed 2 tests #256

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
212 changes: 151 additions & 61 deletions implementations/mercurius/index.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,163 @@
const Fastify = require('fastify')
const mercurius = require('mercurius')
const fs = require('fs');
const gql = require('graphql-tag');
const {buildSubgraphSchema} = require('@apollo/subgraph')
const Fastify = require("fastify");
const mercurius = require("mercurius");
const fs = require("fs");
const gql = require("graphql-tag");
const { buildSubgraphSchema } = require("@apollo/subgraph");


const sdl = fs.readFileSync('products.graphql', 'utf-8');
const typeDefs = gql(sdl)
const deprecatedProduct = {
sku: "apollo-federation-v1",
package: "@apollo/federation-v1",
reason: "Migrate to Federation V2",
};

const products = [
{
id: 'apollo-federation',
sku: 'federation',
package: '@apollo/federation',
variation: 'OSS',
{
id: "apollo-federation",
sku: "federation",
package: "@apollo/federation",
variation: "OSS",
},
{
id: "apollo-studio",
sku: "studio",
package: "",
variation: "platform",
},
];

const productResearch = [
{
study: {
caseNumber: "1234",
description: "Federation Study",
},
{
id: 'apollo-studio',
sku: 'studio',
package: '',
variation: 'platform',
},
{
study: {
caseNumber: "1235",
description: "Studio Study",
},
];
},
];

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

//Resolvers copied from implementions/apollo-server, no change.
const resolvers = {
Query: {
/** @type {(_: any, args: any, context: any) => any} */
product: (_, args, context) => {
return products.find((p) => p.id == args.id);
},
const sdl = fs.readFileSync("products.graphql", "utf-8");
const typeDefs = gql(sdl);

const resolvers = {
Query: {
product: (_, args, context) => {
return products.find((p) => p.id == args.id);
},
Product: {
/** @type {(reference: any) => any} */
variation: (reference) => {
if (reference.variation) return { id: reference.variation };
return { id: products.find((p) => p.id == reference.id)?.variation };
},
dimensions: () => {
return { size: 'small', weight: 1 };
},
createdBy: () => {
return { email: 'support@apollographql.com', totalProductsCreated: 1337 };
},
/** @type {(reference: any) => any} */
__resolveReference: (reference) => {
if (reference.id) return products.find((p) => p.id == reference.id);
else if (reference.sku && reference.package)
return products.find(
(p) => p.sku == reference.sku && p.package == reference.package
);
else
return products.find(
(p) => p.sku == reference.sku && p.variation == reference.variation.id
);
},
deprecatedProduct: (_, args, context) => {
if (
args.sku === deprecatedProduct.sku &&
args.package === deprecatedProduct.package
) {
return deprecatedProduct;
} else {
return null;
}
},
};

},
DeprecatedProduct: {
createdBy: () => {
return user;
},
__resolveReference: (reference) => {
if (
reference.sku === deprecatedProduct.sku &&
reference.package === deprecatedProduct.package
) {
return deprecatedProduct;
} else {
return null;
}
},
},
Product: {
variation: (reference) => {
if (reference.variation) return { id: reference.variation };
return { id: products.find((p) => p.id == reference.id)?.variation };
},
dimensions: () => {
return { size: "small", weight: 1, unit: "kg" };
},
research: (reference) => {
if (reference.id === "apollo-federation") {
return [productResearch[0]];
} else if (reference.id === "apollo-studio") {
return [productResearch[1]];
} else {
return [];
}
},
createdBy: () => {
return user;
},
__resolveReference: (reference) => {
if (reference.id) return products.find((p) => p.id == reference.id);
else if (reference.sku && reference.package)
return products.find(
(p) => p.sku == reference.sku && p.package == reference.package
);
else
return products.find(
(p) => p.sku == reference.sku && p.variation == reference.variation.id
);
},
},
ProductResearch: {
__resolveReference: (reference) => {
return productResearch.find(
(p) => reference.study.caseNumber === p.study.caseNumber
);
},
},
User: {
averageProductsCreatedPerYear: (user, args, context) => {
if (user.email != "support@apollographql.com") {
throw new GraphQLError(
"user.email was not 'support@apollographql.com'"
);
}
return Math.round(user.totalProductsCreated / user.yearsOfEmployment);
},
__resolveReference: (reference) => {
if (reference.email) {
const user = {
email: reference.email,
name: "Jane Smith",
totalProductsCreated: 1337,
};
if (reference.totalProductsCreated) {
user.totalProductsCreated = reference.totalProductsCreated;
}
if (reference.yearsOfEmployment) {
user.yearsOfEmployment = reference.yearsOfEmployment;
}
return user;
} else {
return null;
}
},
},
};

const schema = buildSubgraphSchema([{ typeDefs, resolvers }]);
const app = Fastify()
const app = Fastify();

app.register(mercurius, {schema, path: '/'})
app.register(mercurius, { schema, path: "/" });

app.listen(process.env.PRODUCTS_PORT || 4001, '0.0.0.0', (err, url) => {
if (err) {
fastify.log.error(err)
process.exit(1)
}
console.log(`🚀 Server ready at ${url}`);
})
app.listen(process.env.PRODUCTS_PORT || 4001, "0.0.0.0", (err, url) => {
if (err) {
app.log.error(err);
process.exit(1);
}
console.log(`🚀 Server ready at ${url}`);
});