Skip to content

Commit

Permalink
feat(apps/gql): add RafineQuery with metadata query & resolver (#699)
Browse files Browse the repository at this point in the history
# Description
introducing Rafine as a MVP. Kinda still WIP.

Please describe your changes. Be descriptive enough to reduce churn for
review process.

### Checklist

- [x] discord username: `csirin`
- [x] Closes #643 
- [x] Closes #644 
- [x] Closes #645 
- [ ] PR must be created for an issue from issues under "In progress"
column from [our project
board](https://github.com/orgs/kamp-us/projects/2/views/1).
- [x] A descriptive and understandable title: The PR title should
clearly describe the nature and purpose of the changes. The PR title
should be the first thing displayed when the PR is opened. And it should
follow the semantic commit rules, and should include the
app/package/service name in the title. For example, a title like
"docs(@kampus-apps/pano): Add README.md" can be used.
- [x] Related file selection: Only relevant files should be touched and
no other files should be affected.
- [ ] I ran `npx turbo run` at the root of the repository, and build was
successful.
- [x] I installed the npm packages using `npm install --save-exact
<package>` so my package is pinned to a specific npm version. Leave
empty if no package was installed. Leave empty if no package was
installed with this PR.

### How were these changes tested?

Please describe the tests you did to test the changes you made. Please
also specify your test configuration.

---------

Co-authored-by: Can Sirin <[email protected]>
Co-authored-by: Umut Sirin <[email protected]>
  • Loading branch information
3 people authored Dec 29, 2023
1 parent 69feccf commit 80173d5
Show file tree
Hide file tree
Showing 7 changed files with 7,496 additions and 6,277 deletions.
2 changes: 2 additions & 0 deletions apps/gql/loaders/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type Clients } from "~/clients";
import { createOdinLoaders } from "~/loaders/odin";
import { createPanoLoaders } from "./pano";
import { createRafineLoaders } from "./rafine";
import { createSozlukLoaders } from "./sozluk";
import { createUserLoaders } from "./user";

Expand All @@ -12,5 +13,6 @@ export const createLoaders = (clients: Clients) => {
sozluk: createSozlukLoaders(),
pano: createPanoLoaders(clients),
odin: createOdinLoaders(),
rafine: createRafineLoaders(),
};
};
30 changes: 30 additions & 0 deletions apps/gql/loaders/rafine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { parser, type Metadata } from "html-metadata-parser";

import { createDataLoader } from "~/../../packages/gql-utils";
import { type RafineMetadata } from "~/schema/types.generated";

export const createRafineLoaders = () => {
return {
metadata: createMetadataLoader(),
};
};

const createMetadataLoader = () => {
const parse = createDataLoader<string, Metadata>(
async (urls) => await Promise.all(urls.map((url) => parser(url)))
);

return {
parse,
};
};

