Skip to content

Latest commit

 

History

History
676 lines (493 loc) · 35.1 KB

File metadata and controls

676 lines (493 loc) · 35.1 KB

@graphql-codegen/client-preset

4.1.0

Minor Changes

  • #9562 5beee9794 Thanks @n1ru4l! - Add the addTypenameSelectionDocumentTransform for automatically adding __typename selections to all objct type selection sets.

    This is useful for GraphQL Clients such as Apollo Client or urql that need typename information for their cache to function.

    Example Usage

    import { addTypenameSelectionDocumentTransform } from '@graphql-codegen/client-preset';
    import { CodegenConfig } from "@graphql-codegen/cli";
    
    const config: CodegenConfig = {
      schema: "YOUR_GRAPHQL_ENDPOINT",
      documents: ["./**/*.{ts,tsx}"],
      ignoreNoDocuments: true,
      generates: {
        "./gql/": {
          preset: "client",
          plugins: [],
          presetConfig: {
            persistedDocuments: true,
          },
          documentTransforms: [addTypenameSelectionDocumentTransform],
        },
      },
    };
    
    export default config;
    

Patch Changes

  • Updated dependencies [bb1e0e96e]:
    • @graphql-codegen/plugin-helpers@5.0.1

4.0.1

Patch Changes

  • #9497 2276708d0 Thanks @eddeee888! - Revert default ID scalar input type to string

    We changed the ID Scalar input type from string to string | number in the latest major version of typescript plugin. This causes issues for server plugins (e.g. typescript-resolvers) that depends on typescript plugin. This is because the scalar type needs to be manually inverted on setup which is confusing.

  • Updated dependencies [2276708d0]:

    • @graphql-codegen/visitor-plugin-common@4.0.1
    • @graphql-codegen/typescript-operations@4.0.1
    • @graphql-codegen/typescript@4.0.1
    • @graphql-codegen/gql-tag-operations@4.0.1
    • @graphql-codegen/typed-document-node@5.0.1

4.0.0

Major Changes

  • bb66c2a31 Thanks @n1ru4l! - Require Node.js >= 16. Drop support for Node.js 14

Minor Changes

  • #9196 3848a2b73 Thanks @beerose! - Add @defer directive support

    When a query includes a deferred fragment field, the server will return a partial response with the non-deferred fields first, followed by the remaining fields once they have been resolved.

    Once start using the @defer directive in your queries, the generated code will automatically include support for the directive.

    // src/index.tsx
    import { graphql } from './gql';
    const OrdersFragment = graphql(`
      fragment OrdersFragment on User {
        orders {
          id
          total
        }
      }
    `);
    const GetUserQuery = graphql(`
      query GetUser($id: ID!) {
        user(id: $id) {
          id
          name
          ...OrdersFragment @defer
        }
      }
    `);

    The generated type for GetUserQuery will have information that the fragment is incremental, meaning it may not be available right away.

    // gql/graphql.ts
    export type GetUserQuery = { __typename?: 'Query'; id: string; name: string } & ({
      __typename?: 'Query';
    } & {
      ' $fragmentRefs'?: { OrdersFragment: Incremental<OrdersFragment> };
    });

    Apart from generating code that includes support for the @defer directive, the Codegen also exports a utility function called isFragmentReady. You can use it to conditionally render components based on whether the data for a deferred fragment is available:

    const OrdersList = (props: { data: FragmentType<typeof OrdersFragment> }) => {
      const data = useFragment(OrdersFragment, props.data);
      return (
        // render orders list
      )
    };
    
    function App() {
      const { data } = useQuery(GetUserQuery);
      return (
        {data && (
          <>
            {isFragmentReady(GetUserQuery, OrdersFragment, data)
    					&& <OrdersList data={data} />}
          </>
        )}
      );
    }
    export default App;
  • #9353 d7e335b58 Thanks @charpeni! - Implement the ability the specify the hash algorithm used for persisted documents via persistedDocuments.hashAlgorithm

