Updates contacts SDL/service to latest generated version
This commit is contained in:
@@ -8,7 +8,8 @@ export const schema = gql`
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Query {
|
type Query {
|
||||||
contacts: [Contact!]! @skipAuth
|
contacts: [Contact!]! @requireAuth
|
||||||
|
contact(id: Int!): Contact @requireAuth
|
||||||
}
|
}
|
||||||
|
|
||||||
input CreateContactInput {
|
input CreateContactInput {
|
||||||
@@ -24,6 +25,8 @@ export const schema = gql`
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Mutation {
|
type Mutation {
|
||||||
createContact(input: CreateContactInput!): Contact @skipAuth
|
createContact(input: CreateContactInput!): Contact! @requireAuth
|
||||||
|
updateContact(id: Int!, input: UpdateContactInput!): Contact! @requireAuth
|
||||||
|
deleteContact(id: Int!): Contact! @requireAuth
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|||||||
@@ -1,21 +1,33 @@
|
|||||||
import { UserInputError } from '@redwoodjs/graphql-server'
|
|
||||||
import { db } from 'src/lib/db'
|
import { db } from 'src/lib/db'
|
||||||
|
import { validate } from '@redwoodjs/api'
|
||||||
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'],
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const contacts = () => {
|
export const contacts = () => {
|
||||||
return db.contact.findMany()
|
return db.contact.findMany()
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createContact = ({ input }) => {
|
export const contact = ({ id }) => {
|
||||||
validate(input)
|
return db.contact.findUnique({
|
||||||
return db.contact.create({ data: input })
|
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 },
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ export const standard = defineScenario({
|
|||||||
name: 'John Doe',
|
name: 'John Doe',
|
||||||
email: 'john.doe@example.com',
|
email: 'john.doe@example.com',
|
||||||
message: 'I love RedwoodJS',
|
message: 'I love RedwoodJS',
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -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', () => {
|
describe('contacts', () => {
|
||||||
scenario('returns a list of contacts', async (scenario) => {
|
scenario('returns all contacts', async (scenario) => {
|
||||||
const list = await contacts()
|
const result = await contacts()
|
||||||
|
|
||||||
expect(list.length).toEqual(Object.keys(scenario.contact).length)
|
expect(result.length).toEqual(Object.keys(scenario.contact).length)
|
||||||
expect(list[0].email).toEqual(scenario.contact.john.email)
|
})
|
||||||
|
|
||||||
|
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user