parent
27778a8360
commit
81595c8261
@ -0,0 +1,49 @@ |
|||||||
|
require("isomorphic-fetch") |
||||||
|
import { ApolloServer } from "apollo-server-express" |
||||||
|
import { getConnection } from "typeorm" |
||||||
|
import { bootstrap } from "./app" |
||||||
|
import { connectionOptionsforTesting } from "./app/schema" |
||||||
|
import { signToken } from "./app/userResolver/auth" |
||||||
|
import { User } from "./app/userResolver/User" |
||||||
|
|
||||||
|
let server: ApolloServer |
||||||
|
let port: number |
||||||
|
|
||||||
|
beforeAll(async () => { |
||||||
|
port = 4001 |
||||||
|
server = await bootstrap(connectionOptionsforTesting(), port) |
||||||
|
}) |
||||||
|
|
||||||
|
describe("app should", () => { |
||||||
|
it("accept auth header correctly", async () => { |
||||||
|
const user = await User.create({ |
||||||
|
email: "email@email.com", |
||||||
|
}).save() |
||||||
|
|
||||||
|
let token = signToken({ userId: user.id }) |
||||||
|
|
||||||
|
const url = `http://localhost:${port}/graphql` |
||||||
|
|
||||||
|
const rawResponse = await fetch(url, { |
||||||
|
method: "post", |
||||||
|
headers: { |
||||||
|
"content-Type": "application/json", |
||||||
|
authorization: "Bearer " + token, |
||||||
|
}, |
||||||
|
body: JSON.stringify({ |
||||||
|
query: "{me{email}}", |
||||||
|
}), |
||||||
|
}) |
||||||
|
|
||||||
|
const response = await rawResponse.json() |
||||||
|
|
||||||
|
expect(response.errors).toBeUndefined() |
||||||
|
expect(response.data).toMatchObject({ |
||||||
|
me: { email: user.email }, |
||||||
|
}) |
||||||
|
|
||||||
|
await getConnection().synchronize(true) |
||||||
|
await getConnection().close() |
||||||
|
await server.stop() |
||||||
|
}) |
||||||
|
}) |
@ -1,25 +1,23 @@ |
|||||||
require("dotenv").config() |
|
||||||
import { ApolloServer } from "apollo-server-express" |
|
||||||
import { createConnection } from "typeorm" |
|
||||||
import { connectionOptionsforDB, createSchema } from "./app/schema" |
|
||||||
import express = require("express") |
import express = require("express") |
||||||
;(async () => { |
import { ApolloServer } from "apollo-server-express" |
||||||
await createConnection(connectionOptionsforDB()) |
import { ConnectionOptions, createConnection } from "typeorm" |
||||||
|
import { createSchema } from "./app/schema" |
||||||
|
import { contextFunction } from "./app/userResolver/auth" |
||||||
|
|
||||||
|
export const bootstrap = async (connectionOptions: ConnectionOptions, port: number) => { |
||||||
|
await createConnection(connectionOptions) |
||||||
|
|
||||||
const server = new ApolloServer({ |
const server = new ApolloServer({ |
||||||
schema: await createSchema(), |
schema: await createSchema(), |
||||||
playground: true, |
playground: true, |
||||||
introspection: true, |
introspection: true, |
||||||
debug: true, |
debug: true, |
||||||
context: ({ req, res }) => ({ req, res }), |
context: contextFunction, |
||||||
}) |
}) |
||||||
|
|
||||||
const app = express() |
const app = express() |
||||||
server.applyMiddleware({ app }) |
server.applyMiddleware({ app }) |
||||||
|
app.listen({ port }) |
||||||
|
|
||||||
app.listen({ port: process.env.APP_PORT }, () => |
return server |
||||||
console.log( |
} |
||||||
`Server ready at http://localhost:${process.env.APP_PORT}${server.graphqlPath}` |
|
||||||
) |
|
||||||
) |
|
||||||
})() |
|
||||||
|
@ -0,0 +1,18 @@ |
|||||||
|
require("dotenv").config() |
||||||
|
import { ConnectionOptions } from "typeorm" |
||||||
|
import { bootstrap } from "./app" |
||||||
|
import { User } from "./app/userResolver/User" |
||||||
|
|
||||||
|
let connectionOptions: ConnectionOptions = { |
||||||
|
type: "postgres", |
||||||
|
host: process.env.DB_HOST, |
||||||
|
port: 5432, |
||||||
|
database: process.env.DB_NAME, |
||||||
|
username: process.env.DB_USER, |
||||||
|
password: process.env.DB_PASS, |
||||||
|
entities: [User], |
||||||
|
synchronize: true, |
||||||
|
logging: false, |
||||||
|
} |
||||||
|
|
||||||
|
bootstrap(connectionOptions, 4000) |
Loading…
Reference in new issue