23
loading...
This website collects cookies to deliver better user experience
// @src/server.js
const Fastify = require("fastify");
const mercurius = require("mercurius");
const { gql } = require("graphql-request");
const axios = require("axios");
const app = Fastify();
const schema = gql`
type Company {
name: String
catchPhrase: String
bs: String
}
type Geo {
lat: String
lng: String
}
type Address {
street: String
suite: String
city: String
zipcode: String
geo: Geo
}
type User {
id: ID
name: String
username: String
email: String
phone: String
website: String
company: Company
address: Address
}
type Find {
userId: ID
id: ID
title: String
body: String
user: User
}
type Query {
find(id: ID): Find
}
`;
const resolvers = {
Query: {
find: async (root, { id }, ctx) => {
const getUser = axios.get(`https://jsonplaceholder.typicode.com/users/${id}`);
const getPost = axios.get(`https://jsonplaceholder.typicode.com/posts?userId=${id}`);
const promises = await axios.all([getUser, getPost]);
const user = promises[0].data;
const post = promises[1].data[0];
return { ...post, user };
},
},
};
app.register(mercurius, {
schema,
resolvers,
});
async function start(port) {
try {
await app.listen(port, () => {
console.log(`Api running at http://localhost:${port}/graphql`);
});
} catch (err) {
console.error(err);
process.exit();
}
}
start(3333);
// @src/tests/api.test.js
const { test } = require("uvu");
const assert = require("uvu/assert");
test("Should get the post and user data through the given id", async () => {
// Logic goes here
});
test.run();
// @src/tests/api.test.js
const { test } = require("uvu");
const assert = require("uvu/assert");
const { request, gql } = require("graphql-request");
test("Should get the post and user data through the given id", async () => {
// Logic goes here
});
test.run();
ID 1
and then we will get the following data:// @src/tests/api.test.js
const { test } = require("uvu");
const assert = require("uvu/assert");
const { request, gql } = require("graphql-request");
test("Should get the post and user data through the given id", async () => {
const query = gql`
query {
find(id: 1) {
title
body
user {
username
email
address {
street
}
company {
name
}
}
}
}
`;
// More logic goes here
});
test.run();
// @src/tests/api.test.js
const { test } = require("uvu");
const assert = require("uvu/assert");
const { request, gql } = require("graphql-request");
test("Should get the post and user data through the given id", async () => {
const query = gql`
query {
find(id: 1) {
title
body
user {
username
email
address {
street
}
company {
name
}
}
}
}
`;
const { find: data } = await request("http://localhost:3333/graphql", query);
// More logic goes here
});
test.run();
// @src/tests/api.test.js
const { test } = require("uvu");
const assert = require("uvu/assert");
const { request, gql } = require("graphql-request");
test("Should get the post and user data through the given id", async () => {
const query = gql`
query {
find(id: 1) {
title
body
user {
username
email
address {
street
}
company {
name
}
}
}
}
`;
const { find: data } = await request("http://localhost:3333/graphql", query);
assert.instance(data, Object);
assert.instance(data.user, Object);
assert.instance(data.user.address, Object);
// Even more logic goes here
});
test.run();
ID 1
, we know the username must be Bret
and he must live on Kulas Light
street. So we have to check if the response data matches what is expected.// @src/tests/api.test.js
const { test } = require("uvu");
const assert = require("uvu/assert");
const { request, gql } = require("graphql-request");
test("Should get the post and user data through the given id", async () => {
const query = gql`
query {
find(id: 1) {
title
body
user {
username
email
address {
street
}
company {
name
}
}
}
}
`;
const { find: data } = await request("http://localhost:3333/graphql", query);
assert.instance(data, Object);
assert.instance(data.user, Object);
assert.instance(data.user.address, Object);
assert.equal(data.user.username, "Bret");
assert.equal(data.user.address.street, "Kulas Light");
});
test.run();
npm test
command to run the tests and see if it passes. But first you have to run the Api on port 3333 using the npm start
command and only then run the tests. You should get a result similar to the following: