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

client-preset: Plugin page with advanced doc on Fragment Masking + testing helper #8657

Merged
merged 6 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .changeset/funny-keys-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/client-preset': minor
---

Export a testing helper: `makeFragmentData(data, fragment)`
9 changes: 8 additions & 1 deletion dev-test/gql-tag-operations/graphql/fragment-masking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
import { TypedDocumentNode as DocumentNode, ResultOf } from '@graphql-typed-document-node/core';

export type FragmentType<TDocumentType extends DocumentNode<any, any>> = TDocumentType extends DocumentNode<
infer TType,
Expand Down Expand Up @@ -41,3 +41,10 @@ export function useFragment<TType>(
): TType | ReadonlyArray<TType> | null | undefined {
return fragmentType as any;
}

export function makeFragmentData<F extends DocumentNode, FT extends ResultOf<F>>(
data: FT,
_fragment: F
): FragmentType<F> {
return data as FragmentType<F>;
}
22 changes: 20 additions & 2 deletions packages/presets/client/src/fragment-masking-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export type FragmentType<TDocumentType extends DocumentNode<any, any>> = TDocume
: never
: never;`;

const makeFragmentDataHelper = `
export function makeFragmentData<
F extends DocumentNode,
FT extends ResultOf<F>
>(data: FT, _fragment: F): FragmentType<F> {
return data as FragmentType<F>;
}`;

const defaultUnmaskFunctionName = 'useFragment';

const modifyType = (rawType: string, opts: { nullable: boolean; list: 'with-list' | 'only-list' | false }) =>
Expand Down Expand Up @@ -67,10 +75,18 @@ export const plugin: PluginFunction<{
}> = (_, __, { useTypeImports, augmentedModuleName, unmaskFunctionName }, _info) => {
const documentNodeImport = `${
useTypeImports ? 'import type' : 'import'
} { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';\n`;
} { TypedDocumentNode as DocumentNode, ResultOf } from '@graphql-typed-document-node/core';\n`;

if (augmentedModuleName == null) {
return [documentNodeImport, `\n`, fragmentTypeHelper, `\n`, createUnmaskFunction(unmaskFunctionName)].join(``);
return [
documentNodeImport,
`\n`,
fragmentTypeHelper,
`\n`,
createUnmaskFunction(unmaskFunctionName),
`\n`,
makeFragmentDataHelper,
].join(``);
}

return [
Expand All @@ -80,6 +96,8 @@ export const plugin: PluginFunction<{
...fragmentTypeHelper.split(`\n`),
`\n`,
...createUnmaskFunctionTypeDefinitions(unmaskFunctionName).join('\n').split('\n'),
`\n`,
makeFragmentDataHelper,
]
.map(line => (line === `\n` || line === '' ? line : ` ${line}`))
.join(`\n`),
Expand Down
11 changes: 9 additions & 2 deletions packages/presets/client/tests/client-preset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ export * from "./fragment-masking"`);
expect(result).toHaveLength(4);
const gqlFile = result.find(file => file.filename === 'out1/fragment-masking.ts');
expect(gqlFile.content).toMatchInlineSnapshot(`
"import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
"import { TypedDocumentNode as DocumentNode, ResultOf } from '@graphql-typed-document-node/core';


export type FragmentType<TDocumentType extends DocumentNode<any, any>> = TDocumentType extends DocumentNode<
Expand Down Expand Up @@ -673,7 +673,14 @@ export * from "./fragment-masking"`);
): TType | ReadonlyArray<TType> | null | undefined {
return fragmentType as any
}
"


export function makeFragmentData<
F extends DocumentNode,
FT extends ResultOf<F>
>(data: FT, _fragment: F): FragmentType<F> {
return data as FragmentType<F>;
}"
`);

expect(gqlFile.content).toBeSimilarStringTo(`
Expand Down
8 changes: 7 additions & 1 deletion website/src/category-to-packages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ export const CategoryToPackages = {
'fragment-matcher',
'add',
],
presets: ['near-operation-file-preset', 'import-types-preset', 'graphql-modules-preset', 'gql-tag-operations-preset'],
presets: [
'near-operation-file-preset',
'import-types-preset',
'graphql-modules-preset',
'gql-tag-operations-preset',
'preset-client',
],
typescript: [
'named-operations-object',
'relay-operation-optimizer',
Expand Down
6 changes: 6 additions & 0 deletions website/src/lib/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ export const PACKAGES: Record<
icon: 'codegen',
tags: ['preset', 'utilities'],
},
'preset-client': {
title: 'Client preset',
npmPackage: '@graphql-codegen/client-preset',
icon: 'codegen',
tags: ['preset', 'next', 'react', 'urql', 'typescript', 'vue'],
},
introspection: {
title: 'Introspection',
npmPackage: '@graphql-codegen/introspection',
Expand Down
4 changes: 2 additions & 2 deletions website/src/pages/docs/guides/react-vue.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,8 @@ GraphQL Code Generator `client` preset (`@graphql-codegen/client-preset`) is com
- `@urql/preact` (since `1.4.0`)
- `urql` (since `1.11.0`)
- `graphql-request` (since `5.0.0`)
- `react-query` (with `graphql-request@5.0.0`)
- `swr` (with `graphql-request@5.0.0`)
- `react-query` (with `graphql-request@5.x`)
- `swr` (with `graphql-request@5.x`)

- **Vue**
- `@vue/apollo-composable` (since `4.0.0-alpha.13`)
Expand Down
3 changes: 2 additions & 1 deletion website/src/pages/plugins/presets/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"gql-tag-operations-preset": "gql-tag-operations",
"graphql-modules-preset": "graphql-modules",
"import-types-preset": "import-types",
"near-operation-file-preset": "near-operation-file"
"near-operation-file-preset": "near-operation-file",
"preset-client": "client-preset"
}
Loading