Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
fix: use long names for trail graphql resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangoacher committed Mar 4, 2020
1 parent 16043a5 commit cc4df72
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 38 deletions.
18 changes: 9 additions & 9 deletions packages/trail-fastify-graphql-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ The schema provides the following queries:

* `trail(id: Int!)`: Fetch a trail record by ID.
* `trails(from: Date!, to: Date!, ...)`: Search for trail records within a specified date range, and optionally filter by additional values.
* `enumerate(from: Date!, to: Date!, type: TrailType!, ...)`: Return an enumeration of trails of the specified type, within the specified date range.
* `enumerateTrails(from: Date!, to: Date!, type: TrailType!, ...)`: Return an enumeration of trails of the specified type, within the specified date range.

### Mutations

The schema provides the following mutations:

* `insert(when: Date!, who: StringWithAttrs!, what: StringWithAttrs!, subject: StringWithAttrs!, where: JSON, why: JSON, meta: JSON)`: Insert a new trail record.
* `update(id: Int!, when: Date!, who: StringWithAttrs!, what: StringWithAttrs!, subject: StringWithAttrs!, where: JSON, why: JSON, meta: JSON)`: Update a trail record.
* `remove(id: Int!)`: Delete a trail record.
* `insertTrail(when: Date!, who: StringWithAttrs!, what: StringWithAttrs!, subject: StringWithAttrs!, where: JSON, why: JSON, meta: JSON)`: Insert a new trail record.
* `updateTrail(id: Int!, when: Date!, who: StringWithAttrs!, what: StringWithAttrs!, subject: StringWithAttrs!, where: JSON, why: JSON, meta: JSON)`: Update a trail record.
* `deleteTrail(id: Int!)`: Delete a trail record.

### String values

Expand Down Expand Up @@ -89,15 +89,15 @@ For example:

```graphql
{
enumerate(from: "2018-01-01T12:34:56.000Z", to: "2018-01-05T12:34:56.000Z", type: WHO)
enumerateTrails(from: "2018-01-01T12:34:56.000Z", to: "2018-01-05T12:34:56.000Z", type: WHO)
}
```

### Insert a new trail record

