|
| 1 | +import * as t from "tap"; |
| 2 | +import { ReportingAPIForTesting } from "../agent/api/ReportingAPIForTesting"; |
| 3 | +import { Context, getContext, runWithContext } from "../agent/Context"; |
| 4 | +import { startTestAgent } from "../helpers/startTestAgent"; |
| 5 | +import { GraphQL } from "./GraphQL"; |
| 6 | +import { Token } from "../agent/api/Token"; |
| 7 | + |
| 8 | +function getTestContext(): Context { |
| 9 | + return { |
| 10 | + remoteAddress: "::1", |
| 11 | + method: "POST", |
| 12 | + url: "http://localhost:4000/graphql", |
| 13 | + query: {}, |
| 14 | + headers: { |
| 15 | + "content-type": "application/json", |
| 16 | + }, |
| 17 | + body: { |
| 18 | + query: '{ getFile(path: "/etc/bashrc") }', |
| 19 | + }, |
| 20 | + cookies: {}, |
| 21 | + routeParams: {}, |
| 22 | + source: "express", |
| 23 | + route: "/graphql", |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +export function createGraphQLTests(pkgName: string) { |
| 28 | + t.test("it works", async () => { |
| 29 | + const api = new ReportingAPIForTesting(); |
| 30 | + const agent = startTestAgent({ |
| 31 | + api: api, |
| 32 | + token: new Token("123"), |
| 33 | + wrappers: [new GraphQL()], |
| 34 | + rewrite: { |
| 35 | + graphql: pkgName, |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + const { graphql, buildSchema } = require( |
| 40 | + pkgName |
| 41 | + ) as typeof import("graphql"); |
| 42 | + |
| 43 | + const dsl = ` |
| 44 | +type Mutation { |
| 45 | + createBook(title: String!, authorId: ID!): Book! |
| 46 | + createAuthor(name: String!): Author! |
| 47 | +} |
| 48 | +
|
| 49 | +type Query { |
| 50 | + getBook(id: ID!): Book |
| 51 | + getAuthor(id: ID!): Author |
| 52 | +} |
| 53 | +
|
| 54 | +type Book { |
| 55 | + id: ID! |
| 56 | + title: String! |
| 57 | + author: Author! |
| 58 | +} |
| 59 | +
|
| 60 | +type Author { |
| 61 | + id: ID! |
| 62 | + name: String! |
| 63 | + books: [Book!]! |
| 64 | +}`.trimStart(); |
| 65 | + |
| 66 | + const schema = buildSchema(dsl); |
| 67 | + |
| 68 | + const root = { |
| 69 | + getBook: ({ id }: { id: string }) => { |
| 70 | + return { |
| 71 | + id: id, |
| 72 | + title: "Book Title", |
| 73 | + author: { |
| 74 | + id: "1", |
| 75 | + name: "Author Name", |
| 76 | + books: [], |
| 77 | + }, |
| 78 | + }; |
| 79 | + }, |
| 80 | + }; |
| 81 | + |
| 82 | + const query = async (variableValues: Record<string, unknown>) => { |
| 83 | + return await graphql({ |
| 84 | + schema, |
| 85 | + source: ` |
| 86 | + query getBook($id: ID!) { |
| 87 | + getBook(id: $id) { |
| 88 | + id |
| 89 | + title |
| 90 | + author { |
| 91 | + id |
| 92 | + name |
| 93 | + books { |
| 94 | + id |
| 95 | + title |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + `, |
| 101 | + rootValue: root, |
| 102 | + variableValues: variableValues, |
| 103 | + }); |
| 104 | + }; |
| 105 | + |
| 106 | + api.clear(); |
| 107 | + |
| 108 | + const hits = 3; |
| 109 | + |
| 110 | + await runWithContext(getTestContext(), async () => { |
| 111 | + for (let i = 0; i < hits; i++) { |
| 112 | + await query({ id: "1" }); |
| 113 | + |
| 114 | + // Route is registered at the end of the request |
| 115 | + agent.onRouteExecute(getTestContext()); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + await runWithContext( |
| 120 | + { |
| 121 | + remoteAddress: "::1", |
| 122 | + method: "POST", |
| 123 | + url: "http://localhost:4000/server-rendered-page", |
| 124 | + query: {}, |
| 125 | + headers: {}, |
| 126 | + body: { |
| 127 | + query: "this is not a graphql query", |
| 128 | + }, |
| 129 | + cookies: {}, |
| 130 | + routeParams: {}, |
| 131 | + source: "express", |
| 132 | + route: "/server-rendered-page", |
| 133 | + }, |
| 134 | + async () => { |
| 135 | + await query({ id: "1" }); |
| 136 | + |
| 137 | + // Route is registered at the end of the request |
| 138 | + agent.onRouteExecute(getContext()!); |
| 139 | + } |
| 140 | + ); |
| 141 | + |
| 142 | + await agent.flushStats(1000); |
| 143 | + |
| 144 | + t.same(api.getEvents().length, 1); |
| 145 | + const [heartbeat] = api.getEvents(); |
| 146 | + t.match(heartbeat, { |
| 147 | + type: "heartbeat", |
| 148 | + routes: [ |
| 149 | + { |
| 150 | + method: "POST", |
| 151 | + path: "/graphql", |
| 152 | + hits: hits, |
| 153 | + graphql: { |
| 154 | + type: "query", |
| 155 | + name: "getBook", |
| 156 | + }, |
| 157 | + apispec: {}, |
| 158 | + graphQLSchema: undefined, |
| 159 | + }, |
| 160 | + { |
| 161 | + method: "POST", |
| 162 | + path: "/graphql", |
| 163 | + hits: hits, |
| 164 | + graphql: undefined, |
| 165 | + apispec: {}, |
| 166 | + graphQLSchema: dsl, |
| 167 | + }, |
| 168 | + { |
| 169 | + method: "POST", |
| 170 | + path: "/server-rendered-page", |
| 171 | + hits: 1, |
| 172 | + graphql: undefined, |
| 173 | + apispec: {}, |
| 174 | + graphQLSchema: undefined, |
| 175 | + }, |
| 176 | + ], |
| 177 | + }); |
| 178 | + }); |
| 179 | +} |
0 commit comments