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
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ 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
documents: ["src/**/*.gql"],
// ignoreNoDocuments: true, // for better experience with the watcher
Comment on lines -5 to +6
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

generates: {
"./src/gql/": {
preset: "client",
"./src/gql/generated.ts": {
plugins: ["typescript", "typescript-operations", "typed-document-node"],
// preset: "client",
Comment on lines -8 to +10
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

},
},
};
Expand Down
3 changes: 3 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"devDependencies": {
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/client-preset": "^4.1.0",
"@graphql-codegen/typed-document-node": "^5.0.1",
"@graphql-codegen/typescript": "^4.0.1",
"@graphql-codegen/typescript-operations": "^4.0.1",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.0.0",
Expand Down
25 changes: 8 additions & 17 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import React from "react";
import { useQuery } from "@apollo/client";

import "./App.css";
import Film from "./Film";
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";
Comment on lines -6 to +8
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


function App() {
// `data` is typed!
const { data } = useQuery(allFilmsWithVariablesQueryDocument, {
variables: { first: 10 },
// `data` and `variables` are typed!
const variables: AllFilmsWithVariablesQueryQueryVariables = { first: 10 };
const { data } = useQuery(AllFilmsWithVariablesQueryDocument, {
variables: variables,
});
return (
<div className="App">
Expand Down
19 changes: 4 additions & 15 deletions src/Film.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import { FragmentType, useFragment } from "./gql/fragment-masking";
import { graphql } from "../src/gql";

export const FilmFragment = graphql(/* GraphQL */ `
fragment FilmItem on Film {
id
title
releaseDate
producers
}
`);
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);
Comment on lines +1 to -17
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

return (
<div>
<h3>{film.title}</h3>
<p>{film.releaseDate}</p>
<h3>{props.film.title}</h3>
<p>{props.film.releaseDate}</p>
</div>
);
};
Expand Down
9 changes: 9 additions & 0 deletions src/gql/allFilmsWithVariablesQuery.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
query allFilmsWithVariablesQuery($first: Int!) {
allFilms(first: $first) {
edges {
node {
...FilmItem
}
}
}
}
Comment on lines +1 to +9
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

6 changes: 6 additions & 0 deletions src/gql/filmFragment.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fragment FilmItem on Film {
id
title
releaseDate
producers
}
66 changes: 0 additions & 66 deletions src/gql/fragment-masking.ts

This file was deleted.

8 changes: 2 additions & 6 deletions src/gql/graphql.ts → src/gql/generated.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable */
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
Expand Down Expand Up @@ -1309,12 +1308,9 @@ export type AllFilmsWithVariablesQueryQueryVariables = Exact<{
}>;


export type AllFilmsWithVariablesQueryQuery = { __typename?: 'Root', allFilms?: { __typename?: 'FilmsConnection', edges?: Array<{ __typename?: 'FilmsEdge', node?: (
{ __typename?: 'Film' }
& { ' $fragmentRefs'?: { 'FilmItemFragment': FilmItemFragment } }
) | null } | null> | null } | null };
export type AllFilmsWithVariablesQueryQuery = { __typename?: 'Root', allFilms?: { __typename?: 'FilmsConnection', edges?: Array<{ __typename?: 'FilmsEdge', node?: { __typename?: 'Film', id: string, title?: string | null, releaseDate?: string | null, producers?: Array<string | null> | null } | null } | null> | null } | null };

export type FilmItemFragment = { __typename?: 'Film', id: string, title?: string | null, releaseDate?: string | null, producers?: Array<string | null> | null } & { ' $fragmentName'?: 'FilmItemFragment' };
export type FilmItemFragment = { __typename?: 'Film', id: string, title?: string | null, releaseDate?: string | null, producers?: Array<string | null> | null };

export const FilmItemFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"FilmItem"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Film"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"releaseDate"}},{"kind":"Field","name":{"kind":"Name","value":"producers"}}]}}]} as unknown as DocumentNode<FilmItemFragment, unknown>;
export const AllFilmsWithVariablesQueryDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"allFilmsWithVariablesQuery"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"first"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Int"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"allFilms"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"first"},"value":{"kind":"Variable","name":{"kind":"Name","value":"first"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"edges"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"node"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"FilmItem"}}]}}]}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"FilmItem"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Film"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"releaseDate"}},{"kind":"Field","name":{"kind":"Name","value":"producers"}}]}}]} as unknown as DocumentNode<AllFilmsWithVariablesQueryQuery, AllFilmsWithVariablesQueryQueryVariables>;
47 changes: 0 additions & 47 deletions src/gql/gql.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/gql/index.ts

This file was deleted.