Patch Changes

3.0.1

Patch Changes

  • Updated dependencies [386cf9044, 402cb8ac0]:
    • @graphql-codegen/visitor-plugin-common@3.1.1
    • @graphql-codegen/gql-tag-operations@3.0.1
    • @graphql-codegen/typescript-operations@3.0.4
    • @graphql-codegen/typed-document-node@4.0.1
    • @graphql-codegen/typescript@3.0.4

3.0.0

Major Changes

  • #9137 2256c8b5d Thanks @beerose! - Add TypedDocumentNode string alternative that doesn't require GraphQL AST on the client. This change requires @graphql-typed-document-node/core in version 3.2.0 or higher.

Patch Changes

2.1.1

Patch Changes

2.1.0

Minor Changes

  • #8893 a118c307a Thanks @n1ru4l! - It is no longer mandatory to declare an empty plugins array when using a preset

  • #8723 a3309e63e Thanks @kazekyo! - Introduce a new feature called DocumentTransform.

    DocumentTransform is a functionality that allows you to modify documents before they are processed by plugins. You can use functions passed to the documentTransforms option to make changes to GraphQL documents.

    To use this feature, you can write documentTransforms as follows:

    import type { CodegenConfig } from '@graphql-codegen/cli';
    
    const config: CodegenConfig = {
      schema: 'https://localhost:4000/graphql',
      documents: ['src/**/*.tsx'],
      generates: {
        './src/gql/': {
          preset: 'client',
          documentTransforms: [
            {
              transform: ({ documents }) => {
                // Make some changes to the documents
                return documents;
              },
            },
          ],
        },
      },
    };
    export default config;

    For instance, to remove a @localOnlyDirective directive from documents, you can write the following code:

    import type { CodegenConfig } from '@graphql-codegen/cli';
    import { visit } from 'graphql';
    
    const config: CodegenConfig = {
      schema: 'https://localhost:4000/graphql',
      documents: ['src/**/*.tsx'],
      generates: {
        './src/gql/': {
          preset: 'client',
          documentTransforms: [
            {
              transform: ({ documents }) => {
                return documents.map(documentFile => {
                  documentFile.document = visit(documentFile.document, {
                    Directive: {
                      leave(node) {
                        if (node.name.value === 'localOnlyDirective') return null;
                      },
                    },
                  });
                  return documentFile;
                });
              },
            },
          ],
        },
      },
    };
    export default config;

    DocumentTransform can also be specified by file name. You can create a custom file for a specific transformation and pass it to documentTransforms.

    Let's create the document transform as a file:

    module.exports = {
      transform: ({ documents }) => {
        // Make some changes to the documents
        return documents;
      },
    };

    Then, you can specify the file name as follows:

    import type { CodegenConfig } from '@graphql-codegen/cli';
    
    const config: CodegenConfig = {
      schema: 'https://localhost:4000/graphql',
      documents: ['src/**/*.tsx'],
      generates: {
        './src/gql/': {
          preset: 'client',
          documentTransforms: ['./my-document-transform.js'],
        },
      },
    };
    export default config;

Patch Changes

2.0.0

Major Changes

Patch Changes

1.3.0

