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

fix: TypedDocument addon import specifier #1236

Merged
merged 2 commits into from Mar 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
},
});