Skip to content

Commit

Permalink
Add {defaultValue} template (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzi committed Jan 20, 2023
1 parent 6ba58a4 commit a7ba64c
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
4 changes: 4 additions & 0 deletions changelog.md
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added

- Nullable fields can now be initialized by setting the `maybeValueDefault` option to `"{defaultValue}"`

## 1.1.0 - 2023-01-19

### Added
Expand Down
56 changes: 56 additions & 0 deletions docs/docs/configuration/schema.mdx
Expand Up @@ -375,6 +375,60 @@ export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
</div>
</details>

This option can also be used to initialize nullable fields, instead of defaulting to a static value:

```yml
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
- graphql-codegen-factories/schema
config:
# highlight-start
maybeValueDefault: "{defaultValue}"
# highlight-end
```

<details>
<summary>Example</summary>

<div className="codeBlocks">

```graphql title="schema.graphql"
type Post {
author: PostAuthor
}

type PostAuthor {
username: String
}
```

```typescript title="types.ts"
export function createPost(props: Partial<Post> = {}): Post {
return {
// highlight-start
author: createPostAuthor(),
// highlight-end
...props,
};
}

export function createPostAuthor(props: Partial<PostAuthor> = {}): Post {
return {
// highlight-start
username: "",
// highlight-end
...props,
};
}
```

</div>
</details>

## `config.inputMaybeValueDefault`

By default, inputs' nullable fields are initialized as `config.maybeValueDefault` ("null" by default).
Expand Down Expand Up @@ -420,6 +474,8 @@ export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
</div>
</details>

See `config.maybeValueDefault` to initialize nullable input fields by setting `config.inputMaybeValueDefault` to `"{defaultValue}"`.

## `config.disableDescriptions`

By default, objects' and inputs' description is added above the factory function.
Expand Down
Expand Up @@ -224,6 +224,13 @@ export class FactoriesSchemaVisitor extends FactoriesBaseVisitor<
).string;
}

protected convertNullableDefaultValue(
nullableDefaultValue: string,
defaultValue: string
) {
return nullableDefaultValue.replace("{defaultValue}", defaultValue);
}

protected convertField(
node: UnvisitedFieldDefinitionNode | UnvisitedInputValueDefinitionNode,
nullableDefaultValue: string
Expand All @@ -232,7 +239,12 @@ export class FactoriesSchemaVisitor extends FactoriesBaseVisitor<
return indent(
indent(
`${node.name.value}: ${
isNullable ? nullableDefaultValue : defaultValue
isNullable
? this.convertNullableDefaultValue(
nullableDefaultValue,
defaultValue
)
: defaultValue
},`
)
);
Expand Down
Expand Up @@ -214,6 +214,35 @@ export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
}
`;
exports[`plugin should customize the maybe value default with default value 1`] = `
Object {
"content": "export function createPostMock(props: Partial<Post> = {}): Post {
return {
__typename: \\"Post\\",
author: createPostAuthorMock({}),
...props,
};
}
export function createPostAuthorMock(props: Partial<PostAuthor> = {}): PostAuthor {
return {
__typename: \\"PostAuthor\\",
username: \\"\\",
...props,
};
}
export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
return {
author: createPostAuthorMock({}),
...props,
};
}
",
"prepend": Array [],
}
`;
exports[`plugin should disable descriptions 1`] = `
Object {
"content": "export function createPostMock(props: Partial<Post> = {}): Post {
Expand Down
19 changes: 19 additions & 0 deletions packages/graphql-codegen-factories/src/schema/__tests__/plugin.ts
Expand Up @@ -150,6 +150,25 @@ describe("plugin", () => {
expect(output).toMatchSnapshot();
});

it("should customize the maybe value default with default value", async () => {
const schema = buildSchema(/* GraphQL */ `
type Post {
author: PostAuthor
}
type PostAuthor {
username: String
}
input PostInput {
author: PostAuthor
}
`);

const output = await plugin(schema, [], {
maybeValueDefault: "{defaultValue}",
});
expect(output).toMatchSnapshot();
});

it("should customize the input maybe value default", async () => {
const schema = buildSchema(/* GraphQL */ `
input PostInput {
Expand Down

0 comments on commit a7ba64c

Please sign in to comment.