```graphql
mutation {
insert(when: "2018-01-01T12:34:56.000Z", who: "A Person", what: "A thing", subject: "Substance") {
insertTrail(when: "2018-01-01T12:34:56.000Z", who: "A Person", what: "A thing", subject: "Substance") {
id
when
who
Expand All @@ -114,7 +114,7 @@ mutation {

```graphql
mutation {
insert(when: "2018-01-01T12:34:56.000Z", who: { id: "A Person", attr: 10 }, what: { id: "A thing", attr: 20 }, subject: "Substance") {
insertTrail(when: "2018-01-01T12:34:56.000Z", who: { id: "A Person", attr: 10 }, what: { id: "A thing", attr: 20 }, subject: "Substance") {
id
when
who
Expand All @@ -131,15 +131,15 @@ mutation {

```graphql
mutation {
update(id: 123, when: "2018-01-01T12:34:56.000Z", who: "A N Other", what: "Something else", subject: "Object")
updateTrail(id: 123, when: "2018-01-01T12:34:56.000Z", who: "A N Other", what: "Something else", subject: "Object")
}
```

### Delete a trail record

```graphql
mutation {
remove(id: 123)
deleteTrail(id: 123)
}
```

Expand Down
18 changes: 8 additions & 10 deletions packages/trail-fastify-graphql-plugin/lib/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const typeDefs = `
caseInsensitive: Boolean
): [Trail!]!
enumerate(
enumerateTrails(
from: Date!
to: Date!
type: TrailType!
Expand All @@ -133,7 +133,7 @@ const typeDefs = `
type Mutation {
insert(
insertTrail(
when: Date!
who: StringWithAttrs!
what: StringWithAttrs!
Expand All @@ -143,7 +143,7 @@ const typeDefs = `
meta: JSON
): Trail
update(
updateTrail(
id: Int!
when: Date
who: StringWithAttrs
Expand All @@ -154,7 +154,7 @@ const typeDefs = `
meta: JSON
): Boolean!
remove(id: Int!): Boolean!
deleteTrail(id: Int!): Boolean!
}
`
Expand All @@ -174,26 +174,24 @@ function makeResolvers (opts) {
trails (_, args) {
return trailsManager.search(args)
},
enumerate (_, args) {
enumerateTrails (_, args) {
return trailsManager.enumerate(args)
}
},
Mutation: {
async insert (_, trail) {
async insertTrail (_, trail) {
const id = await trailsManager.insert(trail)
return id ? trailsManager.get(id) : null
},
async update (_, { id, ...trail }) {
async updateTrail (_, { id, ...trail }) {
const record = await trailsManager.get(id)
if (!record) {
return false
}
const { id: x, ...fields } = record
return trailsManager.update(id, { ...fields, ...trail })
},
// NOTE: This resolver should be called 'delete' but graphql-jit has problems
// with the name (presumably because of clash with the JS 'delete' keyword).
remove (_, { id }) {
deleteTrail (_, { id }) {
return trailsManager.delete(id)
}
},
Expand Down
74 changes: 57 additions & 17 deletions packages/trail-fastify-graphql-plugin/test/graphql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ describe('GraphQL', () => {
const from = records[0].when
const to = records[records.length - 1].when

const { data: { enumerate } } = await this.subject.execQuery(`{
enumerate(from: "${from}", to: "${to}", type: WHO)
const { data: { enumerateTrails } } = await this.subject.execQuery(`{
enumerateTrails(from: "${from}", to: "${to}", type: WHO)
}`)

const expected = records.map(r => r.who).sort().filter((w, i, a) => w !== a[i - 1])

expect(enumerate).to.equal(expected)
expect(enumerateTrails).to.equal(expected)
})

test('get trails multiple concurrent', async () => {
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('GraphQL', () => {
const subject = 'window'

const { data: { trail } } = await this.subject.execQuery(`mutation {
trail: insert(when: "${when}", who: "${who}", what: "${what}", subject: "${subject}") {
trail: insertTrail(when: "${when}", who: "${who}", what: "${what}", subject: "${subject}") {
id
when
who
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('GraphQL', () => {
$what: StringWithAttrs!
$subject: StringWithAttrs!
) {
trail: insert(when: $when, who: $who, what: $what, subject: $subject) {
trail: insertTrail(when: $when, who: $who, what: $what, subject: $subject) {
id
when
who
Expand All @@ -235,13 +235,53 @@ describe('GraphQL', () => {
test('insert invalid', async () => {
try {
await this.subject.execQuery(`mutation {
insert(when: "")
insertTrail(when: "")
}`)
} catch (e) {
expect(e.message).to.equal('Query compilation error: Argument "who" of required type "StringWithAttrs!" was not provided.')
}
})

test('insert with JSON values', async () => {
const when = '2018-01-01T12:34:56.000Z'
const who = 'dog'
const what = 'open'
const subject = 'window'
const where = `{ where: "there" }`
const why = `{ reason: "because" }`
const meta = `{ meta0: 0, meta1: "one", meta2: [ 2 ] }`

const { data: { trail } } = await this.subject.execQuery(`mutation {
trail: insertTrail(when: "${when}", who: "${who}", what: "${what}", subject: "${subject}", where: ${where}, why: ${why}, meta: ${meta}) {
id
when
who
what
subject
meta
where
why
}
}`)

const { id } = trail

expect(id).to.be.a.number()

const expected = convertToTrail({
id,
when,
who,
what,
subject,
where: { where: 'there' },
why: { reason: 'because' },
meta: { meta0: 0, meta1: 'one', meta2: [ 2 ] }
})

expect(trail).to.equal(expected)
})

test('update', async () => {
const when = '2018-01-01T12:34:56.000Z'
const who = 'dog'
Expand All @@ -251,7 +291,7 @@ describe('GraphQL', () => {

const newWhat = 'close'
const { data: { ok } } = await this.subject.execQuery(`mutation {
ok: update(id: ${id}, when: "${when}", who: "${who}", what: "${newWhat}", subject: "${subject}")
ok: updateTrail(id: ${id}, when: "${when}", who: "${who}", what: "${newWhat}", subject: "${subject}")
}`)

expect(ok).to.be.true()
Expand All @@ -269,7 +309,7 @@ describe('GraphQL', () => {
const newWhat = 'close'
const newSubject = 'door'
const { data: { ok } } = await this.subject.execQuery(`mutation {
ok: update(id: ${id}, what: "${newWhat}", subject: "${newSubject}")
ok: updateTrail(id: ${id}, what: "${newWhat}", subject: "${newSubject}")
}`)

expect(ok).to.be.true()
Expand All @@ -293,7 +333,7 @@ describe('GraphQL', () => {
$what: StringWithAttrs!
$subject: StringWithAttrs!
) {
ok: update(id: $id, when: $when, who: $who, what: $what, subject: $subject)
ok: updateTrail(id: $id, when: $when, who: $who, what: $what, subject: $subject)
}`, { id, when, who, what: newWhat, subject })

expect(ok).to.be.true()
Expand All @@ -310,29 +350,29 @@ describe('GraphQL', () => {

try {
await this.subject.execQuery(`mutation {
ok: update(id: ${id}, when: "")
ok: updateTrail(id: ${id}, when: "")
}`)
} catch (e) {
expect(e.message).to.equal('Query compilation error: Argument "who" of required type "StringWithAttrs!" was not provided.')
}
})

test('remove', async () => {
test('delete', async () => {
const record = { when: '2018-01-01T12:34:56.000Z', who: 'dog', what: 'open', subject: 'window' }
const [id] = await insertRecords(this, [record])

const { data: { ok } } = await this.subject.execQuery(`mutation {
ok: remove(id: ${id})
ok: deleteTrail(id: ${id})
}`)

expect(ok).to.be.true()
const trail = await getTrail(this, id)
expect(trail).to.be.null()
})

test('remove nonexisting', async () => {
test('delete nonexisting', async () => {
const { data: { ok } } = await this.subject.execQuery(`mutation {
ok: remove(id: 1)
ok: deleteTrail(id: 1)
}`)
expect(ok).to.be.false()
})
Expand All @@ -347,11 +387,11 @@ describe('GraphQL', () => {
const newWhat = 'close'

const { data } = await this.subject.execQuery(`mutation {
insert: insert(when: "${newTrail.when}", who: "${newTrail.who}", what: "${newTrail.what}", subject: "${newTrail.subject}") {
insert: insertTrail(when: "${newTrail.when}", who: "${newTrail.who}", what: "${newTrail.what}", subject: "${newTrail.subject}") {
id
}
update: update(id: ${ids[0]}, when: "${records[0].when}", who: "${records[0].who}", what: "${newWhat}", subject: "${records[0].subject}")
remove: remove(id: ${ids[1]})
update: updateTrail(id: ${ids[0]}, when: "${records[0].when}", who: "${records[0].who}", what: "${newWhat}", subject: "${records[0].subject}")
remove: deleteTrail(id: ${ids[1]})
}`)

const { id } = data.insert
Expand Down
4 changes: 2 additions & 2 deletions packages/trail-fastify-graphql-plugin/test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('Trails graphql HTTP operations', () => {
'Content-Type': 'application/graphql'
},
payload: `mutation {
trail: insert(when: "${when}", who: "${who}", what: "${what}", subject: "${subject}") {
trail: insertTrail(when: "${when}", who: "${who}", what: "${what}", subject: "${subject}") {
id
when
who
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('Trails graphql HTTP operations', () => {
},
payload: JSON.stringify({
query: `mutation insertTrail($when: Date!, $who: StringWithAttrs!, $what: StringWithAttrs!, $subject: StringWithAttrs!) {
trail: insert(when: $when, who: $who, what: $what, subject: $subject) {
trail: insertTrail(when: $when, who: $who, what: $what, subject: $subject) {
id
when
who
Expand Down

0 comments on commit cc4df72

Please sign in to comment.