Minor Changes

  • #8757 4f290aa72 Thanks @n1ru4l! - Add support for persisted documents.

    You can now generate and embed a persisted documents hash for the executable documents.

    /** codegen.ts */
    import { CodegenConfig } from '@graphql-codegen/cli';
    
    const config: CodegenConfig = {
      schema: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
      documents: ['src/**/*.tsx'],
      ignoreNoDocuments: true, // for better experience with the watcher
      generates: {
        './src/gql/': {
          preset: 'client',
          plugins: [],
          presetConfig: {
            persistedDocuments: true,
          },
        },
      },
    };
    
    export default config;

    This will generate ./src/gql/persisted-documents.json (dictionary of hashes with their operation string).

    In addition to that each generated document node will have a __meta__.hash property.

    import { gql } from './gql.js';
    
    const allFilmsWithVariablesQueryDocument = graphql(/* GraphQL */ `
      query allFilmsWithVariablesQuery($first: Int!) {
        allFilms(first: $first) {
          edges {
            node {
              ...FilmItem
            }
          }
        }
      }
    `);
    
    console.log((allFilmsWithVariablesQueryDocument as any)['__meta__']['hash']);
  • #8757 4f290aa72 Thanks @n1ru4l! - Add support for embedding metadata in the document AST.

    It is now possible to embed metadata (e.g. for your GraphQL client within the emitted code).

    /** codegen.ts */
    import { CodegenConfig } from '@graphql-codegen/cli';
    
    const config: CodegenConfig = {
      schema: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
      documents: ['src/**/*.tsx'],
      ignoreNoDocuments: true, // for better experience with the watcher
      generates: {
        './src/gql/': {
          preset: 'client',
          plugins: [],
          presetConfig: {
            onExecutableDocumentNode(documentNode) {
              return {
                operation: documentNode.definitions[0].operation,
                name: documentNode.definitions[0].name.value,
              };
            },
          },
        },
      },
    };
    
    export default config;

    You can then access the metadata via the __meta__ property on the document node.

    import { gql } from './gql.js';
    
    const allFilmsWithVariablesQueryDocument = graphql(/* GraphQL */ `
      query allFilmsWithVariablesQuery($first: Int!) {
        allFilms(first: $first) {
          edges {
            node {
              ...FilmItem
            }
          }
        }
      }
    `);
    
    console.log((allFilmsWithVariablesQueryDocument as any)['__meta__']);

Patch Changes

  • #8757 4f290aa72 Thanks @n1ru4l! - dependencies updates:
  • Updated dependencies [a98198524]:
    • @graphql-codegen/visitor-plugin-common@2.13.8
    • @graphql-codegen/gql-tag-operations@1.6.2
    • @graphql-codegen/typescript-operations@2.5.13
    • @graphql-codegen/typed-document-node@2.3.13
    • @graphql-codegen/typescript@2.8.8

1.2.6

Patch Changes

  • #8796 902451601 Thanks @shmax! - remove extra asterisk and add missing semicolon in generated output

  • Updated dependencies [902451601]:

    • @graphql-codegen/gql-tag-operations@1.6.1

1.2.5

Patch Changes

  • Updated dependencies [eb454d06c, 2a33fc774]:
    • @graphql-codegen/visitor-plugin-common@2.13.7
    • @graphql-codegen/gql-tag-operations@1.6.0
    • @graphql-codegen/typescript-operations@2.5.12
    • @graphql-codegen/typed-document-node@2.3.12
    • @graphql-codegen/typescript@2.8.7

1.2.4

Patch Changes

1.2.3

Patch Changes

  • 46f75304a Thanks @saihaj! - fix the version of @graphql-codegen/plugin-helpers@3.1.1

  • Updated dependencies [307a5d350, 46f75304a]:

    • @graphql-codegen/plugin-helpers@3.1.1
    • @graphql-codegen/add@3.2.3
    • @graphql-codegen/visitor-plugin-common@2.13.5
    • @graphql-codegen/gql-tag-operations@1.5.11
    • @graphql-codegen/typescript-operations@2.5.10
    • @graphql-codegen/typed-document-node@2.3.10
    • @graphql-codegen/typescript@2.8.5

1.2.2

Patch Changes

  • #8702 0eb0dde8a Thanks @ithinkdancan! - add config for nonOptionalTypename

  • Updated dependencies [a6c2097f4, a6c2097f4, a6c2097f4, f79a00e8a, c802a0c0b]:

    • @graphql-codegen/plugin-helpers@3.0.0
    • @graphql-codegen/typed-document-node@2.3.9
    • @graphql-codegen/visitor-plugin-common@2.13.4
    • @graphql-codegen/add@3.2.2
    • @graphql-codegen/gql-tag-operations@1.5.10
    • @graphql-codegen/typescript-operations@2.5.9
    • @graphql-codegen/typescript@2.8.4

