1
- import { GraphQLNonNull , GraphQLString , GraphQLBoolean } from 'graphql' ;
1
+ import {
2
+ GraphQLNonNull ,
3
+ GraphQLString ,
4
+ GraphQLBoolean ,
5
+ GraphQLInputObjectType ,
6
+ } from 'graphql' ;
2
7
import { mutationWithClientMutationId } from 'graphql-relay' ;
3
8
import UsersRouter from '../../Routers/UsersRouter' ;
4
9
import * as objectsMutations from '../helpers/objectsMutations' ;
10
+ import { OBJECT } from './defaultGraphQLTypes' ;
5
11
import { getUserFromSessionToken } from './usersQueries' ;
6
12
7
13
const usersRouter = new UsersRouter ( ) ;
@@ -16,7 +22,7 @@ const load = parseGraphQLSchema => {
16
22
description :
17
23
'The signUp mutation can be used to create and sign up a new user.' ,
18
24
inputFields : {
19
- userFields : {
25
+ fields : {
20
26
descriptions :
21
27
'These are the fields of the new user to be created and signed up.' ,
22
28
type :
@@ -32,12 +38,12 @@ const load = parseGraphQLSchema => {
32
38
} ,
33
39
mutateAndGetPayload : async ( args , context , mutationInfo ) => {
34
40
try {
35
- const { userFields } = args ;
41
+ const { fields } = args ;
36
42
const { config, auth, info } = context ;
37
43
38
44
const { sessionToken } = await objectsMutations . createObject (
39
45
'_User' ,
40
- userFields ,
46
+ fields ,
41
47
config ,
42
48
auth ,
43
49
info
@@ -67,6 +73,90 @@ const load = parseGraphQLSchema => {
67
73
) ;
68
74
parseGraphQLSchema . addGraphQLType ( signUpMutation . type , true , true ) ;
69
75
parseGraphQLSchema . addGraphQLMutation ( 'signUp' , signUpMutation , true , true ) ;
76
+ const logInWithMutation = mutationWithClientMutationId ( {
77
+ name : 'LogInWith' ,
78
+ description :
79
+ 'The logInWith mutation can be used to signup, login user with 3rd party authentication system. This mutation create a user if the authData do not correspond to an existing one.' ,
80
+ inputFields : {
81
+ authData : {
82
+ descriptions : 'This is the auth data of your custom auth provider' ,
83
+ type : new GraphQLNonNull ( OBJECT ) ,
84
+ } ,
85
+ fields : {
86
+ descriptions :
87
+ 'These are the fields of the user to be created/updated and logged in.' ,
88
+ type : new GraphQLInputObjectType ( {
89
+ name : 'UserLoginWithInput' ,
90
+ fields : ( ) => {
91
+ const classGraphQLCreateFields = parseGraphQLSchema . parseClassTypes [
92
+ '_User'
93
+ ] . classGraphQLCreateType . getFields ( ) ;
94
+ return Object . keys ( classGraphQLCreateFields ) . reduce (
95
+ ( fields , fieldName ) => {
96
+ if (
97
+ fieldName !== 'password' &&
98
+ fieldName !== 'username' &&
99
+ fieldName !== 'authData'
100
+ ) {
101
+ fields [ fieldName ] = classGraphQLCreateFields [ fieldName ] ;
102
+ }
103
+ return fields ;
104
+ } ,
105
+ { }
106
+ ) ;
107
+ } ,
108
+ } ) ,
109
+ } ,
110
+ } ,
111
+ outputFields : {
112
+ viewer : {
113
+ description :
114
+ 'This is the new user that was created, signed up and returned as a viewer.' ,
115
+ type : new GraphQLNonNull ( parseGraphQLSchema . viewerType ) ,
116
+ } ,
117
+ } ,
118
+ mutateAndGetPayload : async ( args , context , mutationInfo ) => {
119
+ try {
120
+ const { fields, authData } = args ;
121
+ const { config, auth, info } = context ;
122
+
123
+ const { sessionToken } = await objectsMutations . createObject (
124
+ '_User' ,
125
+ { ...fields , authData } ,
126
+ config ,
127
+ auth ,
128
+ info
129
+ ) ;
130
+
131
+ info . sessionToken = sessionToken ;
132
+
133
+ return {
134
+ viewer : await getUserFromSessionToken (
135
+ config ,
136
+ info ,
137
+ mutationInfo ,
138
+ 'viewer.user.' ,
139
+ true
140
+ ) ,
141
+ } ;
142
+ } catch ( e ) {
143
+ parseGraphQLSchema . handleError ( e ) ;
144
+ }
145
+ } ,
146
+ } ) ;
147
+
148
+ parseGraphQLSchema . addGraphQLType (
149
+ logInWithMutation . args . input . type . ofType ,
150
+ true ,
151
+ true
152
+ ) ;
153
+ parseGraphQLSchema . addGraphQLType ( logInWithMutation . type , true , true ) ;
154
+ parseGraphQLSchema . addGraphQLMutation (
155
+ 'logInWith' ,
156
+ logInWithMutation ,
157
+ true ,
158
+ true
159
+ ) ;
70
160
71
161
const logInMutation = mutationWithClientMutationId ( {
72
162
name : 'LogIn' ,
0 commit comments