Skip to content

Commit

Permalink
Merge pull request #1236 from Quramy/fix_typed_doc_addon_import
Browse files Browse the repository at this point in the history
fix: TypedDocument addon import specifier
  • Loading branch information
Quramy committed Mar 15, 2024
2 parents 3666033 + 781f185 commit 1424023
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -535,7 +535,7 @@ This Addon requires `graphql` v15.4.0 or later. To enable this feature, configur
}
```
When enabled generated files export a type based on [`TypedQueryDocumentNode`](https://github.com/graphql/graphql-js/blob/master/src/utilities/typedQueryDocumentNode.d.ts) from GraphQL. The type extends the standard `DocumentNode` AST type but also includes types for result data and variables as type arguments.
When enabled generated files export a type based on [`TypedQueryDocumentNode`](https://the-guild.dev/blog/typed-document-node) from GraphQL. The type extends the standard `DocumentNode` AST type but also includes types for result data and variables as type arguments.
To use this feature you can apply a type assertion to `gql` template tag expressions that evaluate to a `DocumentNode` value.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions project-fixtures/react-apollo-prj/src/index.tsx
@@ -1,6 +1,6 @@
import React from 'react';
import { gql, useQuery } from '@apollo/client';
import { GitHubQuery, GitHubQueryVariables } from './__generated__/git-hub-query';
import type { GitHubQueryDocument } from './__generated__/git-hub-query';

const repositoryFragment = gql`
fragment RepositoryFragment on Repository {
Expand Down Expand Up @@ -31,7 +31,7 @@ const mutation = gql`
`;

export default () => {
const { data } = useQuery<GitHubQuery, GitHubQueryVariables>(query, { variables: { first: 100 } });
const { data } = useQuery(query as GitHubQueryDocument, { variables: { first: 100 } });
if (!data) return null;
return (
<ul>
Expand Down
Expand Up @@ -4,12 +4,12 @@ exports[`TypedQueryDocumentAddonFactory should add export statement using TypedQ
[
"/* eslint-disable */
/* This is an autogenerated file. Do not edit this file directly! */
import { TypedQueryDocumentNode } from "graphql";
import { TypedDocumentNode } from "@graphql-typed-document-node/core";
export type MyQuery = {
hello: string;
};
export type MyQueryVariables = {};
export type MyQueryDocument = TypedQueryDocumentNode<MyQuery, MyQueryVariables>;
export type MyQueryDocument = TypedDocumentNode<MyQuery, MyQueryVariables>;
",
]
`;
5 changes: 3 additions & 2 deletions src/typegen-addons/typed-query-document.ts
Expand Up @@ -5,13 +5,14 @@ import { astf } from '../ts-ast-util';
export const TypedQueryDocumentAddonFactory: TypeGenAddonFactory = ({ source }) => ({
operationDefinition({ tsResultNode, tsVariableNode }) {
const lhs = astf.createIdentifier(`${tsResultNode.name.text}Document`);
const rhs = astf.createTypeReferenceNode(astf.createIdentifier('TypedQueryDocumentNode'), [
const rhs = astf.createTypeReferenceNode(astf.createIdentifier('TypedDocumentNode'), [
astf.createTypeReferenceNode(astf.createIdentifier(tsResultNode.name.text)),
astf.createTypeReferenceNode(astf.createIdentifier(tsVariableNode.name.text)),
]);
const modifiers = [astf.createModifier(ts.SyntaxKind.ExportKeyword)];
const typeAliasDeclaration = astf.createTypeAliasDeclaration(modifiers, lhs, undefined, rhs);
source.pushNamedImportIfNeeded('TypedQueryDocumentNode', 'graphql');
// source.pushNamedImportIfNeeded('TypedQueryDocumentNode', 'graphql');
source.pushNamedImportIfNeeded('TypedDocumentNode', '@graphql-typed-document-node/core');
source.pushStatement(typeAliasDeclaration);
},
});

0 comments on commit 1424023

Please sign in to comment.