Skip to content

Commit

Permalink
test: provide test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstnmcf committed Sep 26, 2024
1 parent 6c56298 commit db3f42b
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 1 deletion.
29 changes: 28 additions & 1 deletion test/fixtures/authors.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,21 @@ const schema = `
list: [Author]
}
interface BaseError {
message: String!
}
type NotFoundError implements BaseError {
message: String!
}
union UpdateAuthorAddressResponse = Author | NotFoundError
type Mutation {
createAuthor(author: AuthorInput!): Author!
batchCreateAuthor(authors: [AuthorInput]!): [Author]!
publishBlogPost(authorId: ID!): Boolean!
updateAuthorAddress(authorId: ID!, address: AuthorAddressInput!): UpdateAuthorAddressResponse!
}
`

Expand Down Expand Up @@ -145,7 +156,23 @@ const resolvers = {
}
})

return { success: true }
return true
},

async updateAuthorAddress (_, { authorId, address }, context) {
const author = data.authors[authorId]
if (!author) {
return {
__typename: 'NotFoundError',
message: 'Author not found'
}
}

// we actually don't update the author, just return it
return {
__typename: 'Author',
...author
}
}
},
Author: {
Expand Down
100 changes: 100 additions & 0 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,106 @@ test('mutations', async (t) => {
}
]
}
},

{
name: 'should run a mutation query with union type response',
query: `
mutation UpdateAuthorAddress($authorId: ID!, $address: AuthorAddressInput!) {
updateAuthorAddress(authorId: $authorId, address: $address) {
__typename
...on Author {
id name { firstName lastName }
}
... on NotFoundError {
message
}
}
}
`,
variables: {
authorId: '1',
address: {
street: 'Johnson Street 5',
city: 'Johnson City',
zip: 4200,
country: 'US',
mainResidence: true
}
},
result: {
updateAuthorAddress: {
__typename: 'Author',
id: '1',
name: { firstName: 'Peter', lastName: 'Pluck' }
}
}
},
{
name: 'should run a mutation query with union type response and return NotFoundError',
query: `
mutation UpdateAuthorAddress($authorId: ID!, $address: AuthorAddressInput!) {
updateAuthorAddress(authorId: $authorId, address: $address) {
__typename
... on Author {
id name { firstName lastName }
}
... on NotFoundError {
message
}
}
}
`,
variables: {
authorId: '99',
address: {
street: 'Johnson Street 5',
city: 'Johnson City',
zip: 4200,
country: 'US',
mainResidence: true
}
},
result: {
updateAuthorAddress: {
__typename: 'NotFoundError',
message: 'Author not found'
}
}
},

{
name: 'should run a mutation query with fragment',
query: `
fragment authorFields on Author {
id name { firstName lastName }
}
mutation UpdateAuthorAddress($authorId: ID!, $address: AuthorAddressInput!) {
updateAuthorAddress(authorId: $authorId, address: $address) {
__typename
...authorFields
... on NotFoundError {
message
}
}
}
`,
variables: {
authorId: '99',
address: {
street: 'Johnson Street 5',
city: 'Johnson City',
zip: 4200,
country: 'US',
mainResidence: true
}
},
result: {
updateAuthorAddress: {
__typename: 'NotFoundError',
message: 'Author not found'
}
}
}
]

Expand Down

0 comments on commit db3f42b

Please sign in to comment.