Skip to content

Commit

Permalink
feat: remove Sanity from School type
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc committed Aug 26, 2023
1 parent de26e81 commit 2e9ac3f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
9 changes: 1 addition & 8 deletions src/app/(public)/about/work/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import WorkExperience from '@/components/organisms/about-work/WorkExperience';
import { jobsQueryQL } from '@/queries/jobs';
import { languagesQueryQL } from '@/queries/languages';
import { publicationQueryQL } from '@/queries/publications';
import { schoolsQueryQL } from '@/queries/schools';
import { querySchools } from '@/queries/schools';
import { softwareDevIntroQuery } from '@/queries/short-texts';
import { skillQueryQL } from '@/queries/skills';

Expand All @@ -24,7 +24,6 @@ import { Icon } from '@/types/Icon';
import { Job } from '@/types/Job';
import { Language } from '@/types/Language';
import { Publication } from '@/types/Publication';
import { School } from '@/types/School';
import { SoftwareDevIntro } from '@/types/ShortText';
import { Skill } from '@/types/Skill';

Expand All @@ -51,12 +50,6 @@ async function queryPublications() {
return flattenToArray<Publication>(data.publications);
}

async function querySchools() {
const { data } = await apolloClient.query({ query: schoolsQueryQL });

return flattenToArray<School>(data.schools);
}

async function querySkills() {
const { data } = await apolloClient.query({ query: skillQueryQL });

Expand Down
12 changes: 12 additions & 0 deletions src/lib/graphqlUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { DocumentNode } from 'graphql/language';
import {
flattenEntityResponse,
flattenEntityResponseCollection,
StrapiEntityResponse,
StrapiEntityResponseCollection,
} from 'strapi-flatten-graphql';

import apolloClient from '../../apollo/apollo-client';

export function flattenToArray<T extends object>(
entityResponseCollection: StrapiEntityResponseCollection<T>
): T[] {
Expand All @@ -20,3 +23,12 @@ export function flattenToObject<T extends object>(
const flattenedData = flattenEntityResponse(entityResponse);
return flattenedData as T;
}

export async function queryData<T>(
query: DocumentNode,
dataKey: string
): Promise<T[]> {
const { data } = await apolloClient.query({ query });

return flattenToArray<T>(data[dataKey]);
}
6 changes: 2 additions & 4 deletions src/pages/api/schools.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { NextApiRequest, NextApiResponse } from 'next';

import { schoolsQuery } from '@/queries/schools';
import { querySchools } from '@/queries/schools';
import { School } from '@/sanityTypes/School';

import { sanityClient } from '../../../sanity/lib/client';

const schoolsApi = async (req: NextApiRequest, res: NextApiResponse) => {
const schools: School[] = await sanityClient.fetch(schoolsQuery);
const schools: School[] = await querySchools();

res.status(200).json(schools);
};
Expand Down
28 changes: 12 additions & 16 deletions src/queries/schools.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { gql } from '@apollo/client';
import { groq } from 'next-sanity';

export const schoolsQuery = groq`
*[_type == "school"] | order(endYear desc) {
_id,
name,
schoolName,
"schoolIcon": schoolIcon.asset->url,
"flagUrl": countryFlag.asset->url,
degreeName,
degreeUrl,
grade,
startYear,
endYear,
description,
}`;
import { flattenToArray } from '@/lib/graphqlUtils';

export const schoolsQueryQL = gql`
import apolloClient from '../../apollo/apollo-client';

import { School } from '@/types/School';

export async function querySchools() {
const { data } = await apolloClient.query({ query: schoolsQuery });

return flattenToArray<School>(data.schools);
}

const schoolsQuery = gql`
query {
schools(locale: "en", sort: "start:DESC") {
data {
Expand Down

0 comments on commit 2e9ac3f

Please sign in to comment.