Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL upgrade and deterministic enum mocks #1

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
},
"dependencies": {
"fast-json-stable-stringify": "^2.1.0",
"graphql": "^15.0.0",
"graphql": "^16.9.0",
"seedrandom": "^3.0.5"
}
}
9 changes: 8 additions & 1 deletion src/__tests__/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { GraphQLError } from "graphql";
import schema from "./schema";
import schemaAST from "./schema-ast.json";

const seed = 'seed';

describe("Automocking", () => {
describe("Guardrails", () => {
test("it throws without a valid schema", () => {
Expand Down Expand Up @@ -89,10 +91,15 @@ describe("Automocking", () => {
returnEnum
}
`;
const resp: any = ergonomock(schema, testQuery);
const resp: any = ergonomock(schema, testQuery, { seed });
expect(resp.data).toMatchObject({
returnEnum: expect.toBeOneOf(["A", "B", "C"]),
});

for (let i = 0; i < 10; ++i) {
const resp2 = ergonomock(schema, testQuery, { seed });
expect(resp).toEqual(resp2);
}
});

test("can automock unions", () => {
Expand Down
8 changes: 3 additions & 5 deletions src/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
parse,
execute,
GraphQLType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
getNamedType,
Expand All @@ -20,7 +19,6 @@ import {
buildASTSchema,
} from "graphql";
import random from "./utils/random";
import getRandomElement from "./utils/getRandomElement";
import forEachFieldInQuery from "./utils/forEachFieldInQuery";

const defaultMockMap: Map<string, GraphQLFieldResolver<any, any>> = new Map();
Expand Down Expand Up @@ -118,7 +116,7 @@ export function ergonomock(
if (fieldType instanceof GraphQLUnionType || fieldType instanceof GraphQLInterfaceType) {
let implementationType;
const possibleTypes = schema.getPossibleTypes(fieldType);
implementationType = getRandomElement(possibleTypes);
implementationType = random.item(possibleTypes);
return Object.assign(
{ __typename: implementationType },
mockResolverFunction(implementationType)(root, args, context, info)
Expand All @@ -131,7 +129,7 @@ export function ergonomock(

// Default mock for enums
if (fieldType instanceof GraphQLEnumType) {
return getRandomElement(fieldType.getValues()).value;
return random.item(fieldType.getValues()).value;
}

// Automock object types
Expand Down Expand Up @@ -190,7 +188,7 @@ function assignResolveType(type: GraphQLType) {
// the default `resolveType` always returns null. We add a fallback
// resolution that works with how unions and interface are mocked
namedFieldType.resolveType = (data: any, context: any, info: GraphQLResolveInfo) => {
return info.schema.getType(data.__typename) as GraphQLObjectType;
return info.schema.getType(data.__typename)?.name;
};
}
}
2 changes: 1 addition & 1 deletion src/utils/forEachFieldInQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function forEachFieldInQuery(
if (isAbstractType(parentType)) {
const possibleTypes = schema.getPossibleTypes(parentType);
possibleTypes.forEach(t => {
const fieldDef = getFieldDef(schema, t, fieldName);
const fieldDef = getFieldDef(schema, t, node);
if (fieldDef) {
fn(fieldDef, t.name, fieldName);
}
Expand Down
4 changes: 0 additions & 4 deletions src/utils/getRandomElement.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/utils/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ const random = {
// max is exclusive, min is inclusive
float: (max: number = 100, min: number = 1) => rng() * (max - min) + min,

item: <T>(array: readonly T[]): T => array[random.integer(array.length, 0)],

// max and min are inclusive
list: (maxLength: number = 4, minLength: number = 1) => [
...Array(random.integer(maxLength + 1, minLength))
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2964,10 +2964,10 @@ graphql-tag@^2.12.3:
dependencies:
tslib "^2.1.0"

graphql@^15.0.0:
version "15.0.0"
resolved "https://registry.npmjs.org/graphql/-/graphql-15.0.0.tgz"
integrity sha512-ZyVO1xIF9F+4cxfkdhOJINM+51B06Friuv4M66W7HzUOeFd+vNzUn4vtswYINPi6sysjf1M2Ri/rwZALqgwbaQ==
graphql@^16.9.0:
version "16.9.0"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.9.0.tgz#1c310e63f16a49ce1fbb230bd0a000e99f6f115f"
integrity sha512-GGTKBX4SD7Wdb8mqeDLni2oaRGYQWjWHGKPQ24ZMnUtKfcsVoiv4uX8+LJr1K6U5VW2Lu1BwJnj7uiori0YtRw==

growly@^1.3.0:
version "1.3.0"
Expand Down
Loading