Skip to content

Commit

Permalink
Fix GraphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
captbaritone committed Oct 1, 2024
1 parent debfb53 commit e4ef68d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 54 deletions.
71 changes: 26 additions & 45 deletions packages/skin-database/api/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,42 @@
import { RequestHandler, Router } from "express";
import { graphqlHTTP } from "express-graphql";
import { Router } from "express";
import { createHandler } from "graphql-http/lib/use/express";

import DEFAULT_QUERY from "./defaultQuery";
// import DEFAULT_QUERY from "./defaultQuery";
import { getSchema } from "./schema";

/** @gqlContext */
export type Ctx = Express.Request;

const router = Router();

function getQueryNameFromDocument(document) {
const operationDefinition = document.definitions.find(
(def) => def.kind === "OperationDefinition"
);
if (!operationDefinition) {
return null;
}
return operationDefinition.name?.value;
}

const extensions = ({
variables,
operationName,
context: req,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
document,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
result,
}) => {
const runTime = Date.now() - req.startTime;
const vars = JSON.stringify(variables);
const queryName = operationName ?? getQueryNameFromDocument(document);
// TODO: Log/notify on error.
req.log(
`Handled GraphQL Query: "${queryName}" with variables ${vars} in ${runTime}ms`
);
return { runTime };
};

router.use(
"/",
graphqlHTTP({
createHandler<Ctx>({
schema: getSchema(),
context: (req) => {
return req.raw;
},
/*
graphiql: {
defaultQuery: DEFAULT_QUERY,
},
customFormatErrorFn: (error) => {
console.error(error);
return {
message: error.message,
locations: error.locations,
stack: error.stack ? error.stack.split("\n") : [],
path: error.path,
};
},
extensions,
}) as RequestHandler
},*/
// graphqlHTTP({
// schema: getSchema(),
// graphiql: {
// defaultQuery: DEFAULT_QUERY,
// },
// customFormatErrorFn: (error) => {
// console.error(error);
// return {
// message: error.message,
// locations: error.locations,
// stack: error.stack ? error.stack.split("\n") : [],
// path: error.path,
// };
// },
// extensions,
// }) as RequestHandler
})
);

export default router;
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function mark_skin_nsfw(
}

const _mark_skin_nsfw = requireAuthed(async (md5: string, req: Ctx) => {
req.log(`Approving skin with hash "${md5}"`);
req.log(`Marking skin with hash "${md5}" NSFW`);
const skin = await SkinModel.fromMd5(req.ctx, md5);
if (skin == null) {
return false;
Expand Down
6 changes: 3 additions & 3 deletions packages/skin-database/api/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ export function getSchema(): GraphQLSchema {
}
},
resolve(source, args, context) {
return assertNonNull(mutationApprove_skinResolver(source, args, context));
return assertNonNull(mutationApprove_skinResolver(source, args.md5, context));
}
},
mark_skin_nsfw: {
Expand All @@ -1113,7 +1113,7 @@ export function getSchema(): GraphQLSchema {
}
},
resolve(source, args, context) {
return assertNonNull(mutationMark_skin_nsfwResolver(source, args, context));
return assertNonNull(mutationMark_skin_nsfwResolver(source, args.md5, context));
}
},
reject_skin: {
Expand All @@ -1127,7 +1127,7 @@ export function getSchema(): GraphQLSchema {
}
},
resolve(source, args, context) {
return assertNonNull(mutationReject_skinResolver(source, args, context));
return assertNonNull(mutationReject_skinResolver(source, args.md5, context));
}
},
request_nsfw_review_for_skin: {
Expand Down
3 changes: 2 additions & 1 deletion packages/skin-database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"express-sitemap-xml": "^2.0.0",
"fast-xml-parser": "^4.2.2",
"graphql": "^16.8.1",
"grats": "^0.0.28",
"graphql-http": "^1.22.1",
"grats": "^0.0.29",
"imagemin": "^7.0.0",
"imagemin-optipng": "^7.0.0",
"knex": "^0.21.1",
Expand Down
14 changes: 10 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14846,6 +14846,11 @@ graphql-depth-limit@^1.1.0:
dependencies:
arrify "^1.0.1"

graphql-http@^1.22.1:
version "1.22.1"
resolved "https://registry.yarnpkg.com/graphql-http/-/graphql-http-1.22.1.tgz#3857ac75366e55db189cfe09ade9cc4c4f2cfd09"
integrity sha512-4Jor+LRbA7SfSaw7dfDUs2UBzvWg3cKrykfHRgKsOIvQaLuf+QOcG2t3Mx5N9GzSNJcuqMqJWz0ta5+BryEmXg==

[email protected]:
version "5.12.1"
resolved "https://registry.yarnpkg.com/graphql-ws/-/graphql-ws-5.12.1.tgz#c62d5ac54dbd409cc6520b0b39de374b3d59d0dd"
Expand All @@ -14856,13 +14861,14 @@ [email protected], graphql@^16.8.1, graphql@^16.9.0:
resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.8.1.tgz#1930a965bef1170603702acdb68aedd3f3cf6f07"
integrity sha512-59LZHPdGZVh695Ud9lRzPBVTtlX9ZCV150Er2W43ro37wVof0ctenSaskPPjN7lVTIN8mSZt8PHUNKZuNQUuxw==

grats@^0.0.28:
version "0.0.28"
resolved "https://registry.yarnpkg.com/grats/-/grats-0.0.28.tgz#232fa29cc4387607950e2e408e18c26582586e1b"
integrity sha512-rNsUJv2V2CtjFrMsfYWHz/0v5ndv2zfaEWj5NdzUzEsezQsCu8CTkKFTyz9DIUZfeU32YaScj2a96Q6yXGFpiQ==
grats@^0.0.29:
version "0.0.29"
resolved "https://registry.yarnpkg.com/grats/-/grats-0.0.29.tgz#9efe40da41a90c35c75f7ee4617290623a4318a9"
integrity sha512-6A77DUF+FHeVEWPUr2DENA9M3iCrR7/TbGQdmJGW7mXs6a5QhnDigq8PkQQdliVzE6KIj86SokkRWS4rAljlPg==
dependencies:
commander "^10.0.0"
graphql "^16.9.0"
semver "^7.5.4"
typescript "5.5.4"

gulp-eslint@^3.0.1:
Expand Down

0 comments on commit e4ef68d

Please sign in to comment.