1.2.1

Patch Changes

  • Updated dependencies [62f655452]:
    • @graphql-codegen/visitor-plugin-common@2.13.3
    • @graphql-codegen/typescript-operations@2.5.8
    • @graphql-codegen/gql-tag-operations@1.5.9
    • @graphql-codegen/typed-document-node@2.3.8
    • @graphql-codegen/typescript@2.8.3

1.2.0

Minor Changes

1.1.5

Patch Changes

  • Updated dependencies [00ddc9368]:
    • @graphql-codegen/gql-tag-operations@1.5.8

1.1.4

Patch Changes

  • Updated dependencies [ef4c2c9c2]:
    • @graphql-codegen/visitor-plugin-common@2.13.2
    • @graphql-codegen/typescript@2.8.2
    • @graphql-codegen/gql-tag-operations@1.5.7
    • @graphql-codegen/typescript-operations@2.5.7
    • @graphql-codegen/typed-document-node@2.3.7

1.1.3

Patch Changes

  • Updated dependencies [63dc8f205]:
    • @graphql-codegen/visitor-plugin-common@2.13.1
    • @graphql-codegen/plugin-helpers@2.7.2
    • @graphql-codegen/gql-tag-operations@1.5.6
    • @graphql-codegen/typescript-operations@2.5.6
    • @graphql-codegen/typed-document-node@2.3.6
    • @graphql-codegen/typescript@2.8.1

1.1.2

Patch Changes

1.1.1

Patch Changes

1.1.0

Minor Changes

Patch Changes

  • #8500 71aae7a92 Thanks @charlypoly! - Add warning and errors to prevent unwanted configuration

  • Updated dependencies [a46b8d99c]:

    • @graphql-codegen/visitor-plugin-common@2.13.0
    • @graphql-codegen/gql-tag-operations@1.5.5
    • @graphql-codegen/typescript-operations@2.5.5
    • @graphql-codegen/typed-document-node@2.3.5
    • @graphql-codegen/typescript@2.7.5

1.0.7

Patch Changes

1.0.6

Patch Changes

  • Updated dependencies [1bd7f771c]:
    • @graphql-codegen/visitor-plugin-common@2.12.2
    • @graphql-codegen/gql-tag-operations@1.5.4
    • @graphql-codegen/typescript-operations@2.5.4
    • @graphql-codegen/typed-document-node@2.3.4
    • @graphql-codegen/typescript@2.7.4

1.0.5

Patch Changes

1.0.4

Patch Changes

  • #8455 d19573d88 Thanks @charlypoly! - The client preset now allows the use of the following config:
    • scalars
    • defaultScalarType
    • strictScalars
    • namingConvention
    • useTypeImports
    • skipTypename
    • arrayInputCoercion

1.0.3

Patch Changes

  • #8443 e2d115146 Thanks @charlypoly! - fix(gql-tag-operations): issues with "no documents" scenario

  • Updated dependencies [e2d115146]:

    • @graphql-codegen/gql-tag-operations@1.5.3

1.0.2

Patch Changes

1.0.1

Patch Changes

  • #8302 876844e76 Thanks @charlypoly! - @graphql-codegen/gql-tag-operations and @graphql-codegen/gql-tag-operations-preset

    Introduce a gqlTagName configuration option


    @graphql-codegen/client-preset

    New preset for GraphQL Code Generator v3, more information on the RFC: #8296


    @graphql-codegen/cli

    Update init wizard with 3.0 recommendations (codegen.ts, client preset)

  • Updated dependencies [876844e76]:

    • @graphql-codegen/gql-tag-operations@1.5.0