0.14.0 • Published 8 months ago

@newmo/graphql-codegen-fake-server-client v0.14.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

@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 is http://127.0.0.1:4000/fake.
  • namingConvention (optional): Naming convention for the generated types. Default is change-case#pascalCase.
  • typesPrefix (optional): Prefix for the generated types.
  • typesSuffix (optional): Suffix for the generated types.

License

MIT

0.14.0

8 months ago

0.13.0

9 months ago

0.9.7

1 year ago

0.10.0

1 year ago

0.9.5

1 year ago

0.11.0

11 months ago

0.9.0

1 year ago

0.12.0

11 months ago

0.8.0

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago