From 52e80526eb77d666af1672c407f5c2f5f6c2c773 Mon Sep 17 00:00:00 2001 From: Rob Cameron Date: Fri, 25 Mar 2022 10:42:48 -0700 Subject: [PATCH] Updates contacts SDL/service to latest generated version --- api/src/graphql/contacts.sdl.js | 7 ++- api/src/services/contacts/contacts.js | 40 ++++++++----- .../services/contacts/contacts.scenarios.js | 2 +- api/src/services/contacts/contacts.test.js | 58 +++++++++++++++++-- 4 files changed, 85 insertions(+), 22 deletions(-) diff --git a/api/src/graphql/contacts.sdl.js b/api/src/graphql/contacts.sdl.js index 30e3cab..c6b4824 100644 --- a/api/src/graphql/contacts.sdl.js +++ b/api/src/graphql/contacts.sdl.js @@ -8,7 +8,8 @@ export const schema = gql` } type Query { - contacts: [Contact!]! @skipAuth + contacts: [Contact!]! @requireAuth + contact(id: Int!): Contact @requireAuth } input CreateContactInput { @@ -24,6 +25,8 @@ export const schema = gql` } type Mutation { - createContact(input: CreateContactInput!): Contact @skipAuth + createContact(input: CreateContactInput!): Contact! @requireAuth + updateContact(id: Int!, input: UpdateContactInput!): Contact! @requireAuth + deleteContact(id: Int!): Contact! @requireAuth } ` diff --git a/api/src/services/contacts/contacts.js b/api/src/services/contacts/contacts.js index 5428b8d..4a326ad 100644 --- a/api/src/services/contacts/contacts.js +++ b/api/src/services/contacts/contacts.js @@ -1,21 +1,33 @@ -import { UserInputError } from '@redwoodjs/graphql-server' import { db } from 'src/lib/db' - -const validate = (input) => { - if (input.email && !input.email.match(/[^@]+@[^.]+\..+/)) { - throw new UserInputError("Can't create new contact",{ - messages: { - email: ['is not formatted like an email address'], - }, - }) - } -} +import { validate } from '@redwoodjs/api' export const contacts = () => { return db.contact.findMany() } -export const createContact = ({ input }) => { - validate(input) - return db.contact.create({ data: input }) +export const contact = ({ id }) => { + return db.contact.findUnique({ + where: { id }, + }) +} + +export const createContact = ({ input }) => { + validate(input.email, 'email', { email: true }) + + return db.contact.create({ + data: input, + }) +} + +export const updateContact = ({ id, input }) => { + return db.contact.update({ + data: input, + where: { id }, + }) +} + +export const deleteContact = ({ id }) => { + return db.contact.delete({ + where: { id }, + }) } diff --git a/api/src/services/contacts/contacts.scenarios.js b/api/src/services/contacts/contacts.scenarios.js index ce89852..6a8dd45 100644 --- a/api/src/services/contacts/contacts.scenarios.js +++ b/api/src/services/contacts/contacts.scenarios.js @@ -5,7 +5,7 @@ export const standard = defineScenario({ name: 'John Doe', email: 'john.doe@example.com', message: 'I love RedwoodJS', - } + }, }, }, }) diff --git a/api/src/services/contacts/contacts.test.js b/api/src/services/contacts/contacts.test.js index 8708b0c..6cf1ed9 100644 --- a/api/src/services/contacts/contacts.test.js +++ b/api/src/services/contacts/contacts.test.js @@ -1,10 +1,58 @@ -import { contacts } from './contacts' +import { + contacts, + contact, + createContact, + updateContact, + deleteContact, +} from './contacts' + +// Generated boilerplate tests do not account for all circumstances +// and can fail without adjustments, e.g. Float and DateTime types. +// Please refer to the RedwoodJS Testing Docs: +// https://redwoodjs.com/docs/testing#testing-services +// https://redwoodjs.com/docs/testing#jest-expect-type-considerations describe('contacts', () => { - scenario('returns a list of contacts', async (scenario) => { - const list = await contacts() + scenario('returns all contacts', async (scenario) => { + const result = await contacts() - expect(list.length).toEqual(Object.keys(scenario.contact).length) - expect(list[0].email).toEqual(scenario.contact.john.email) + expect(result.length).toEqual(Object.keys(scenario.contact).length) + }) + + scenario('returns a single contact', async (scenario) => { + const result = await contact({ id: scenario.contact.john.id }) + + expect(result).toEqual(scenario.contact.john) + }) + + scenario('creates a contact', async () => { + const result = await createContact({ + input: { + name: 'Jane Doe', + email: 'jane@anonymous.com', + message: 'RedwoodJS is the best', + }, + }) + + expect(result.name).toEqual('Jane Doe') + expect(result.email).toEqual('jane@anonymous.com') + expect(result.message).toEqual('RedwoodJS is the best') + }) + + scenario('updates a contact', async (scenario) => { + const original = await contact({ id: scenario.contact.john.id }) + const result = await updateContact({ + id: original.id, + input: { name: 'Johnathan Doe' }, + }) + + expect(result.name).toEqual('Johnathan Doe') + }) + + scenario('deletes a contact', async (scenario) => { + const original = await deleteContact({ id: scenario.contact.john.id }) + const result = await contact({ id: original.id }) + + expect(result).toEqual(null) }) })