Skip to content

Commit a3222e8

Browse files
committed
Added graphql codegen to stack
- Generate types file with "npm run codegen"
1 parent 562afcb commit a3222e8

File tree

7 files changed

+6178
-751
lines changed

7 files changed

+6178
-751
lines changed

codegen.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { CodegenConfig } from '@graphql-codegen/cli';
2+
3+
const config: CodegenConfig = {
4+
schema: 'src/graphql/schema.graphql',
5+
generates: {
6+
'./src/generated/resolvers-types.ts': {
7+
config: {
8+
useIndexSignature: true,
9+
},
10+
plugins: ['typescript', 'typescript-resolvers'],
11+
},
12+
},
13+
};
14+
export default config;

package-lock.json

+6,029-739
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
"description": "Comerical CRM",
55
"main": "index.js",
66
"scripts": {
7-
"dev": "nodemon -w ./src --exec \"npm run start\"",
8-
"start": "ts-node src/index.ts"
7+
"dev": "nodemon -w ./src --ext \"ts\" --exec \"npm run start\"",
8+
"start": "ts-node src/index.ts",
9+
"codegen": "graphql-codegen"
910
},
1011
"author": "David Mitchell <thecodingtree>",
1112
"license": "ISC",
1213
"devDependencies": {
14+
"@graphql-codegen/cli": "^5.0.0",
15+
"@graphql-codegen/typescript": "^4.0.1",
16+
"@graphql-codegen/typescript-resolvers": "^4.0.1",
1317
"@tsconfig/node18": "^18.2.1",
1418
"@types/node": "^18.17.14",
1519
"nodemon": "^3.0.1",

src/generated/resolvers-types.ts

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { GraphQLResolveInfo } from 'graphql';
2+
export type Maybe<T> = T | null;
3+
export type InputMaybe<T> = Maybe<T>;
4+
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
5+
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
6+
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
7+
export type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };
8+
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
9+
/** All built-in and custom scalars, mapped to their actual values */
10+
export type Scalars = {
11+
ID: { input: string; output: string; }
12+
String: { input: string; output: string; }
13+
Boolean: { input: boolean; output: boolean; }
14+
Int: { input: number; output: number; }
15+
Float: { input: number; output: number; }
16+
};
17+
18+
export type Query = {
19+
__typename?: 'Query';
20+
hello: Scalars['String']['output'];
21+
};
22+
23+
export type WithIndex<TObject> = TObject & Record<string, any>;
24+
export type ResolversObject<TObject> = WithIndex<TObject>;
25+
26+
export type ResolverTypeWrapper<T> = Promise<T> | T;
27+
28+
29+
export type ResolverWithResolve<TResult, TParent, TContext, TArgs> = {
30+
resolve: ResolverFn<TResult, TParent, TContext, TArgs>;
31+
};
32+
export type Resolver<TResult, TParent = {}, TContext = {}, TArgs = {}> = ResolverFn<TResult, TParent, TContext, TArgs> | ResolverWithResolve<TResult, TParent, TContext, TArgs>;
33+
34+
export type ResolverFn<TResult, TParent, TContext, TArgs> = (
35+
parent: TParent,
36+
args: TArgs,
37+
context: TContext,
38+
info: GraphQLResolveInfo
39+
) => Promise<TResult> | TResult;
40+
41+
export type SubscriptionSubscribeFn<TResult, TParent, TContext, TArgs> = (
42+
parent: TParent,
43+
args: TArgs,
44+
context: TContext,
45+
info: GraphQLResolveInfo
46+
) => AsyncIterable<TResult> | Promise<AsyncIterable<TResult>>;
47+
48+
export type SubscriptionResolveFn<TResult, TParent, TContext, TArgs> = (
49+
parent: TParent,
50+
args: TArgs,
51+
context: TContext,
52+
info: GraphQLResolveInfo
53+
) => TResult | Promise<TResult>;
54+
55+
export interface SubscriptionSubscriberObject<TResult, TKey extends string, TParent, TContext, TArgs> {
56+
subscribe: SubscriptionSubscribeFn<{ [key in TKey]: TResult }, TParent, TContext, TArgs>;
57+
resolve?: SubscriptionResolveFn<TResult, { [key in TKey]: TResult }, TContext, TArgs>;
58+
}
59+
60+
export interface SubscriptionResolverObject<TResult, TParent, TContext, TArgs> {
61+
subscribe: SubscriptionSubscribeFn<any, TParent, TContext, TArgs>;
62+
resolve: SubscriptionResolveFn<TResult, any, TContext, TArgs>;
63+
}
64+
65+
export type SubscriptionObject<TResult, TKey extends string, TParent, TContext, TArgs> =
66+
| SubscriptionSubscriberObject<TResult, TKey, TParent, TContext, TArgs>
67+
| SubscriptionResolverObject<TResult, TParent, TContext, TArgs>;
68+
69+
export type SubscriptionResolver<TResult, TKey extends string, TParent = {}, TContext = {}, TArgs = {}> =
70+
| ((...args: any[]) => SubscriptionObject<TResult, TKey, TParent, TContext, TArgs>)
71+
| SubscriptionObject<TResult, TKey, TParent, TContext, TArgs>;
72+
73+
export type TypeResolveFn<TTypes, TParent = {}, TContext = {}> = (
74+
parent: TParent,
75+
context: TContext,
76+
info: GraphQLResolveInfo
77+
) => Maybe<TTypes> | Promise<Maybe<TTypes>>;
78+
79+
export type IsTypeOfResolverFn<T = {}, TContext = {}> = (obj: T, context: TContext, info: GraphQLResolveInfo) => boolean | Promise<boolean>;
80+
81+
export type NextResolverFn<T> = () => Promise<T>;
82+
83+
export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs = {}> = (
84+
next: NextResolverFn<TResult>,
85+
parent: TParent,
86+
args: TArgs,
87+
context: TContext,
88+
info: GraphQLResolveInfo
89+
) => TResult | Promise<TResult>;
90+
91+
92+
93+
/** Mapping between all available schema types and the resolvers types */
94+
export type ResolversTypes = ResolversObject<{
95+
Boolean: ResolverTypeWrapper<Scalars['Boolean']['output']>;
96+
Query: ResolverTypeWrapper<{}>;
97+
String: ResolverTypeWrapper<Scalars['String']['output']>;
98+
}>;
99+
100+
/** Mapping between all available schema types and the resolvers parents */
101+
export type ResolversParentTypes = ResolversObject<{
102+
Boolean: Scalars['Boolean']['output'];
103+
Query: {};
104+
String: Scalars['String']['output'];
105+
}>;
106+
107+
export type QueryResolvers<ContextType = any, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = ResolversObject<{
108+
hello?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
109+
}>;
110+
111+
export type Resolvers<ContextType = any> = ResolversObject<{
112+
Query?: QueryResolvers<ContextType>;
113+
}>;
114+

src/graphql/resolvers.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Resolvers } from '../generated/resolvers-types';
2+
3+
const resolvers: Resolvers = {
4+
Query: {
5+
hello: () => 'world!',
6+
},
7+
};
8+
9+
export default resolvers;

src/graphql/schema.graphql

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type Query {
2+
hello: String!
3+
}

src/index.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
import { ApolloServer } from '@apollo/server';
22
import { startStandaloneServer } from '@apollo/server/standalone';
3+
import { readFileSync } from 'fs';
34
import { gql } from 'graphql-tag';
45

5-
const typeDefs = gql`
6-
type Query {
7-
hello: String!
8-
}
9-
`;
6+
import resolvers from './graphql/resolvers';
107

11-
const resolvers = {
12-
Query: {
13-
hello: () => 'world',
14-
},
15-
};
8+
const typeDefs = readFileSync('./src/graphql/schema.graphql', 'utf8');
169

1710
const server = new ApolloServer({
1811
typeDefs,

0 commit comments

Comments
 (0)