simple
This commit is contained in:
parent
90f4cfa6ee
commit
ae97dc0a5f
@ -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
|
// 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}'],
|
documents: ['src/**/*.{ts,tsx}'],
|
||||||
generates: {
|
generates: {
|
||||||
'./src/__generated__/': {
|
'./graphql/__generated__/': {
|
||||||
preset: 'client',
|
preset: 'client',
|
||||||
plugins: [],
|
plugins: [],
|
||||||
presetConfig: {
|
presetConfig: {
|
||||||
|
@ -4,7 +4,7 @@ export default {
|
|||||||
projects: {
|
projects: {
|
||||||
directus: {
|
directus: {
|
||||||
schema: "http://172.16.6.246:8055/graphql?access_token=WkVWFMiFcrjsXRZqsL30Cd4Sboe0DRk-",
|
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: {
|
extensions: {
|
||||||
languageService: {
|
languageService: {
|
||||||
cacheSchemaFileForLookup: true
|
cacheSchemaFileForLookup: true
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
"name": "graphql-demo",
|
"name": "graphql-demo",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "bun run --watch src/index.ts",
|
"dev": "bun run --watch src/index.ts",
|
||||||
"dev-apollo": "bun run --watch src/index.apollo.ts",
|
|
||||||
"compile": "graphql-codegen",
|
"compile": "graphql-codegen",
|
||||||
"watch": "graphql-codegen -w"
|
"watch": "graphql-codegen -w"
|
||||||
},
|
},
|
||||||
@ -21,6 +20,7 @@
|
|||||||
"@apollo/client": "^3.13.1",
|
"@apollo/client": "^3.13.1",
|
||||||
"axios": "^1.7.9",
|
"axios": "^1.7.9",
|
||||||
"graphql": "^16.10.0",
|
"graphql": "^16.10.0",
|
||||||
|
"graphql-request": "^7.1.2",
|
||||||
"graphql-tag": "^2.12.6",
|
"graphql-tag": "^2.12.6",
|
||||||
"ofetch": "^1.4.1"
|
"ofetch": "^1.4.1"
|
||||||
}
|
}
|
||||||
|
@ -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(),
|
|
||||||
});
|
|
@ -1,6 +1,6 @@
|
|||||||
import { gql } from "@apollo/client";
|
import { gql } from "graphql-request";
|
||||||
|
|
||||||
export const getUserProjectsGQL = gql(`
|
export const getUserProjectsGQL = gql`
|
||||||
query GetUserProjects {
|
query GetUserProjects {
|
||||||
demo_users {
|
demo_users {
|
||||||
id
|
id
|
||||||
@ -13,4 +13,4 @@ export const getUserProjectsGQL = gql(`
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`);
|
`;
|
||||||
|
3
src/graphql-client.ts
Normal file
3
src/graphql-client.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import { GraphQLClient } from "graphql-request";
|
||||||
|
|
||||||
|
export const graphqlClient = new GraphQLClient(process.env.GRAPHQL_ENDPOINT_WITH_TOKEN!);
|
@ -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)))
|
|
||||||
})()
|
|
38
src/index.ts
38
src/index.ts
@ -1,34 +1,8 @@
|
|||||||
import { ofetch } from 'ofetch';
|
import type { GetUserProjectsQuery, GetUserProjectsQueryVariables } from "../graphql/__generated__/graphql"
|
||||||
import gql from 'graphql-tag'
|
import { graphqlClient } from "./graphql-client"
|
||||||
import { print } from 'graphql';
|
import { getUserProjectsGQL } from "./gql"
|
||||||
|
|
||||||
/** GraphQL */
|
|
||||||
|
|
||||||
const gqlText = print(gql(`
|
|
||||||
query get {
|
|
||||||
demo_projects {
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}`
|
|
||||||
));
|
|
||||||
|
|
||||||
console.log(gqlText);
|
|
||||||
|
|
||||||
// const gqlText = gql(
|
|
||||||
// `query {w
|
|
||||||
|
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const result = await ofetch("http://172.16.6.246:8055/graphql", {
|
const result = await graphqlClient.request<GetUserProjectsQuery, GetUserProjectsQueryVariables>(getUserProjectsGQL)
|
||||||
method: 'POST',
|
console.log(result.demo_users.map(user => user?.projects?.map(project => project?.demo_projects_id)))
|
||||||
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