2024-08-31 02:59:05 +00:00
|
|
|
import config from 'config';
|
2024-08-30 00:28:44 +00:00
|
|
|
import { format } from 'date-fns';
|
|
|
|
|
2024-08-28 00:06:01 +00:00
|
|
|
import {
|
|
|
|
graphql,
|
|
|
|
buildSchema,
|
|
|
|
GraphQLScalarType,
|
|
|
|
} from 'graphql';
|
|
|
|
|
2024-08-29 15:53:54 +00:00
|
|
|
import {
|
|
|
|
scenesSchema,
|
|
|
|
fetchScenesGraphql,
|
|
|
|
fetchScenesByIdGraphql,
|
|
|
|
} from './scenes.js';
|
|
|
|
|
|
|
|
import {
|
|
|
|
entitiesSchema,
|
|
|
|
fetchEntitiesGraphql,
|
|
|
|
fetchEntitiesByIdGraphql,
|
|
|
|
} from './entities.js';
|
2024-06-25 00:43:04 +00:00
|
|
|
|
2024-08-30 00:28:44 +00:00
|
|
|
import {
|
|
|
|
actorsSchema,
|
|
|
|
fetchActorsGraphql,
|
|
|
|
fetchActorsByIdGraphql,
|
|
|
|
} from './actors.js';
|
|
|
|
|
2024-08-31 02:59:05 +00:00
|
|
|
import { verifyKey } from '../auth.js';
|
|
|
|
|
2024-06-25 00:43:04 +00:00
|
|
|
const schema = buildSchema(`
|
|
|
|
type Query {
|
2024-08-29 15:53:54 +00:00
|
|
|
movies(
|
2024-08-28 00:06:01 +00:00
|
|
|
limit: Int = 30
|
2024-08-29 15:53:54 +00:00
|
|
|
): ReleasesResult
|
2024-06-25 00:43:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-28 00:06:01 +00:00
|
|
|
scalar Date
|
|
|
|
|
2024-06-25 00:43:04 +00:00
|
|
|
${scenesSchema}
|
2024-08-30 00:28:44 +00:00
|
|
|
${actorsSchema}
|
2024-08-29 15:53:54 +00:00
|
|
|
${entitiesSchema}
|
2024-06-25 00:43:04 +00:00
|
|
|
`);
|
|
|
|
|
2024-08-28 00:06:01 +00:00
|
|
|
const DateTimeScalar = new GraphQLScalarType({
|
|
|
|
name: 'DateTime',
|
|
|
|
serialize(value) {
|
|
|
|
if (value instanceof Date) {
|
|
|
|
return value.toISOString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-08-30 00:28:44 +00:00
|
|
|
const DateScalar = new GraphQLScalarType({
|
|
|
|
name: 'Date',
|
|
|
|
serialize(value) {
|
|
|
|
if (value instanceof Date) {
|
|
|
|
return format(value, 'yyyy-MM-dd');
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-06-25 00:43:04 +00:00
|
|
|
export async function graphqlApi(req, res) {
|
2024-08-31 02:59:05 +00:00
|
|
|
if (!config.apiAccess.graphqlEnabled) {
|
|
|
|
res.status(404).send();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await verifyKey(req.headers['api-user'], req.headers['api-key'], req);
|
|
|
|
|
2024-06-25 00:43:04 +00:00
|
|
|
const data = await graphql({
|
|
|
|
schema,
|
|
|
|
source: req.body.query,
|
|
|
|
variableValues: req.body.variables,
|
2024-08-28 00:06:01 +00:00
|
|
|
resolvers: {
|
|
|
|
DateTimeScalar,
|
2024-08-30 00:28:44 +00:00
|
|
|
DateScalar,
|
2024-08-28 00:06:01 +00:00
|
|
|
},
|
2024-07-07 19:01:44 +00:00
|
|
|
rootValue: {
|
|
|
|
scenes: async (query) => fetchScenesGraphql(query, req),
|
2024-08-29 15:53:54 +00:00
|
|
|
scene: async (query) => fetchScenesByIdGraphql(query, req),
|
2024-08-30 00:28:44 +00:00
|
|
|
scenesById: async (query) => fetchScenesByIdGraphql(query, req),
|
|
|
|
actors: async (query) => fetchActorsGraphql(query, req),
|
|
|
|
actor: async (query, args, info) => fetchActorsByIdGraphql(query, req, info),
|
|
|
|
actorsById: async (query, args, info) => fetchActorsByIdGraphql(query, req, info),
|
2024-08-29 15:53:54 +00:00
|
|
|
entities: async (query) => fetchEntitiesGraphql(query, req),
|
2024-08-30 00:28:44 +00:00
|
|
|
entity: async (query, args, info) => fetchEntitiesByIdGraphql(query, req, info),
|
|
|
|
entitiesBySlug: async (query, args, info) => fetchEntitiesByIdGraphql(query, req, info),
|
|
|
|
entitiesById: async (query, args, info) => fetchEntitiesByIdGraphql(query, req, info),
|
2024-07-07 19:01:44 +00:00
|
|
|
},
|
2024-06-25 00:43:04 +00:00
|
|
|
});
|
|
|
|
|
2024-08-28 00:06:01 +00:00
|
|
|
// console.log(data);
|
2024-06-25 00:43:04 +00:00
|
|
|
|
|
|
|
res.send(data);
|
|
|
|
}
|