summaryrefslogtreecommitdiffstats
path: root/backend
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-02-02 15:23:20 -0500
committerWes Bos <wesbos@gmail.com>2018-02-02 15:23:20 -0500
commiteabff7cd396d1f37ceb929e666f082ce57f07919 (patch)
tree6fbd487688ee9e53bada734288994c35b732150e /backend
parentcd0b07b3adb36fb5ec098f9e511ab45d774fee7b (diff)
updateS
Diffstat (limited to 'backend')
-rw-r--r--backend/database/datamodel.graphql1
-rw-r--r--backend/database/prisma.yml8
-rw-r--r--backend/package.json8
-rw-r--r--backend/src/generated/prisma.graphql22
-rw-r--r--backend/src/index.js10
-rw-r--r--backend/src/resolvers/Mutation.js31
-rw-r--r--backend/src/resolvers/Query.js18
-rw-r--r--backend/src/schema.graphql20
-rw-r--r--backend/tests/item.test.js81
9 files changed, 178 insertions, 21 deletions
diff --git a/backend/database/datamodel.graphql b/backend/database/datamodel.graphql
index 605e242..c383b23 100644
--- a/backend/database/datamodel.graphql
+++ b/backend/database/datamodel.graphql
@@ -16,6 +16,7 @@ type User {
posts: [Post!]!
website: String
age: Int
+ dummy: String
}
type Item {
diff --git a/backend/database/prisma.yml b/backend/database/prisma.yml
index 6570be2..ce652ef 100644
--- a/backend/database/prisma.yml
+++ b/backend/database/prisma.yml
@@ -1,12 +1,12 @@
# the name for the service (will be part of the service's HTTP endpoint)
-service: my-own
+service: sick-fits
# the cluster and stage the service is deployed to
stage: ${env:PRISMA_STAGE}
# to disable authentication:
-# disableAuth: true
-secret: ${env:PRISMA_SECRET}
+disableAuth: true
+# secret: ${env:PRISMA_SECRET}
# the file path pointing to your data model
datamodel: datamodel.graphql
@@ -17,5 +17,3 @@ datamodel: datamodel.graphql
cluster: ${env:PRISMA_CLUSTER}
-
-disableAuth: true \ No newline at end of file
diff --git a/backend/package.json b/backend/package.json
index ca26206..86ad2b6 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -2,9 +2,11 @@
"name": "my-own",
"scripts": {
"start": "nodemon -e js,graphql -x node -r dotenv/config src/index.js",
- "debug": "nodemon -e js,graphql -x node --inspect -r dotenv/config src/index.js",
+ "debug":
+ "nodemon -e js,graphql -x node --inspect -r dotenv/config src/index.js",
"playground": "graphql playground",
- "dev": "npm-run-all --parallel start playground"
+ "dev": "npm-run-all --parallel start playground",
+ "test": "jest"
},
"dependencies": {
"bcryptjs": "2.4.3",
@@ -15,6 +17,8 @@
"devDependencies": {
"dotenv": "4.0.0",
"graphql-cli": "2.13.0",
+ "graphql-request": "^1.4.1",
+ "jest-cli": "^22.1.4",
"nodemon": "1.14.11",
"npm-run-all": "4.1.2",
"prisma": "1.0.11"
diff --git a/backend/src/generated/prisma.graphql b/backend/src/generated/prisma.graphql
index 50aaa97..e59a1c9 100644
--- a/backend/src/generated/prisma.graphql
+++ b/backend/src/generated/prisma.graphql
@@ -30,6 +30,7 @@ type User implements Node {
posts(where: PostWhereInput, orderBy: PostOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [Post!]
website: String
age: Int
+ dummy: String
}
@@ -420,6 +421,7 @@ input UserCreateInput {
name: String!
website: String
age: Int
+ dummy: String
posts: PostCreateManyWithoutAuthorInput
}
@@ -434,6 +436,7 @@ input UserCreateWithoutPostsInput {
name: String!
website: String
age: Int
+ dummy: String
}
type UserEdge {
@@ -454,6 +457,8 @@ enum UserOrderByInput {
website_DESC
age_ASC
age_DESC
+ dummy_ASC
+ dummy_DESC
updatedAt_ASC
updatedAt_DESC
createdAt_ASC
@@ -467,6 +472,7 @@ type UserPreviousValues {
name: String!
website: String
age: Int
+ dummy: String
}
type UserSubscriptionPayload {
@@ -492,6 +498,7 @@ input UserUpdateInput {
name: String
website: String
age: Int
+ dummy: String
posts: PostUpdateManyWithoutAuthorInput
}
@@ -510,6 +517,7 @@ input UserUpdateWithoutPostsDataInput {
name: String
website: String
age: Int
+ dummy: String
}
input UserUpdateWithoutPostsInput {
@@ -604,6 +612,20 @@ input UserWhereInput {
age_lte: Int
age_gt: Int
age_gte: Int
+ dummy: String
+ dummy_not: String
+ dummy_in: [String!]
+ dummy_not_in: [String!]
+ dummy_lt: String
+ dummy_lte: String
+ dummy_gt: String
+ dummy_gte: String
+ dummy_contains: String
+ dummy_not_contains: String
+ dummy_starts_with: String
+ dummy_not_starts_with: String
+ dummy_ends_with: String
+ dummy_not_ends_with: String
posts_every: PostWhereInput
posts_some: PostWhereInput
posts_none: PostWhereInput
diff --git a/backend/src/index.js b/backend/src/index.js
index c73cb74..fefe598 100644
--- a/backend/src/index.js
+++ b/backend/src/index.js
@@ -24,10 +24,10 @@ const server = new GraphQLServer({
}),
});
-// This is an example of custom express middlware
-server.express.use((req, res, next) => {
- console.log("Hi I'm middlware");
- next();
-});
+// // This is an example of custom express middlware
+// server.express.use((req, res, next) => {
+// // console.log("Hi I'm middlware");
+// next();
+// });
server.start(() => console.log('Server is running on http://localhost:4000'));
diff --git a/backend/src/resolvers/Mutation.js b/backend/src/resolvers/Mutation.js
index 2796cf8..fbae6fe 100644
--- a/backend/src/resolvers/Mutation.js
+++ b/backend/src/resolvers/Mutation.js
@@ -35,8 +35,35 @@ const mutations = {
},
// Creation of Post Mutations
- createItem: forwardTo('db'),
- deleteItem: forwardTo('db'),
+ async createItem(parent, args, ctx, info) {
+ return ctx.db.mutation.createItem({ data: { ...args } }, info);
+ },
+
+ async deleteItem(parent, args, ctx, info) {
+ console.log('gonna delete one');
+ console.log(args);
+ return ctx.db.mutation.deleteItem(
+ {
+ where: {
+ id: args.id,
+ },
+ },
+ info
+ );
+ },
+
+ async updateItem(parent, args, ctx, info) {
+ console.log('Updating an item');
+ const updates = { ...args };
+ delete updates.id;
+ return ctx.db.mutation.updateItem({
+ where: { id: args.id },
+ data: {
+ ...updates,
+ },
+ });
+ },
+
async createDraft(parent, { title, text }, ctx, info) {
// const userId = getUserId(ctx);
const userId = getUserId(ctx);
diff --git a/backend/src/resolvers/Query.js b/backend/src/resolvers/Query.js
index 9d9d142..a2eb749 100644
--- a/backend/src/resolvers/Query.js
+++ b/backend/src/resolvers/Query.js
@@ -1,18 +1,26 @@
const { getUserId, Context } = require('../utils');
+const { forwardTo } = require('prisma-binding');
const Query = {
+ items(parent, args, ctx, info) {
+ // check auth
+ return ctx.db.query.items({ ...args }, info);
+ },
+
+ itemsConnection: forwardTo('db'),
+
feed(parent, args, ctx, info) {
- return ctx.db.query.posts({ where: { isPublished: true } }, info);
+ return ctx.db.query.posts({}, info);
},
drafts(parent, args, ctx, info) {
- const id = getUserId(ctx);
+ // const id = getUserId(ctx);
const where = {
isPublished: false,
- author: {
- id,
- },
+ // author: {
+ // id,
+ // },
};
return ctx.db.query.posts({ where }, info);
diff --git a/backend/src/schema.graphql b/backend/src/schema.graphql
index 788d490..993236e 100644
--- a/backend/src/schema.graphql
+++ b/backend/src/schema.graphql
@@ -1,10 +1,25 @@
-# import Post, Item, ItemCreateInput from './generated/prisma.graphql'
+# import ItemWhereInput, ItemOrderByInput, allItems, Post, Item, ItemCreateInput, ItemOrderByInput, ItemWhereInput from './generated/prisma.graphql'
type Query {
feed: [Post!]!
drafts: [Post!]!
post(id: ID!): Post!
me: User
+ itemsConnection(
+ where: ItemWhereInput
+ orderBy: ItemOrderByInput
+ skip: Int
+ after: String
+ before: String
+ first: Int
+ last: Int
+ ): ItemConnection!
+ items(
+ where: ItemWhereInput
+ orderBy: ItemOrderByInput
+ skip: Int
+ first: Int
+ ): [Item]!
}
# import Mutation from './generated/prisma.graphql'
@@ -16,8 +31,9 @@ type Mutation {
publish(id: ID!): Post!
deletePost(id: ID!): Post!
updatePost(id: ID!, title: String, text: String): Post!
- createItem(data: ItemCreateInput): Item!
+ createItem(title: String, description: String, price: Int): Item!
deleteItem(id: ID!): Item!
+ updateItem(id: ID!, title: String, description: String, price: Int): Item!
}
type AuthPayload {
diff --git a/backend/tests/item.test.js b/backend/tests/item.test.js
new file mode 100644
index 0000000..0e47419
--- /dev/null
+++ b/backend/tests/item.test.js
@@ -0,0 +1,81 @@
+const { request } = require('graphql-request');
+
+let id;
+
+test('Creating an item', async () => {
+ const query = `
+ mutation createItem {
+ createItem(title: "wes", description:"Bos", price: 500) {
+ title
+ description
+ price
+ id
+ }
+ }
+ `;
+
+ const { createItem } = await request('http://localhost:4000', query);
+
+ expect(createItem.title).toEqual('wes');
+ expect(createItem.description).toEqual('Bos');
+ expect(createItem).toHaveProperty('id');
+ expect(createItem.price).toEqual(500);
+ id = createItem.id;
+});
+
+test('Get an array of items', async () => {
+ const query = `
+ query getAllItems {
+ items {
+ title
+ id
+ description
+ price
+ }
+ }
+ `;
+
+ const { items } = await request('http://localhost:4000', query);
+ expect(items.length).toBeGreaterThan(0);
+});
+
+test('Query a specific item', async () => {
+ const query = `
+ query findAnItem {
+ items(where: {
+ id: "${id}"
+ }) {
+ title
+ id
+ description
+ price
+ }
+ }
+ `;
+
+ const { items } = await request('http://localhost:4000', query);
+ const item = items[0];
+ expect(item.title).toEqual('wes');
+ expect(item.description).toEqual('Bos');
+ expect(item.price).toEqual(500);
+});
+
+// Test Delete that item
+test('delete an item', async () => {
+ const query = `
+ mutation remove {
+ deleteItem(id: "${id}") {
+ id
+ }
+ }
+ `;
+ const res = await request('http://localhost:4000', query);
+ expect(res.deleteItem.id).toEqual(id);
+});
+
+// mutation updateItem {
+// updateItem(id: "cjd21afpr47m00172nigtlkw8", title: "WES from playground") {
+// id
+// title
+// }
+// }