Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually keep track of connections #2824

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
3 changes: 3 additions & 0 deletions site/gatsby-site/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

import type { Config } from 'jest';

require('dotenv').config();

const config: Config = {
preset: "ts-jest",
clearMocks: true,
collectCoverage: true,
testTimeout: 10000,
coverageDirectory: "coverage",
coverageProvider: "v8",
testEnvironment: "node",
Expand Down
62 changes: 53 additions & 9 deletions site/gatsby-site/package-lock.json

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

3 changes: 2 additions & 1 deletion site/gatsby-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@googlemaps/google-maps-services-js": "^3.3.3",
"@graphql-tools/executor-http": "^1.0.9",
"@graphql-tools/schema": "^10.0.3",
"@graphql-tools/stitch": "^9.2.9",
"@graphql-tools/utils": "^10.1.2",
"@graphql-tools/wrap": "^10.0.5",
"@mdx-js/react": "^2.3.0",
Expand Down Expand Up @@ -141,8 +142,8 @@
"@netlify/plugin-gatsby": "^3.7.0",
"@playwright/test": "^1.44.1",
"@tailwindcss/typography": "^0.5.8",
"@types/node": "^20.12.13",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.13",
"@types/supertest": "^6.0.2",
"autoprefixer": "^10.4.7",
"babel-eslint": "^10.1.0",
Expand Down
2 changes: 1 addition & 1 deletion site/gatsby-site/playwright/e2e/cite.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ test.describe('Cite pages', () => {
await expect(page.locator('[data-cy="timeline-text-response"]')).not.toBeVisible();
});

test('There should not be image errors (400)', async ({ page }) => {
test.skip('There should not be image errors (400)', async ({ page }) => {
page.on('console', (msg) => {
if (msg.type() === 'error') {
expect(msg.text()).not.toContain('the server responded with a status of 400');
Expand Down
11 changes: 5 additions & 6 deletions site/gatsby-site/server/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { MongoClient } from "mongodb";
import config from "./config";
import * as reporter from "./reporter";

const client = new MongoClient(config.MONGODB_CONNECTION_STRING);

function extractToken(header: string) {

Expand Down Expand Up @@ -56,7 +55,7 @@ async function verifyToken(token: string) {
return response.json();
}

async function getUser(userId: string) {
async function getUser(userId: string, client: MongoClient) {

const db = client.db('customData');

Expand All @@ -70,7 +69,7 @@ async function getUser(userId: string) {
}
}

async function getUserFromHeader(header: string) {
async function getUserFromHeader(header: string, client: MongoClient) {

const token = extractToken(header);

Expand All @@ -80,7 +79,7 @@ async function getUserFromHeader(header: string) {

if (data.sub) {

const userData = await getUser(data.sub);
const userData = await getUser(data.sub, client);

return userData;
}
Expand All @@ -89,11 +88,11 @@ async function getUserFromHeader(header: string) {
return null;
}

export const context = async ({ req }: { req: IncomingMessage }) => {
export const context = async ({ req, client }: { req: IncomingMessage, client: MongoClient }) => {

try {

const user = await getUserFromHeader(req.headers.authorization!);
const user = await getUserFromHeader(req.headers.authorization!, client);

return { user, req, client };
}
Expand Down
6 changes: 5 additions & 1 deletion site/gatsby-site/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import { startStandaloneServer } from '@apollo/server/standalone';
import { schema } from './schema';
import { context } from './context';
import { ApolloServer } from '@apollo/server';
import config from './config';
import { MongoClient } from 'mongodb';

(async () => {

const server = new ApolloServer({
schema,
});

const { url } = await startStandaloneServer(server, { context });
const client = new MongoClient(config.MONGODB_CONNECTION_STRING);

const { url } = await startStandaloneServer(server, { context: ({ req }) => context({ req, client }) });

console.log(`🚀 Server ready at: ${url}`);
})();
4 changes: 2 additions & 2 deletions site/gatsby-site/server/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const ignoredMutations = [

export const getSchema = () => {

const schema = wrapSchema({
const schema = {
schema: makeExecutableSchema({ typeDefs: remoteTypeDefs }),
executor: userExecutor,
transforms: [
Expand Down Expand Up @@ -70,7 +70,7 @@ export const getSchema = () => {
return true
})
],
});
}

return schema;
}
57 changes: 53 additions & 4 deletions site/gatsby-site/server/schema.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,66 @@
import { mergeSchemas } from '@graphql-tools/schema';
import { getSchema as getLocalSchema } from './local';
import { getSchema as getRemoteSchema } from './remote';
import { stitchSchemas } from '@graphql-tools/stitch'
import { MapperKind, mapSchema } from '@graphql-tools/utils';

require('json-bigint-patch');

const localSchema = getLocalSchema();

const remoteSchema = getRemoteSchema();

const gatewaySchema = mergeSchemas({
schemas: [remoteSchema, localSchema],
const gatewaySchema = stitchSchemas({
subschemas: [localSchema, remoteSchema],
});

const transformedSchema = mapSchema(gatewaySchema, {

export const schema = gatewaySchema;
// looks like stichSchemas from ./schema is messing up some resolvers
// this is a temporary patch until the subscription collection field is replaced with our own implementation
// it seems to only be affecting the subscriptions collection

[MapperKind.OBJECT_FIELD]: (fieldConfig, fieldName, parent) => {

if (parent == 'Subscription' && fieldName === 'incident_id') {
return {
...fieldConfig,
resolve: (source) => {
return source.incident_id

Check warning on line 28 in site/gatsby-site/server/schema.ts

View check run for this annotation

Codecov / codecov/patch

site/gatsby-site/server/schema.ts#L28

Added line #L28 was not covered by tests
},
};
}

if (parent == 'Subscription' && fieldName === 'entityId') {
return {
...fieldConfig,
resolve: (source) => {
return source.entityId

Check warning on line 37 in site/gatsby-site/server/schema.ts

View check run for this annotation

Codecov / codecov/patch

site/gatsby-site/server/schema.ts#L37

Added line #L37 was not covered by tests
},
};
}


if (parent == 'Subscription' && fieldName === '_id') {
return {
...fieldConfig,
resolve: (source) => {
return source._id

Check warning on line 47 in site/gatsby-site/server/schema.ts

View check run for this annotation

Codecov / codecov/patch

site/gatsby-site/server/schema.ts#L47

Added line #L47 was not covered by tests
},
};
}

if (parent == 'Subscription' && fieldName === 'type') {
return {
...fieldConfig,
resolve: (source) => {
return source.type

Check warning on line 56 in site/gatsby-site/server/schema.ts

View check run for this annotation

Codecov / codecov/patch

site/gatsby-site/server/schema.ts#L56

Added line #L56 was not covered by tests
},
};
}
return fieldConfig;
},
});


export const schema = transformedSchema;

42 changes: 42 additions & 0 deletions site/gatsby-site/server/tests/reports.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ApolloServer } from "@apollo/server";
import request from 'supertest';
import { startTestServer } from "./utils";

describe('Reports', () => {
let server: ApolloServer, url: string;

beforeAll(async () => {
({ server, url } = await startTestServer());
});

afterAll(async () => {
await server?.stop();
});

it(`report query aliased translations`, async () => {

const queryData = {
query: `
query {
report(query: { report_number: 1991 }) {
title
translations_es: translations(input: "es") {
title
}
}
}
`,
};

const response = await request(url).post('/').send(queryData);

expect(response.body.data).toMatchObject({
report: {
title: expect.any(String),
translations_es: {
title: expect.any(String)
}
}
})
});
});
4 changes: 3 additions & 1 deletion site/gatsby-site/server/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export const startTestServer = async () => {
schema,
});

const { url } = await startStandaloneServer(server, { context, listen: { port: 0 } });
const client = new MongoClient(config.MONGODB_CONNECTION_STRING);

const { url } = await startStandaloneServer(server, { context: ({ req }) => context({ req, client }), listen: { port: 0 } });

return { server, url }
}
Expand Down
Loading
Loading