Start a mock GraphQL server with zero configuration
All you need to get a mock server up and running is your schema, Kimera and a GraphQL Server.
const { ApolloServer, gql } = require("apollo-server");
const { getExecutableSchema } = require("@lola-tech/graphql-kimera");
const schema = gql`
type Query {
...
`;
const executableSchema = getExecutableSchema({ typeDefs: schema });
const apollo = new ApolloServer({
schema: executableSchema,
});
apollo.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Customize mocks with scenarios and builders
Kimera allows you to mock the response to any number of queries with a single scenario
object.
Mock GraphQL types with builders
.
const executableSchema = getExecutableSchema({
typeDefs,
mockProvidersFn: (context) => ({
scenario: {
// Mock the `rockets` query to
// return three rockets, the first named "Saturn V":
rockets: [{ name: "Saturn V" }, {}, {}],
me: { name: "Homer" }
},
builders: {
// "Rocket" fields that aren't addressed in the scenario
// are mocked using the "Rocket" builder:
Rocket: () => ({
model: ["Orion", "Apollo"][_.random(0, 1)],
name: "Rocket name",
}),
},
}),
});
Write resolvers only when you need to
You can write resolvers, but you don't need to.
When you do, you'll get easy access to the mocks through the store
.
const executableSchema = getExecutableSchema({
typeDefs: schema,
mockProvidersFn: (context) => ({
scenario: {
rockets: mockResolver(
(store) => (_, { model }) => {
const rockets = store.get();
return rockets.filter((r) => r.model === model);
},
// You'll even be able to specify how the mocks are built.
[{ model: "Shuttle" }, {}, { model: "Shuttle" }]
),
},
}),
});