Expanded GraphQL API with scenes entities and actors.

This commit is contained in:
2024-08-30 02:28:44 +02:00
parent 706ccf1ab3
commit edb10c6d1a
8 changed files with 250 additions and 34 deletions

View File

@@ -1,4 +1,7 @@
import { fetchActors } from '../actors.js';
import {
fetchActors,
fetchActorsById,
} from '../actors.js';
export function curateActorsQuery(query) {
return {
@@ -36,3 +39,101 @@ export async function fetchActorsApi(req, res) {
total,
});
}
export const actorsSchema = `
extend type Query {
actors(
query: String
limit: Int! = 30
page: Int! = 1
order: [String]
): ActorsResult
actor(
id: Int!
): Actor
actorsById(
ids: [Int]!
): [Actor]
}
type Country {
alpha2: String
name: String
}
type Location {
country: Country
city: String
state: String
}
type ActorsResult {
nodes: [Actor]
total: Int
}
type Actor {
id: Int!
name: String
slug: String
gender: String
dateOfBirth: Date
age: Int
origin: Location
residence: Location
height: Int
bust: String
hip: Int
waist: Int
naturalBoobs: Boolean
eyes: String
hairColor: String
hasPiercings: Boolean
hasTattoos: Boolean
tattoos: String
piercings: String
scenes: Int
likes: Int
}
`;
function curateGraphqlActor(actor) {
return {
...actor,
age: actor.ageFromBirth,
height: actor.height?.metric,
weight: actor.weight?.metric,
};
}
export async function fetchActorsGraphql(query, _req) {
const {
actors,
total,
} = await fetchActors(query, {
limit: query.limit,
page: query.page,
order: query.order,
aggregateCountries: false,
});
return {
nodes: actors.map((actor) => curateGraphqlActor(actor)),
total,
};
}
export async function fetchActorsByIdGraphql(query, _req, _info) {
const actors = await fetchActorsById([].concat(query.id, query.ids).filter(Boolean));
const curatedActors = actors.map((actor) => curateGraphqlActor(actor));
console.log(actors);
if (query.ids) {
return curatedActors;
}
return curatedActors[0];
}

View File

@@ -1,8 +1,12 @@
import { parseResolveInfo } from 'graphql-parse-resolve-info';
import {
fetchEntities,
fetchEntitiesById,
} from '../entities.js';
import { getIdsBySlug } from '../cache.js';
export async function fetchEntitiesApi(req, res) {
const entities = await fetchEntities(req.query);
@@ -13,25 +17,37 @@ export const entitiesSchema = `
extend type Query {
entities(
query: String
type: String
order: [String]
limit: Int! = 30
page: Int! = 1
): EntitiesResult
entity(
id: Int!
slug: String!
): Entity
entitiesBySlug(
slugs: [String]!
): [Entity]
entitiesById(
ids: [Int]!
): [Entity]
}
type EntitiesResult {
nodes: [Entity]
total: Int
}
type Entity {
id: Int!
name: String
slug: String
url: String
type: String
parent: Entity
children: [Entity]
}
`;
@@ -43,8 +59,17 @@ export async function fetchEntitiesGraphql(query, _req) {
};
}
export async function fetchEntitiesByIdGraphql(query, _req) {
const [entity] = await fetchEntitiesById([query.id]);
export async function fetchEntitiesByIdGraphql(query, req, info) {
const entityIds = query.ids || await getIdsBySlug([].concat(query.slug, query.slugs).filter(Boolean), 'entities');
const parsedContext = parseResolveInfo(info);
return entity;
const entities = await fetchEntitiesById(entityIds, {
includeChildren: Object.hasOwn(parsedContext.fieldsByTypeName.Entity, 'children'),
});
if (query.slugs || query.ids) {
return entities;
}
return entities[0];
}

View File

@@ -1,3 +1,5 @@
import { format } from 'date-fns';
import {
graphql,
buildSchema,
@@ -16,6 +18,12 @@ import {
fetchEntitiesByIdGraphql,
} from './entities.js';
import {
actorsSchema,
fetchActorsGraphql,
fetchActorsByIdGraphql,
} from './actors.js';
const schema = buildSchema(`
type Query {
movies(
@@ -26,6 +34,7 @@ const schema = buildSchema(`
scalar Date
${scenesSchema}
${actorsSchema}
${entitiesSchema}
`);
@@ -40,6 +49,17 @@ const DateTimeScalar = new GraphQLScalarType({
},
});
const DateScalar = new GraphQLScalarType({
name: 'Date',
serialize(value) {
if (value instanceof Date) {
return format(value, 'yyyy-MM-dd');
}
return value;
},
});
export async function graphqlApi(req, res) {
const data = await graphql({
schema,
@@ -47,12 +67,19 @@ export async function graphqlApi(req, res) {
variableValues: req.body.variables,
resolvers: {
DateTimeScalar,
DateScalar,
},
rootValue: {
scenes: async (query) => fetchScenesGraphql(query, req),
scene: async (query) => fetchScenesByIdGraphql(query, req),
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),
entities: async (query) => fetchEntitiesGraphql(query, req),
entity: async (query) => fetchEntitiesByIdGraphql(query, req),
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),
},
});

View File

@@ -85,6 +85,10 @@ export const scenesSchema = `
scene(
id: Int!
): Release
scenesById(
ids: [Int]!
): [Release]
}
type ReleasesAggregate {
@@ -115,12 +119,6 @@ export const scenesSchema = `
movies: [Release]
}
type Actor {
id: Int!
name: String
slug: String
}
type Tag {
id: Int!
name: String
@@ -184,8 +182,6 @@ export async function fetchScenesGraphql(query, req) {
aggregate: false,
}, req.user);
console.log(query);
return {
nodes: scenes,
total,
@@ -197,24 +193,17 @@ export async function fetchScenesGraphql(query, req) {
},
*/
};
/*
return {
scenes,
aggActors,
aggTags,
aggChannels,
limit,
total,
};
*/
}
export async function fetchScenesByIdGraphql(query, req) {
const [scene] = await fetchScenesById([query.id], {
const scenes = await fetchScenesById([].concat(query.id, query.ids).filter(Boolean), {
reqUser: req.user,
includePartOf: true,
});
return scene;
if (query.ids) {
return scenes;
}
return scenes[0];
}