This commit is contained in:
Liam Chan 2025-02-18 12:17:00 +08:00
parent 9ff78b25ce
commit 90f4cfa6ee
5 changed files with 41 additions and 36 deletions

View File

@ -14,9 +14,11 @@ import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/
* Learn more about it here: https://the-guild.dev/graphql/codegen/plugins/presets/preset-client#reducing-bundle-size
*/
type Documents = {
"\n query GetUserProjects {\n demo_users {\n id\n name\n projects {\n demo_projects_id {\n id\n name\n }\n }\n }\n }\n": typeof types.GetUserProjectsDocument,
"\n query get {\n demo_projects {\n id\n }\n }": typeof types.GetDocument,
};
const documents: Documents = {
"\n query GetUserProjects {\n demo_users {\n id\n name\n projects {\n demo_projects_id {\n id\n name\n }\n }\n }\n }\n": types.GetUserProjectsDocument,
"\n query get {\n demo_projects {\n id\n }\n }": types.GetDocument,
};
@ -34,6 +36,10 @@ const documents: Documents = {
*/
export function gql(source: string): unknown;
/**
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function gql(source: "\n query GetUserProjects {\n demo_users {\n id\n name\n projects {\n demo_projects_id {\n id\n name\n }\n }\n }\n }\n"): (typeof documents)["\n query GetUserProjects {\n demo_users {\n id\n name\n projects {\n demo_projects_id {\n id\n name\n }\n }\n }\n }\n"];
/**
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/

View File

@ -3507,10 +3507,16 @@ export type Version_Root_Table_2_Root_Table = {
root_table_id?: Maybe<Scalars['JSON']['output']>;
};
export type GetUserProjectsQueryVariables = Exact<{ [key: string]: never; }>;
export type GetUserProjectsQuery = { __typename?: 'Query', demo_users: Array<{ __typename?: 'demo_users', id: string, name: string, projects?: Array<{ __typename?: 'demo_users_demo_projects', demo_projects_id?: { __typename?: 'demo_projects', id: string, name: string } | null } | null> | null }> };
export type GetQueryVariables = Exact<{ [key: string]: never; }>;
export type GetQuery = { __typename?: 'Query', demo_projects: Array<{ __typename?: 'demo_projects', id: string }> };
export const GetUserProjectsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetUserProjects"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"demo_users"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"projects"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"demo_projects_id"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}}]}}]}}]}}]}}]} as unknown as DocumentNode<GetUserProjectsQuery, GetUserProjectsQueryVariables>;
export const GetDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"get"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"demo_projects"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}}]}}]}}]} as unknown as DocumentNode<GetQuery, GetQueryVariables>;

6
src/apollo-client.ts Normal file
View File

@ -0,0 +1,6 @@
import { ApolloClient, InMemoryCache } from '@apollo/client';
export const client = new ApolloClient({
uri: process.env.GRAPHQL_ENDPOINT_WITH_TOKEN,
cache: new InMemoryCache(),
});

16
src/gql.ts Normal file
View File

@ -0,0 +1,16 @@
import { gql } from "@apollo/client";
export const getUserProjectsGQL = gql(`
query GetUserProjects {
demo_users {
id
name
projects {
demo_projects_id {
id
name
}
}
}
}
`);

View File

@ -1,39 +1,10 @@
import { ofetch } from 'ofetch';
// import { gql } from './__generated__';
import { ApolloClient, InMemoryCache, gql } from '@apollo/client';
const gqlText = gql(
`
query get {
demo_projects {
id
}
}`
);
const client = new ApolloClient({
uri: process.env.GRAPHQL_ENDPOINT_WITH_TOKEN, // 你的 GraphQL 服务器地址
cache: new InMemoryCache(), // 内置的内存缓存
// 可以添加更多选项,比如 headers、fetchPolicy 等
});
import type { GetUserProjectsQuery, GetUserProjectsQueryVariables } from "./__generated__/graphql"
import { client } from "./apollo-client"
import { getUserProjectsGQL } from "./gql"
(async () => {
const result = await client.query({
query: gqlText
const result = await client.query<GetUserProjectsQuery, GetUserProjectsQueryVariables>({
query: getUserProjectsGQL,
})
})
// (async () => {
// const result = await ofetch("http://172.16.6.246:8055/graphql", {
// method: 'POST',
// headers: {
// 'Authorization': 'Bearer WkVWFMiFcrjsXRZqsL30Cd4Sboe0DRk-',
// 'Content-Type': 'application/json',
// },
// body: {
// query: gqlText,
// variables: {}
// },
// })
// console.log(JSON.stringify(result, null, 2));
// })();
console.log(result.data?.demo_users.map(user => user?.projects?.map(project => project?.demo_projects_id)))
})()