From 436fe3815e2fceb0e3aae92f9e0981d3d85ce148 Mon Sep 17 00:00:00 2001 From: Anthony Lee Date: Fri, 3 Nov 2017 13:17:47 -0500 Subject: [PATCH] Add mutation through GraphiQL --- graphql/db.json | 40 ++++++++++++++++++++++++++++++++++------ graphql/schema/schema.js | 15 +++++++++------ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/graphql/db.json b/graphql/db.json index dd45876..80b4ea2 100644 --- a/graphql/db.json +++ b/graphql/db.json @@ -1,11 +1,39 @@ { "users": [ - { "id": "23", "firstName": "Bill", "age": 20, "companyId": "1" }, - { "id": "40", "firstName": "Alex", "age": 40, "companyId": "2" }, - { "id": "41", "firstName": "Nick", "age": 40, "companyId": "2" } + { + "id": "23", + "firstName": "Bill", + "age": 20, + "companyId": "1" + }, + { + "id": "40", + "firstName": "Alex", + "age": 40, + "companyId": "2" + }, + { + "id": "41", + "firstName": "Nick", + "age": 40, + "companyId": "2" + }, + { + "firstName": "Anthony", + "age": 26, + "id": "yOTVhLU" + } ], "companies": [ - { "id": "1", "name": "Apple", "description": "iphone" }, - { "id": "2", "name": "Google", "description": "search" } + { + "id": "1", + "name": "Apple", + "description": "iphone" + }, + { + "id": "2", + "name": "Google", + "description": "search" + } ] -} +} \ No newline at end of file diff --git a/graphql/schema/schema.js b/graphql/schema/schema.js index 5e33891..af6a1f2 100644 --- a/graphql/schema/schema.js +++ b/graphql/schema/schema.js @@ -6,7 +6,8 @@ const { GraphQLString, GraphQLInt, GraphQLSchema, - GraphQLList + GraphQLList, + GraphQLNonNull } = graphql; const CompanyType = new GraphQLObjectType({ @@ -69,17 +70,19 @@ const mutation = new GraphQLObjectType({ addUser: { type: UserType, args: { - firstName: { type: GraphQLString }, - age: { type: GraphQLInt }, + firstName: { type: new GraphQLNonNull(GraphQLString) }, + age: { type: new GraphQLNonNull(GraphQLInt) }, companyId: { type: GraphQLString } }, - resolve() { - + resolve(parentValue, { firstName, age }) { + return axios.post(`http://localhost:3000/users`, { firstName, age }) + .then(res => res.data); } } } }); module.exports = new GraphQLSchema({ - query: RootQuery + query: RootQuery, + mutation });