Skip to content

Instantly share code, notes, and snippets.

@danstarns
Last active February 2, 2023 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danstarns/9f79c8dc93901ce0ff7faa3f624a9fdf to your computer and use it in GitHub Desktop.
Save danstarns/9f79c8dc93901ce0ff7faa3f624a9fdf to your computer and use it in GitHub Desktop.
mocking-gql-api
import express from "express"
import { createYoga } from "graphql-yoga";
import { makeExecutableSchema } from "graphql-tools";
const app = express();
// GraphQL Schema
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
// could be apollo server
const yoga = createYoga({
schema,
});
app.use("/graphql", yoga);
// In test file -----------------------------------------
import supertest from "supertest";
describe("test", () => {
test("should call the graphql api", async () => {
const query = `
mutation {
signIn(email: $email, password: $password) {
token
}
}
`;
const variables = {
email,
newPassword,
};
// make a mock call to the server at /graphql
const response = await supertest(app)
.post("/graphql")
.send({ query, variables })
.set("Accept", "application/json")
.expect("Content-Type", /json/);
const body = await response.body;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment