@@ -7,16 +7,22 @@ const {
77 GraphQLString,
88 GraphQLInt,
99 GraphQLSchema,
10- GraphQLList
10+ GraphQLList,
11+ GraphQLNonNull
1112} = graphql ;
1213
13- const BASE_URL = ' http://localhost:3000' ;
14+ const BASE_URL = " http://localhost:3000" ;
1415
15- const fetchFromUrl = url =>
16- axios
17- . get ( url )
18- . then ( _ . property ( "data" ) )
19- . catch ( console . error ) ;
16+ const getDataPromise = axiosPromise =>
17+ axiosPromise . then ( _ . property ( "data" ) ) . catch ( console . error ) ;
18+
19+ const fetchFromUrl = url => getDataPromise ( axios . get ( url ) ) ;
20+
21+ const addOnUrl = ( url , body ) => getDataPromise ( axios . post ( url , body ) ) ;
22+
23+ const deleteOnUrl = url => getDataPromise ( axios . delete ( url ) ) ;
24+
25+ const editOnUrl = ( url , body ) => getDataPromise ( axios . patch ( url , body ) ) ;
2026
2127const CompanyType = new GraphQLObjectType ( {
2228 name : "Company" ,
@@ -42,9 +48,7 @@ const UserType = new GraphQLObjectType({
4248 company : {
4349 type : CompanyType ,
4450 resolve ( parentValue , args ) {
45- return fetchFromUrl (
46- `${ BASE_URL } /companies/${ parentValue . companyId } `
47- ) ;
51+ return fetchFromUrl ( `${ BASE_URL } /companies/${ parentValue . companyId } ` ) ;
4852 }
4953 }
5054 } )
@@ -70,6 +74,45 @@ const RootQuery = new GraphQLObjectType({
7074 }
7175} ) ;
7276
77+ const mutation = new GraphQLObjectType ( {
78+ name : "mutation" ,
79+ fields : {
80+ addUser : {
81+ type : UserType ,
82+ args : {
83+ firstName : { type : new GraphQLNonNull ( GraphQLString ) } ,
84+ age : { type : new GraphQLNonNull ( GraphQLInt ) } ,
85+ companyId : { type : GraphQLString }
86+ } ,
87+ resolve ( parentValue , args ) {
88+ return addOnUrl ( `${ BASE_URL } /users` , args ) ;
89+ }
90+ } ,
91+ deleteUser : {
92+ type : UserType ,
93+ args : {
94+ id : { type : new GraphQLNonNull ( GraphQLString ) }
95+ } ,
96+ resolve ( parentValue , args ) {
97+ return deleteOnUrl ( `${ BASE_URL } /users/${ args . id } ` ) ;
98+ }
99+ } ,
100+ editUser : {
101+ type : UserType ,
102+ args : {
103+ id : { type : new GraphQLNonNull ( GraphQLString ) } ,
104+ firstName : { type : GraphQLString } ,
105+ age : { type : GraphQLInt } ,
106+ companyId : { type : GraphQLString }
107+ } ,
108+ resolve ( parentValue , args ) {
109+ return editOnUrl ( `${ BASE_URL } /users/${ args . id } ` , args ) ;
110+ }
111+ }
112+ }
113+ } ) ;
114+
73115module . exports = new GraphQLSchema ( {
74- query : RootQuery
116+ query : RootQuery ,
117+ mutation
75118} ) ;
0 commit comments