Skip to content

Commit 27e2e90

Browse files
committed
Use graphql-binding to generate typings
1 parent 44a77e9 commit 27e2e90

File tree

5 files changed

+208
-394
lines changed

5 files changed

+208
-394
lines changed

packages/graphql-authentication/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@
2929
"test": "jest --watch",
3030
"test-coverage": "jest --coverage",
3131
"ci": "npm run -s lint && npm run -s build && npm run -s test-coverage && codecov",
32-
"graphql-types": "gql-gen --template graphql-codegen-typescript-template --out=src/schema.ts --schema http://localhost:4000"
32+
"graphql-types": "graphql-binding --input src/schema.ts --language typescript --outputBinding src/binding.ts"
3333
},
3434
"devDependencies": {
3535
"@types/email-templates": "^3.5.0",
3636
"@types/jest": "^23.1.0",
3737
"@volst/tslint-config": "^0.2.1",
3838
"codecov": "^3.0.2",
3939
"email-templates": "^4.0.1",
40-
"graphql-code-generator": "^0.9.4",
41-
"graphql-codegen-typescript-template": "^0.9.4",
40+
"graphql-binding": "^2.1.1",
41+
"graphql-cli": "^2.16.3",
4242
"graphql-request": "^1.6.0",
4343
"graphql-yoga": "1.14.10",
4444
"jest": "^23.1.0",
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import { makeBindingClass, Options } from 'graphql-binding';
2+
import { GraphQLResolveInfo, GraphQLSchema } from 'graphql';
3+
import { IResolvers } from 'graphql-tools/dist/Interfaces';
4+
import schema from './schema';
5+
6+
export interface Query {
7+
currentUser: <T = User | null>(
8+
args?: {},
9+
info?: GraphQLResolveInfo | string,
10+
options?: Options
11+
) => Promise<T>;
12+
}
13+
14+
export interface Mutation {
15+
signupByInvite: <T = AuthPayload>(
16+
args: { data: SignupByInviteInput },
17+
info?: GraphQLResolveInfo | string,
18+
options?: Options
19+
) => Promise<T>;
20+
signup: <T = AuthPayload>(
21+
args: { data: SignupInput },
22+
info?: GraphQLResolveInfo | string,
23+
options?: Options
24+
) => Promise<T>;
25+
confirmEmail: <T = AuthPayload>(
26+
args: { email: String; emailConfirmToken: String },
27+
info?: GraphQLResolveInfo | string,
28+
options?: Options
29+
) => Promise<T>;
30+
inviteUser: <T = UserIdPayload>(
31+
args: { data: InviteUserInput },
32+
info?: GraphQLResolveInfo | string,
33+
options?: Options
34+
) => Promise<T>;
35+
login: <T = AuthPayload>(
36+
args: { email: String; password: String },
37+
info?: GraphQLResolveInfo | string,
38+
options?: Options
39+
) => Promise<T>;
40+
changePassword: <T = UserIdPayload>(
41+
args: { oldPassword: String; newPassword: String },
42+
info?: GraphQLResolveInfo | string,
43+
options?: Options
44+
) => Promise<T>;
45+
updateCurrentUser: <T = User | null>(
46+
args: { data: UserUpdateInput },
47+
info?: GraphQLResolveInfo | string,
48+
options?: Options
49+
) => Promise<T>;
50+
triggerPasswordReset: <T = TriggerPasswordResetPayload>(
51+
args: { email: String },
52+
info?: GraphQLResolveInfo | string,
53+
options?: Options
54+
) => Promise<T>;
55+
passwordReset: <T = UserIdPayload>(
56+
args: { email: String; resetToken: String; password: String },
57+
info?: GraphQLResolveInfo | string,
58+
options?: Options
59+
) => Promise<T>;
60+
}
61+
62+
export interface Subscription {}
63+
64+
export interface Binding {
65+
query: Query;
66+
mutation: Mutation;
67+
subscription: Subscription;
68+
request: <T = any>(
69+
query: string,
70+
variables?: { [key: string]: any }
71+
) => Promise<T>;
72+
delegate(
73+
operation: 'query' | 'mutation',
74+
fieldName: string,
75+
args: {
76+
[key: string]: any;
77+
},
78+
infoOrQuery?: GraphQLResolveInfo | string,
79+
options?: Options
80+
): Promise<any>;
81+
delegateSubscription(
82+
fieldName: string,
83+
args?: {
84+
[key: string]: any;
85+
},
86+
infoOrQuery?: GraphQLResolveInfo | string,
87+
options?: Options
88+
): Promise<AsyncIterator<any>>;
89+
getAbstractResolvers(filterSchema?: GraphQLSchema | string): IResolvers;
90+
}
91+
92+
export interface BindingConstructor<T> {
93+
new (...args): T;
94+
}
95+
96+
export const Binding = makeBindingClass<BindingConstructor<Binding>>({
97+
schema
98+
});
99+
100+
/**
101+
* Types
102+
*/
103+
104+
export interface SignupByInviteInput {
105+
email: String;
106+
inviteToken: String;
107+
password: String;
108+
name: String;
109+
}
110+
111+
export interface SignupInput {
112+
email: String;
113+
password: String;
114+
name: String;
115+
}
116+
117+
export interface InviteUserInput {
118+
email: String;
119+
}
120+
121+
export interface UserUpdateInput {
122+
email?: String;
123+
name?: String;
124+
}
125+
126+
export interface AuthPayload {
127+
token: String;
128+
user: User;
129+
}
130+
131+
export interface TriggerPasswordResetPayload {
132+
ok: Boolean;
133+
}
134+
135+
export interface User {
136+
id: ID_Output;
137+
email: String;
138+
name: String;
139+
inviteAccepted: Boolean;
140+
emailConfirmed: Boolean;
141+
deletedAt?: DateTime;
142+
lastLogin?: DateTime;
143+
joinedAt: DateTime;
144+
isSuper: Boolean;
145+
}
146+
147+
export interface UserIdPayload {
148+
id: ID_Output;
149+
}
150+
151+
/*
152+
The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
153+
*/
154+
export type String = string;
155+
156+
/*
157+
The `Boolean` scalar type represents `true` or `false`.
158+
*/
159+
export type Boolean = boolean;
160+
161+
export type DateTime = Date | string;
162+
163+
/*
164+
The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.
165+
*/
166+
export type ID_Input = string | number;
167+
export type ID_Output = string;

packages/graphql-authentication/src/mutations.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,11 @@ import {
1919
UserEmailUnconfirmedError
2020
} from './errors';
2121
import {
22-
SignupByInviteMutationArgs,
23-
SignupMutationArgs,
24-
ConfirmEmailMutationArgs,
25-
LoginMutationArgs,
26-
ChangePasswordMutationArgs,
27-
InviteUserMutationArgs,
28-
TriggerPasswordResetMutationArgs,
29-
PasswordResetMutationArgs,
30-
UpdateCurrentUserMutationArgs
31-
} from './schema';
22+
SignupByInviteInput,
23+
SignupInput,
24+
InviteUserInput,
25+
UserUpdateInput
26+
} from './binding';
3227

