Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
feat: complete implementation of graphql fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
batrdn committed Mar 23, 2024
1 parent 98a551d commit 719b7b9
Show file tree
Hide file tree
Showing 34 changed files with 13,847 additions and 2,163 deletions.
6,398 changes: 4,345 additions & 2,053 deletions package-lock.json

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions packages/core/src/core-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,92 @@ export abstract class CoreFixture<T> {

return [this.fakerApi.generate(fieldName, type, rule, isAccurate)];
}

/**
* Extracts field paths or patterns from `overrideValues` and `options` to be used for validation and generation.
*
* @remarks
*
* The `path` values extracted from `options` and `overrideValues` may either be a field path or a pattern.
* For example, `address.street` is a field path, and `address.*` is a pattern.
* The naming, however, still refers to them as paths for simplicity.
*
* @see CorePathFinder
*
* @param overrideValues Values to override in the schema.
* @param options Fixture generation options.
*
* @returns An array of field paths found from `overrideValues` and `options`.
*
* @protected
*/
protected extractPaths(
overrideValues: Record<FieldPath, Value> | undefined,
options: FixtureOptions | undefined,
): FieldPath[] {
const overridePaths = overrideValues ? Object.keys(overrideValues) : [];
const excludePaths = options?.exclude || [];
const rulePaths = options?.rules?.map(({ path }) => path) || [];

return [...overridePaths, ...excludePaths, ...rulePaths];
}

/**
* Overrides a value, or to be more specific, returns the override value found in the path.
*
* @param path The path to the field.
* @param overrideValues Values to override in the schema.
*
* @returns The override value for the field or `undefined` if it does not exist.
*
* @protected
*/
protected overrideValue(
path: FieldPath,
overrideValues: Record<FieldPath, Value> | undefined,
): Value | undefined {
if (!overrideValues) {
return undefined;
}

const overrideValuePatterns = Object.keys(overrideValues);
const matchingPatterns = this.pathfinder.findPatterns(
path,
overrideValuePatterns,
);

if (matchingPatterns.length > 1) {
throw new Error(
`Forbidden: multiple override values found for path '${path}': ${matchingPatterns.join(
', ',
)}`,
);
}

const overrideKey = matchingPatterns[0];

return Object.prototype.hasOwnProperty.call(overrideValues, overrideKey)
? overrideValues[overrideKey]
: undefined;
}

/**
* Checks if a value should be overridden.
*
* @param path The path to the field.
* @param overrideValues Values to override in the schema.
*
* @returns `true` If a field (path) is found in the `overrideValues`, it should be overridden.
*
* @protected
*/
protected shouldOverride(
path: FieldPath,
overrideValues: Record<FieldPath, Value> | undefined,
): boolean {
return (
!!overrideValues &&
this.pathfinder.exists(path, Object.keys(overrideValues))
);
}
}
6 changes: 3 additions & 3 deletions packages/core/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
"test/**/*.test.ts",
"test/**/*.spec.ts",
"test/**/*.d.ts"
]
}
16 changes: 16 additions & 0 deletions packages/graphql/codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
schema:
- https://api.cartql.com
- ./test/graphql/alternative-schema.graphql
documents: './test/**/*.graphql'
generates:
./test/generated/types.ts:
plugins:
- typescript
- typescript-operations
- typed-document-node
./test/generated/schema.graphql:
plugins:
- schema-ast
./test/generated/schema.json:
plugins:
- introspection
22 changes: 21 additions & 1 deletion packages/graphql/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
{
"name": "@mocking-bird/graphql",
"version": "0.0.1",
"scripts": {
"typecheck": "tsc -p tsconfig.json --noEmit",
"test": "nx test @mocking-bird/graphql",
"codegen": "graphql-codegen"
},
"dependencies": {
"tslib": "^2.3.0"
},
"peerDependencies": {
"@graphql-typed-document-node/core": ">=3",
"graphql": ">=15"
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts"
"typings": "./src/index.d.ts",
"devDependencies": {
"@apollo/client": "^3.9.5",
"@graphql-codegen/cli": "^5.0.2",
"@graphql-codegen/introspection": "^4.0.3",
"@graphql-codegen/schema-ast": "^4.0.2",
"@graphql-codegen/typed-document-node": "^5.0.6",
"@graphql-codegen/typescript": "^4.0.6",
"@graphql-codegen/typescript-operations": "^4.2.0",
"cross-fetch": "^4.0.0",
"nock-graphql": "^2.1.0"
}
}
Loading

0 comments on commit 719b7b9

Please sign in to comment.