# Customize services

Mock services can be customized to fit your specific needs.

A service is defined as a ServiceMock object. The following sections outline and explain the available options.

# API types

A real-life service will often provide multiple APIs, such as GraphQL, REST, or even HTML pages. Poser allows you to mock multiple APIs in each service.

# GraphQL

GraphQL APIs are first-class citizens in Poser. All data is mocked using the GraphQL schema and store.

Poser simply requires a GraphQL schema to auto-mock all fields. Custom mocks, resolvers and fixtures can also be added.

GraphQL documentation
/graphql/

# REST or JSON APIs

Support here is basic for the moment, but Poser allows you to define custom routes and responses in code. These mocks can also access the store to tap into the same state as your GraphQL mocks, including mutating state.

REST documentation
/rest/

# HTML files

Poser also provides basic support for rendering HTML pages. This is useful in scenarios where an application needs to "send" a user to another location or flows where a user will transit through another application, such as a payment process.

HTML documentation
/html/

# Extending services

Poser allows you to extend any provided service to prevent repetition and encourage code re-use.

To extend a service, use poser.extendService():

import { poser, faker, utils } from '@datacamp/poser';

// the "base" service
const myService = {
  graphql: {
    typeDefs: '...',
    mocks: {
      Book: {
        id: () => faker.datatype.string(7)
      }
    },
    resolvers: {
      Query: {
        books: () => utils.mockList(5)
      }
    }
  }
};

// register the "base" service
poser.registerService('my-service', myService);


// the extension
const myServiceExtension = {
  graphql: {
    resolvers: {
      Query: {
        books: () => utils.mockList(10)
      },
      Mutation: {
        updateBook: () => { ... }
      }
    }
  }
}

// extend the service
poser.extendService('my-service', myServiceExtension);

In the above snippet, the service my-service is extended:

  • The schema is already set in the base service, no need to add it again
  • mocks.Book.id will stay the same
  • Query.books will now return 10 items
  • mutations have been added