3328
function generateToken(user: User, ctx: Context) {
3429
return jwt.sign({ userId: user.id }, ctx.graphqlAuthentication.secret);
@@ -47,7 +42,7 @@ function getHashedPassword(value: string) {
4742
export const mutations = {
4843
async signupByInvite(
4944
parent: any,
50-
{ data }: SignupByInviteMutationArgs,
45+
{ data }: { data: SignupByInviteInput },
5146
ctx: Context
5247
) {
5348
// Important first check, because i.e. the `inviteToken` could be an empty string
@@ -87,7 +82,7 @@ export const mutations = {
8782
};
8883
},
8984

90-
async signup(parent: any, { data }: SignupMutationArgs, ctx: Context) {
85+
async signup(parent: any, { data }: { data: SignupInput }, ctx: Context) {
9186
if (!data.email) {
9287
throw new MissingDataError();
9388
}
@@ -137,7 +132,7 @@ export const mutations = {
137132

138133
async confirmEmail(
139134
parent: any,
140-
{ emailConfirmToken, email }: ConfirmEmailMutationArgs,
135+
{ emailConfirmToken, email }: { emailConfirmToken: string; email: string },
141136
ctx: Context
142137
) {
143138
if (!emailConfirmToken || !email) {
@@ -171,7 +166,7 @@ export const mutations = {
171166

172167
async login(
173168
parent: any,
174-
{ email, password }: LoginMutationArgs,
169+
{ email, password }: { email: string; password: string },
175170
ctx: Context
176171
) {
177172
const user = await ctx.graphqlAuthentication.adapter.findUserByEmail(
@@ -215,7 +210,7 @@ export const mutations = {
215210

216211
async changePassword(
217212
parent: any,
218-
{ oldPassword, newPassword }: ChangePasswordMutationArgs,
213+
{ oldPassword, newPassword }: { oldPassword: string; newPassword: string },
219214
ctx: Context
220215
) {
221216
const user = await getUser(ctx);
@@ -241,7 +236,7 @@ export const mutations = {
241236

242237
async inviteUser(
243238
parent: any,
244-
{ data }: InviteUserMutationArgs,
239+
{ data }: { data: InviteUserInput },
245240
ctx: Context
246241
) {
247242
await getUser(ctx);
@@ -313,7 +308,7 @@ export const mutations = {
313308

314309
async triggerPasswordReset(
315310
parent: any,
316-
{ email }: TriggerPasswordResetMutationArgs,
311+
{ email }: { email: string },
317312
ctx: Context
318313
) {
319314
if (!validator.isEmail(email)) {
@@ -362,7 +357,11 @@ export const mutations = {
362357

363358
async passwordReset(
364359
parent: any,
365-
{ email, resetToken, password }: PasswordResetMutationArgs,
360+
{
361+
email,
362+
resetToken,
363+
password
364+
}: { email: string; resetToken: string; password: string },
366365
ctx: Context
367366
) {
368367
if (!resetToken || !password) {
@@ -397,7 +396,7 @@ export const mutations = {
397396

398397
async updateCurrentUser(
399398
parent: any,
400-
{ data }: UpdateCurrentUserMutationArgs,
399+
{ data }: { data: UserUpdateInput },
401400
ctx: Context
402401
) {
403402
const user = await getUser(ctx);

0 commit comments

Comments
 (0)