This commit is contained in:
Liam Chan 2025-02-18 12:30:47 +08:00
parent 90f4cfa6ee
commit ae97dc0a5f
12 changed files with 16 additions and 55 deletions

View File

@ -5,7 +5,7 @@ const config: CodegenConfig = {
// this assumes that all your source files are in a top-level `src/` directory - you might need to adjust this to your file structure
documents: ['src/**/*.{ts,tsx}'],
generates: {
'./src/__generated__/': {
'./graphql/__generated__/': {
preset: 'client',
plugins: [],
presetConfig: {

View File

@ -4,7 +4,7 @@ export default {
projects: {
directus: {
schema: "http://172.16.6.246:8055/graphql?access_token=WkVWFMiFcrjsXRZqsL30Cd4Sboe0DRk-",
documents: '**/*.{graphql,js,ts,jsx,tsx}',
documents: 'src/**/*.{graphql,js,ts,jsx,tsx}',
extensions: {
languageService: {
cacheSchemaFileForLookup: true

View File

@ -2,7 +2,6 @@
"name": "graphql-demo",
"scripts": {
"dev": "bun run --watch src/index.ts",
"dev-apollo": "bun run --watch src/index.apollo.ts",
"compile": "graphql-codegen",
"watch": "graphql-codegen -w"
},
@ -21,6 +20,7 @@
"@apollo/client": "^3.13.1",
"axios": "^1.7.9",
"graphql": "^16.10.0",
"graphql-request": "^7.1.2",
"graphql-tag": "^2.12.6",
"ofetch": "^1.4.1"
}

View File

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

View File

@ -1,6 +1,6 @@
import { gql } from "@apollo/client";
import { gql } from "graphql-request";
export const getUserProjectsGQL = gql(`
export const getUserProjectsGQL = gql`
query GetUserProjects {
demo_users {
id
@ -13,4 +13,4 @@ export const getUserProjectsGQL = gql(`
}
}
}
`);
`;

3
src/graphql-client.ts Normal file
View File

@ -0,0 +1,3 @@
import { GraphQLClient } from "graphql-request";
export const graphqlClient = new GraphQLClient(process.env.GRAPHQL_ENDPOINT_WITH_TOKEN!);

View File

@ -1,10 +0,0 @@
import type { GetUserProjectsQuery, GetUserProjectsQueryVariables } from "./__generated__/graphql"
import { client } from "./apollo-client"
import { getUserProjectsGQL } from "./gql"
(async () => {
const result = await client.query<GetUserProjectsQuery, GetUserProjectsQueryVariables>({
query: getUserProjectsGQL,
})
console.log(result.data?.demo_users.map(user => user?.projects?.map(project => project?.demo_projects_id)))
})()

View File

@ -1,34 +1,8 @@
import { ofetch } from 'ofetch';
import gql from 'graphql-tag'
import { print } from 'graphql';
/** GraphQL */
const gqlText = print(gql(`
query get {
demo_projects {
id
}
}`
));
console.log(gqlText);
// const gqlText = gql(
// `query {w
import type { GetUserProjectsQuery, GetUserProjectsQueryVariables } from "../graphql/__generated__/graphql"
import { graphqlClient } from "./graphql-client"
import { getUserProjectsGQL } from "./gql"
(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));
})();
const result = await graphqlClient.request<GetUserProjectsQuery, GetUserProjectsQueryVariables>(getUserProjectsGQL)
console.log(result.demo_users.map(user => user?.projects?.map(project => project?.demo_projects_id)))
})()