apollo
This commit is contained in:
parent
9ff78b25ce
commit
90f4cfa6ee
6
src/__generated__/gql.ts
generated
6
src/__generated__/gql.ts
generated
@ -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
|
* Learn more about it here: https://the-guild.dev/graphql/codegen/plugins/presets/preset-client#reducing-bundle-size
|
||||||
*/
|
*/
|
||||||
type Documents = {
|
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,
|
"\n query get {\n demo_projects {\n id\n }\n }": typeof types.GetDocument,
|
||||||
};
|
};
|
||||||
const documents: Documents = {
|
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,
|
"\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;
|
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.
|
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
|
||||||
*/
|
*/
|
||||||
|
6
src/__generated__/graphql.ts
generated
6
src/__generated__/graphql.ts
generated
@ -3507,10 +3507,16 @@ export type Version_Root_Table_2_Root_Table = {
|
|||||||
root_table_id?: Maybe<Scalars['JSON']['output']>;
|
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 GetQueryVariables = Exact<{ [key: string]: never; }>;
|
||||||
|
|
||||||
|
|
||||||
export type GetQuery = { __typename?: 'Query', demo_projects: Array<{ __typename?: 'demo_projects', id: string }> };
|
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>;
|
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
6
src/apollo-client.ts
Normal 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
16
src/gql.ts
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`);
|
@ -1,39 +1,10 @@
|
|||||||
import { ofetch } from 'ofetch';
|
import type { GetUserProjectsQuery, GetUserProjectsQueryVariables } from "./__generated__/graphql"
|
||||||
// import { gql } from './__generated__';
|
import { client } from "./apollo-client"
|
||||||
import { ApolloClient, InMemoryCache, gql } from '@apollo/client';
|
import { getUserProjectsGQL } from "./gql"
|
||||||
|
|
||||||
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 等
|
|
||||||
});
|
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const result = await client.query({
|
const result = await client.query<GetUserProjectsQuery, GetUserProjectsQueryVariables>({
|
||||||
query: gqlText
|
query: getUserProjectsGQL,
|
||||||
})
|
})
|
||||||
})
|
console.log(result.data?.demo_users.map(user => user?.projects?.map(project => project?.demo_projects_id)))
|
||||||
|
})()
|
||||||
// (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));
|
|
||||||
// })();
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user