1.0.2 • Published 5 years ago

@world-kit/gql-mock v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Gql-mock

License: MIT NPM version

You have backend and frontend teams...so Is frontend always waiting backend? We solved this problem, now frontend make mocks and do software faster. Parallel work is real! This util for generating mocks by source Graphql schema.

Usage

npm i -S @world-kit/gql-mock
import { GQLMock } from "@world-kit/gql-mock";
...
  const gqlMock = new GQLMock(schema);

The simple example with json-server

// run json server on 9999 port
npx json-server -w db.json -p 9999
import { GQLMock } from "@world-kit/gql-mock";

getMock().then(preparedMocks => {
  const gqlMock = new GQLMock(schema, {
    preparedMocks
  });

  // for example use case
  addMockFunctionsToSchema({
    schema,
    mocks: gqlMock.funcMocks,
    preserveResolvers: true
  });
});

Graphql schema

# graphql-schema.gql

scalar JSON
scalar Number

type Query {
  test: TEST
  test2: TEST
}

type TEST {
  name: String
  json: JSON
  object: OBJECT
  number: Number
  mock: MOCK
}

type OBJECT {
  value: String
}

type MOCK {
  value: String
  value_33: String
}

Result contains all mocks for your schema. Like this:

// db.json
{
  "users": [
    {
      "data": {
        "Query": {
          "test": {
            "name": "Hello World",
            "json": {
              "value": "JSON"
            },
          },
          "test2": {
            "name": "Hello World",
            "json": {
              "value": "JSON"
            },
          }
          ...
        }
      }
    }
  ]
}
  • We can change any fields in db.json and for next start your application will be work with current mocks.
  • If you put '{}' as value for any field, then it'll use data from root type.

Options of GQLMock function

  • preparedMocks - setter for an init state of mocks.
  • defaultTypesValue - Map of default values for types, where key is a typename and value is a mock function.

    // for example
    // custom values for default types
    const defaultValues = new Map<string, IMockFn>([
      ["Number", () => 5555],
      ["JSON", () => ({ value: "JSON" })],
      ["String", () => "string"]
    ]);
    
    const gqlMock = new GQLMock(schema, {
      defaultTypesValue: defaultValues
    });

Avalible fields of GQLMock instance

  • mocks - generated mocks by GQL schema.
  • funcMocks - It's mocks transformed into an object where every field is a function which returns a mock value for this field.

Credits

This tool is inspired by graphql-tools project.

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.0-alpha.0

5 years ago

0.4.1

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.1-alpha.5

5 years ago