diff --git a/src/__generated__/gql.ts b/src/__generated__/gql.ts index 914a4e9..8220309 100644 --- a/src/__generated__/gql.ts +++ b/src/__generated__/gql.ts @@ -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. */ diff --git a/src/__generated__/graphql.ts b/src/__generated__/graphql.ts index b0f754a..436810f 100644 --- a/src/__generated__/graphql.ts +++ b/src/__generated__/graphql.ts @@ -3507,10 +3507,16 @@ export type Version_Root_Table_2_Root_Table = { root_table_id?: Maybe; }; +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; 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; \ No newline at end of file diff --git a/src/apollo-client.ts b/src/apollo-client.ts new file mode 100644 index 0000000..754a408 --- /dev/null +++ b/src/apollo-client.ts @@ -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(), +}); diff --git a/src/gql.ts b/src/gql.ts new file mode 100644 index 0000000..16913df --- /dev/null +++ b/src/gql.ts @@ -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 + } + } + } + } +`); \ No newline at end of file diff --git a/src/index.apollo.ts b/src/index.apollo.ts index 3001fcd..5b0e6a1 100644 --- a/src/index.apollo.ts +++ b/src/index.apollo.ts @@ -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({ + 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))) +})()