export const transformMetadata = (metadata: Metadata) => {
return {
...metadata,
description: metadata.meta.description || "",
title: metadata.meta.title || "",
url: metadata.meta.url || "",
__typename: "RafineMetadata",
} satisfies RafineMetadata;
};
1 change: 1 addition & 0 deletions apps/gql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"graphql-config": "4.5.0",
"graphql-scalars": "1.21.3",
"graphql-yoga": "3.9.1",
"html-metadata-parser": "2.0.4",
"object-hash": "3.0.0",
"svix": "1.15.0",
"znv": "0.3.2",
Expand Down
13 changes: 13 additions & 0 deletions apps/gql/schema/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
transformPanoPostConnection,
transformPanoUpvote,
} from "~/loaders/pano";
import { transformMetadata } from "~/loaders/rafine";
import { transformSozlukTerm, transformSozlukTermsConnection } from "~/loaders/sozluk";
import { transformUser } from "~/loaders/user";
import { type Resolvers, type ResolversInterfaceTypes } from "../types.generated";
Expand Down Expand Up @@ -54,6 +55,8 @@ export const resolvers = {
pano: () => ({ post: null, posts: [], postsBySite: null, allPosts: null }),
odin: () => ({ lesson: null, lessons: null }),

rafine: () => ({ metadata: null }),

node: async (_, args, { loaders }) => {
const id = parse<NodeTypename>(args.id);

Expand Down Expand Up @@ -397,6 +400,16 @@ export const resolvers = {
},
},

RafineQuery: {
metadata: async (_, args, { loaders }) =>
transformMetadata(await loaders.rafine.metadata.parse.load(args.input)),
},
RafineMetadata: {
title: (metadata) => metadata.title,
description: (metadata) => metadata.description,
url: (metadata) => metadata.url,
},

PanoPostError: {}, // union
CreatePanoPostPayload: {
edge: (payload) => payload.edge,
Expand Down
13 changes: 13 additions & 0 deletions apps/gql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Query {
sozluk: SozlukQuery!
pano: PanoQuery!
odin: OdinQuery!
rafine: RafineQuery!
}

type PageInfo {
Expand Down Expand Up @@ -335,3 +336,15 @@ type OdinLessonEdge {
cursor: String!
node: OdinLesson
}

### Rafine

type RafineQuery {
metadata(input: String!): RafineMetadata
}

type RafineMetadata {
title: String!
description: String!
url: String!
}
47 changes: 47 additions & 0 deletions apps/gql/schema/types.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export type Query = {
node: Maybe<Node>;
odin: OdinQuery;
pano: PanoQuery;
rafine: RafineQuery;
sozluk: SozlukQuery;
user: Maybe<User>;
viewer: Maybe<Viewer>;
Expand All @@ -303,6 +304,22 @@ export type QueryUserArgs = {
username: InputMaybe<Scalars["String"]["input"]>;
};

export type RafineMetadata = {
__typename?: "RafineMetadata";
description: Scalars["String"]["output"];
title: Scalars["String"]["output"];
url: Scalars["String"]["output"];
};

export type RafineQuery = {
__typename?: "RafineQuery";
metadata: Maybe<RafineMetadata>;
};

export type RafineQueryMetadataArgs = {
input: Scalars["String"]["input"];
};

export type RemovePanoCommentInput = {
id: Scalars["ID"]["input"];
};
Expand Down Expand Up @@ -608,6 +625,8 @@ export type ResolversTypes = ResolversObject<{
PanoUpvote: ResolverTypeWrapper<PanoUpvote>;
PanoUpvoteError: ResolverTypeWrapper<ResolversUnionTypes<ResolversTypes>["PanoUpvoteError"]>;
Query: ResolverTypeWrapper<{}>;
RafineMetadata: ResolverTypeWrapper<RafineMetadata>;
RafineQuery: ResolverTypeWrapper<RafineQuery>;
RemovePanoCommentInput: RemovePanoCommentInput;
RemovePanoCommentPayload: ResolverTypeWrapper<
Omit<RemovePanoCommentPayload, "error"> & { error: Maybe<ResolversTypes["PanoCommentError"]> }
Expand Down Expand Up @@ -682,6 +701,8 @@ export type ResolversParentTypes = ResolversObject<{
PanoUpvote: PanoUpvote;
PanoUpvoteError: ResolversUnionTypes<ResolversParentTypes>["PanoUpvoteError"];
Query: {};
RafineMetadata: RafineMetadata;
RafineQuery: RafineQuery;
RemovePanoCommentInput: RemovePanoCommentInput;
RemovePanoCommentPayload: Omit<RemovePanoCommentPayload, "error"> & {
error: Maybe<ResolversParentTypes["PanoCommentError"]>;
Expand Down Expand Up @@ -1060,11 +1081,35 @@ export type QueryResolvers<
>;
odin: Resolver<ResolversTypes["OdinQuery"], ParentType, ContextType>;
pano: Resolver<ResolversTypes["PanoQuery"], ParentType, ContextType>;
rafine: Resolver<ResolversTypes["RafineQuery"], ParentType, ContextType>;
sozluk: Resolver<ResolversTypes["SozlukQuery"], ParentType, ContextType>;
user: Resolver<Maybe<ResolversTypes["User"]>, ParentType, ContextType, Partial<QueryUserArgs>>;
viewer: Resolver<Maybe<ResolversTypes["Viewer"]>, ParentType, ContextType>;
}>;

export type RafineMetadataResolvers<
ContextType = KampusGQLContext,
ParentType extends ResolversParentTypes["RafineMetadata"] = ResolversParentTypes["RafineMetadata"]
> = ResolversObject<{
description: Resolver<ResolversTypes["String"], ParentType, ContextType>;
title: Resolver<ResolversTypes["String"], ParentType, ContextType>;
url: Resolver<ResolversTypes["String"], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export type RafineQueryResolvers<
ContextType = KampusGQLContext,
ParentType extends ResolversParentTypes["RafineQuery"] = ResolversParentTypes["RafineQuery"]
> = ResolversObject<{
metadata: Resolver<
Maybe<ResolversTypes["RafineMetadata"]>,
ParentType,
ContextType,
RequireFields<RafineQueryMetadataArgs, "input">
>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export type RemovePanoCommentPayloadResolvers<
ContextType = KampusGQLContext,
ParentType extends ResolversParentTypes["RemovePanoCommentPayload"] = ResolversParentTypes["RemovePanoCommentPayload"]
Expand Down Expand Up @@ -1248,6 +1293,8 @@ export type Resolvers<ContextType = KampusGQLContext> = ResolversObject<{
PanoUpvote: PanoUpvoteResolvers<ContextType>;
PanoUpvoteError: PanoUpvoteErrorResolvers<ContextType>;
Query: QueryResolvers<ContextType>;
RafineMetadata: RafineMetadataResolvers<ContextType>;
RafineQuery: RafineQueryResolvers<ContextType>;
RemovePanoCommentPayload: RemovePanoCommentPayloadResolvers<ContextType>;
RemovePanoPostPayload: RemovePanoPostPayloadResolvers<ContextType>;
RemovePanoUpvotePayload: RemovePanoUpvotePayloadResolvers<ContextType>;
Expand Down
Loading

0 comments on commit 80173d5

Please sign in to comment.