feat: add graphql
This commit is contained in:
13
src/graphql-documents/categories.gql.generated.ts
Normal file
13
src/graphql-documents/categories.gql.generated.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/* eslint-disable */
|
||||
/* Generated by scripts/endpoint-generate.mjs — do not edit by hand */
|
||||
import type { ExecutionResult } from "graphql";
|
||||
|
||||
import { execute } from "../graphql/execute";
|
||||
import type { CategoriesListQuery, CategoriesListQueryVariables } from "../graphql/graphql";
|
||||
import { categoriesListDocument } from "./categories.gql";
|
||||
|
||||
export const categoriesListQuery = {
|
||||
async execute(variables?: CategoriesListQueryVariables): Promise<ExecutionResult<CategoriesListQuery>> {
|
||||
return execute<CategoriesListQuery, CategoriesListQueryVariables>(categoriesListDocument, variables);
|
||||
},
|
||||
};
|
||||
14
src/graphql-documents/categories.gql.ts
Normal file
14
src/graphql-documents/categories.gql.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import gql from "graphql-tag";
|
||||
|
||||
export const categoriesListDocument = gql`
|
||||
query CategoriesList {
|
||||
categories {
|
||||
documentId
|
||||
slug
|
||||
title
|
||||
sortOrder
|
||||
group
|
||||
badge
|
||||
}
|
||||
}
|
||||
`;
|
||||
13
src/graphql-documents/home-page.gql.generated.ts
Normal file
13
src/graphql-documents/home-page.gql.generated.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/* eslint-disable */
|
||||
/* Generated by scripts/endpoint-generate.mjs — do not edit by hand */
|
||||
import type { ExecutionResult } from "graphql";
|
||||
|
||||
import { execute } from "../graphql/execute";
|
||||
import type { HomePageQuery, HomePageQueryVariables } from "../graphql/graphql";
|
||||
import { homePageDocument } from "./home-page.gql";
|
||||
|
||||
export const homePageQuery = {
|
||||
async execute(variables?: HomePageQueryVariables): Promise<ExecutionResult<HomePageQuery>> {
|
||||
return execute<HomePageQuery, HomePageQueryVariables>(homePageDocument, variables);
|
||||
},
|
||||
};
|
||||
38
src/graphql-documents/home-page.gql.ts
Normal file
38
src/graphql-documents/home-page.gql.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import gql from "graphql-tag";
|
||||
|
||||
export const homePageDocument = gql`
|
||||
query HomePage {
|
||||
homePage {
|
||||
sections {
|
||||
... on ComponentSectionsHomeHero {
|
||||
title
|
||||
description
|
||||
badge
|
||||
stats {
|
||||
id
|
||||
key
|
||||
value
|
||||
}
|
||||
}
|
||||
... on ComponentSectionsServices {
|
||||
title
|
||||
description
|
||||
cards {
|
||||
id
|
||||
title
|
||||
description
|
||||
color {
|
||||
value
|
||||
}
|
||||
icon {
|
||||
value
|
||||
}
|
||||
category {
|
||||
slug
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
53
src/graphql/execute.ts
Normal file
53
src/graphql/execute.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { DocumentNode, ExecutionResult } from "graphql";
|
||||
import { print } from "graphql";
|
||||
|
||||
import type { TypedDocumentString } from "./graphql";
|
||||
|
||||
const defaultUrl = "http://localhost:1337/graphql";
|
||||
|
||||
function getEndpoint(): string {
|
||||
if (typeof import.meta !== "undefined" && import.meta.env?.PUBLIC_GRAPHQL_URL) {
|
||||
return import.meta.env.PUBLIC_GRAPHQL_URL;
|
||||
}
|
||||
if (typeof process !== "undefined" && process.env.PUBLIC_GRAPHQL_URL) {
|
||||
return process.env.PUBLIC_GRAPHQL_URL;
|
||||
}
|
||||
return defaultUrl;
|
||||
}
|
||||
|
||||
function toQueryString(query: TypedDocumentString<unknown, unknown> | DocumentNode): string {
|
||||
if (typeof query === "object" && query !== null && "kind" in query && query.kind === "Document") {
|
||||
return print(query as DocumentNode);
|
||||
}
|
||||
return String(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* GraphQL over HTTP. Accepts codegen {@link TypedDocumentString} or a `graphql-tag` {@link DocumentNode}.
|
||||
*/
|
||||
export async function execute<TResult, TVariables>(
|
||||
query: TypedDocumentString<TResult, TVariables> | DocumentNode,
|
||||
variables?: TVariables
|
||||
): Promise<ExecutionResult<TResult>> {
|
||||
const payload: { query: string; variables?: TVariables } = {
|
||||
query: toQueryString(query as TypedDocumentString<unknown, unknown> | DocumentNode),
|
||||
};
|
||||
if (variables !== undefined) {
|
||||
payload.variables = variables;
|
||||
}
|
||||
|
||||
const response = await fetch(getEndpoint(), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/graphql-response+json, application/json",
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`GraphQL HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
return response.json() as Promise<ExecutionResult<TResult>>;
|
||||
}
|
||||
1494
src/graphql/graphql.ts
Normal file
1494
src/graphql/graphql.ts
Normal file
File diff suppressed because it is too large
Load Diff
1
src/graphql/index.ts
Normal file
1
src/graphql/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./graphql";
|
||||
Reference in New Issue
Block a user