@newmo/graphql-codegen-fake-server-client
v0.14.0
Published
GraphQL Codegen plugin for generating a fake server client
Downloads
2,264
Readme
@newmo/graphql-codegen-fake-server-client
GraphQL Code Generator plugin that generates fake client for @newmo/graphql-fake-server .
Installation
npm add --save-dev @newmo/graphql-codegen-fake-server-client
# This plugin depends on @graphql-codegen/client-preset
npm install @graphql-codegen/cli @graphql-codegen/client-preset --save-dev
GraphQL Code Generator configuration:
import type { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
overwrite: true,
schema: "./api/graphql/api.graphqls",
documents: "./api/graphql/query.graphql",
generates: {
"./generated/": {
preset: "client"
},
"./generated/fake-client.ts": {
plugins: ["@newmo/graphql-codegen-fake-server-client"],
config: {
// Required: path to the generated client's graphql file
typesFile: "./graphql"
},
},
},
};
export default config;
You can use ./generated/fake-client.ts
to register the fake to the fake server.
import { it, expect } from "vitest";
import { createFakeClient } from "./generated/fake-client";
const fakeClient = createFakeClient({
fakeServerEndpoint: "http://localhost:4000"
})
it("register fake response for query", async () => {
const sequenceId = crypto.randomUUID();
// register fake response for GetBooks query
const resRegister = await fakeClient.registerGetBooksQueryResponse(sequenceId, {
books: [
{
id: "new id",
title: "new title",
},
],
});
expect(resRegister).toMatchInlineSnapshot(`"{"ok":true}"`);
// request to server
const client = new GraphQLClient(`${fakeServerUrl}/graphql`, {
headers: {
"sequence-id": sequenceId,
},
});
// Got fake response
const response = await client.request(GetBooksDocument);
expect(response).toMatchInlineSnapshot(`
{
"books": [
{
"id": "new id",
"title": "new title",
},
],
}
`);
// Get actual request and response for testing
const calledResults = await fakeClient.calledGetBooksDocumentQuery(sequenceId);
console.log(calledResults[0].request);
console.log(calledResults[0].response);
});
Options
typesFile
(required): Path to the generated client's graphql file.fakeServerEndpoint
(optional): Fake server endpoint. Default ishttp://127.0.0.1:4000/fake
.namingConvention
(optional): Naming convention for the generated types. Default ischange-case#pascalCase
.typesPrefix
(optional): Prefix for the generated types.typesSuffix
(optional): Suffix for the generated types.
License
MIT