3 Jan 2022
const path = require("path");
const { makeExecutableSchema } = require("@graphql-tools/schema");
const { mergeResolvers } = require("@graphql-tools/merge");
const { loadFilesSync } = require("@graphql-tools/load-files");
const resolverFiles = loadFilesSync(path.join(__dirname, "resolvers"));
const typeDefs = /* GraphQL */ `
type Query {
products: [Product!]!
reviews: [Review!]!
}
type Product {
name: String!
price: Int!
}
type Review {
message: String!
author: String!
}
`;
const resolvers = mergeResolvers(resolverFiles);
const schema = makeExecutableSchema({ typeDefs, resolvers });
const resolverFiles = loadFilesSync(path.join(__dirname, "./**/*.resolver.*"));
const faker = require("faker");
module.exports = {
Query: {
products: (parent, args, context, info) =>
Array.from({ length: 10 }, (_, i) => ({
name: faker.commerce.productName(),
price: faker.commerce.price(),
})),
},
};
const faker = require("faker");
module.exports = {
Query: {
reviews: (parent, args, context, info) =>
Array.from({ length: 5 }, () => ({
message: faker.lorem.sentence(),
author: faker.name.findName(),
})),
},
};
const { graphql } = require("graphql");
const { schema } = require("./");
test("should fetch products", async () => {
const result = await graphql({
schema,
source: /* GraphQL */ `
{
products {
name
price
}
}
`,
});
expect(result.data.products).toBeDefined();
expect(result.data.products.length).toBe(10);
});
test("should fetch reviews", async () => {
const result = await graphql({
schema,
source: /* GraphQL */ `
{
reviews {
message
author
}
}
`,
});
expect(result.data.reviews).toBeDefined();
expect(result.data.reviews.length).toBe(5);
});