Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions library/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"follow-redirects": "^1.15.6",
"got": "^14.6.0",
"graphql": "^16.8.2",
"graphql-v15": "npm:graphql@^15.10.1",
"hono": "^4.4.2",
"koa-router": "^13.0.1",
"koa-v2": "npm:koa@^2.16.1",
Expand Down
174 changes: 0 additions & 174 deletions library/sources/GraphQL.schema.test.ts

This file was deleted.

188 changes: 188 additions & 0 deletions library/sources/GraphQL.schema.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import * as t from "tap";
import { ReportingAPIForTesting } from "../agent/api/ReportingAPIForTesting";
import { Context, getContext, runWithContext } from "../agent/Context";
import { startTestAgent } from "../helpers/startTestAgent";
import { GraphQL } from "./GraphQL";
import { Token } from "../agent/api/Token";

function getTestContext(): Context {
return {
remoteAddress: "::1",
method: "POST",
url: "http://localhost:4000/graphql",
query: {},
headers: {
"content-type": "application/json",
},
body: {
query: '{ getFile(path: "/etc/bashrc") }',
},
cookies: {},
routeParams: {},
source: "express",
route: "/graphql",
};
}

export function createGraphQLTests(pkgName: string) {
Comment thread
hansott marked this conversation as resolved.
function expectedDSL(dsl: string): string {
if (pkgName === "graphql-v15") {
// GraphQL v15 printSchema adds a new line at the end
return dsl + "\n";
}

return dsl;
}

t.test("it works", async () => {
const api = new ReportingAPIForTesting();
const agent = startTestAgent({
api: api,
token: new Token("123"),
wrappers: [new GraphQL()],
rewrite: {
graphql: pkgName,
},
});

const { graphql, buildSchema } = require(
pkgName
) as typeof import("graphql");

const dsl = `
type Mutation {
createBook(title: String!, authorId: ID!): Book!
createAuthor(name: String!): Author!
}

type Query {
getBook(id: ID!): Book
getAuthor(id: ID!): Author
}

type Book {
id: ID!
title: String!
author: Author!
}

type Author {
id: ID!
name: String!
books: [Book!]!
}`.trimStart();

const schema = buildSchema(dsl);

const root = {
getBook: ({ id }: { id: string }) => {
Comment thread
hansott marked this conversation as resolved.
return {
id: id,
title: "Book Title",
author: {
id: "1",
name: "Author Name",
books: [],
},
};
},
};

const query = async (variableValues: Record<string, unknown>) => {
return await graphql({
schema,
source: `
query getBook($id: ID!) {
getBook(id: $id) {
id
title
author {
id
name
books {
id
title
}
}
}
}
`,
rootValue: root,
variableValues: variableValues,
});
};

api.clear();

const hits = 3;

await runWithContext(getTestContext(), async () => {
for (let i = 0; i < hits; i++) {
await query({ id: "1" });

// Route is registered at the end of the request
agent.onRouteExecute(getTestContext());
}
});

await runWithContext(
{
remoteAddress: "::1",
method: "POST",
url: "http://localhost:4000/server-rendered-page",
query: {},
headers: {},
body: {
query: "this is not a graphql query",
},
cookies: {},
routeParams: {},
source: "express",
route: "/server-rendered-page",
},
async () => {
await query({ id: "1" });

// Route is registered at the end of the request
agent.onRouteExecute(getContext()!);
}
);

await agent.flushStats(1000);

t.same(api.getEvents().length, 1);
const [heartbeat] = api.getEvents();
t.match(heartbeat, {
type: "heartbeat",
routes: [
{
method: "POST",
path: "/graphql",
hits: hits,
graphql: {
type: "query",
name: "getBook",
},
apispec: {},
graphQLSchema: undefined,
},
{
method: "POST",
path: "/graphql",
hits: hits,
graphql: undefined,
apispec: {},
graphQLSchema: expectedDSL(dsl),
},
{
method: "POST",
path: "/server-rendered-page",
hits: 1,
graphql: undefined,
apispec: {},
graphQLSchema: undefined,
},
],
});
});
}
Loading
Loading