Last active
February 2, 2023 13:05
-
-
Save danstarns/9f79c8dc93901ce0ff7faa3f624a9fdf to your computer and use it in GitHub Desktop.
mocking-gql-api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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