#
ServiceMock
ServiceMock
is a simple JavaScript object:
const myServiceMock = {
graphql: {
typeDefs: `
type Query {
books: [Book]
}
type Book {
id: Int
title: String
}
`
}
}
#
graphql
The only required keys are either graphql.typeDefs
or graphql.typeDefsPath
.
#
typeDefs or typeDefsPath
One of these two keys is required:
#
typeDefs
string
A string containing a GraphQL Schema definition in GSL.
#
typeDefsPath
string | URL
A string or URL
pointing to a file containing a GraphQL Schema definition in GSL.
Poser can help fetching schemas and transform them into GSL (when introspection is available), see CLI.
#
mocks
Record<string, Record<string, () => any>>
The mocks
object can provide a function for each type field. The object's keys must be Typenames, and values are objects with field names as keys.
Example (assuming the types Book
and User
exist in the schema):
const mocks = {
Book: {
title: () => faker.random.words(4)
},
User: {
email: () => faker.internet.email(),
username: () => faker.internet.userName()
}
}
Learn more about mocks and how they fit in the different levels of mocking
#
resolvers
(Store) => Record<string, Record<string, (src, args, ctx, info) => any>>
The resolvers
object, like the mocks
object, can provide a function for each type field. In addition to type fields, resolvers can also specify fields for the Query
and Mutation
types.
Example:
function resolvers(store) {
return {
Query: {
bookById: () => store.get('Book', id);
},
Mutation: {
updateBookTitle: (_, { bookId, title }) => {
store.set('Book', id, 'title', title);
return store.get('Book', id);
}
}
}
}
#
fixtures
{ [typeName: string]: Array<any> }
Fixtures allow you to pre-populate the store with entities. Fields not set in fixtures will be generated as usual.
const fixtures = {
Book: [
{ id: 1, title: 'The amazing story' },
{ id: 2, title: 'A great story' },
{ id: 3, title: 'Greatest letters assembled into words' }
],
User: [
{ id: 1, email: '1@datacamp.com', username: 'User One' },
{ id: 2, email: '2@datacamp.com', username: 'User Two' },
{ id: 3, email: '3@datacamp.com', username: 'User Three' },
]
}
#
typeDefsExtra
string
typeDefsExtra
is an optional property of graphql
. It allows to "extend" the graphQL schema with additional types.
This is useful when the schema does not expose enough types and fields for mocking.
It is currently only possible to add new types, not to extend existing types. typeDefsExtra
is simply appended to the schema.
#
router
(router: Router, store?: Store) => void
The router
option can be used to register custom URLs to mock rest endpoints.
The first argument is an Express Router.
If a graphql schema is setup (via graphql.typeDefs
or graphql.typeDepsPath
), the store will be passed as a second argument.
const myService = {
router: (router, store) => {
router.get('/foo', (req, res) => {
res.send({
foo: 'bar'
})
})
}
}
Read more about mocking rest endpoints in REST.
#
disableFrontpage
Boolean
When set to true, the service's "front page" (ie. the page at http://localhost:1000/{serviceName}) is disabled.
This option allows a router to handle that url itself, when mocking the root url is required.