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

Example: feat: document node #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tn-m
Copy link

@tn-m tn-m commented Jul 26, 2023

NOTE: This PR shows the document node feature, how it looks like. It's ok to merge if you want, but not expected as of now.

Added some plugins

  • typescript-operations
    • This plugin generates TypeScript types based on your GraphQLSchema and your GraphQL operations and fragments. It generates types for your GraphQL documents: Query, Mutation, Subscription and Fragment.

  • typed-document-node
    • With the typed-document-node plugin you no longer need to reconfigure, and install different GraphQL Code Generator plugins to manage all your GraphQL client hooks. Generate a single TypedDocumentNode and pass it to your GraphQL client of choice.

Preset has document node, however does not load .gql file, just .ts / .tsx colocation is supported.
It's up to you which you prefer to use

dotansimha/graphql-code-generator#8296 (comment)

Comment on lines -5 to +6
documents: ["src/**/*.tsx"],
ignoreNoDocuments: true, // for better experience with the watcher
documents: ["src/**/*.gql"],
// ignoreNoDocuments: true, // for better experience with the watcher
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to split gql into another file, but it's up to you

Comment on lines -8 to +10
"./src/gql/": {
preset: "client",
"./src/gql/generated.ts": {
plugins: ["typescript", "typescript-operations", "typed-document-node"],
// preset: "client",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading some plugins

Comment on lines -6 to +8
import { graphql } from "../src/gql";

const allFilmsWithVariablesQueryDocument = graphql(/* GraphQL */ `
query allFilmsWithVariablesQuery($first: Int!) {
allFilms(first: $first) {
edges {
node {
...FilmItem
}
}
}
}
`);
import {
AllFilmsWithVariablesQueryDocument,
AllFilmsWithVariablesQueryQueryVariables,
} from "./gql/generated";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of writing query in tsx, you can import the types like this

Comment on lines +1 to -17
import { Film as GqlFilm } from "./gql/generated";

const Film = (props: {
/* `film` property has the correct type 🎉 */
film: FragmentType<typeof FilmFragment>;
film: GqlFilm;
}) => {
const film = useFragment(FilmFragment, props.film);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of useFragment, there are a type Film which is auto generated.
In this case conflicting to the component name though

Comment on lines +1 to +9
query allFilmsWithVariablesQuery($first: Int!) {
allFilms(first: $first) {
edges {
node {
...FilmItem
}
}
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put out the gql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant