1- import { GraphQLNonNull , GraphQLString , GraphQLBoolean } from 'graphql' ;
1+ import {
2+ GraphQLNonNull ,
3+ GraphQLString ,
4+ GraphQLBoolean ,
5+ GraphQLInputObjectType ,
6+ } from 'graphql' ;
27import { mutationWithClientMutationId } from 'graphql-relay' ;
38import UsersRouter from '../../Routers/UsersRouter' ;
49import * as objectsMutations from '../helpers/objectsMutations' ;
10+ import { OBJECT } from './defaultGraphQLTypes' ;
511import { getUserFromSessionToken } from './usersQueries' ;
612
713const usersRouter = new UsersRouter ( ) ;
@@ -16,7 +22,7 @@ const load = parseGraphQLSchema => {
1622 description :
1723 'The signUp mutation can be used to create and sign up a new user.' ,
1824 inputFields : {
19- userFields : {
25+ fields : {
2026 descriptions :
2127 'These are the fields of the new user to be created and signed up.' ,
2228 type :
@@ -32,12 +38,12 @@ const load = parseGraphQLSchema => {
3238 } ,
3339 mutateAndGetPayload : async ( args , context , mutationInfo ) => {
3440 try {
35- const { userFields } = args ;
41+ const { fields } = args ;
3642 const { config, auth, info } = context ;
3743
3844 const { sessionToken } = await objectsMutations . createObject (
3945 '_User' ,
40- userFields ,
46+ fields ,
4147 config ,
4248 auth ,
4349 info
@@ -67,6 +73,90 @@ const load = parseGraphQLSchema => {
6773 ) ;
6874 parseGraphQLSchema . addGraphQLType ( signUpMutation . type , true , true ) ;
6975 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+ ) ;
70160
71161 const logInMutation = mutationWithClientMutationId ( {
72162 name : 'LogIn' ,
0 commit comments