Skip to content

Commit

Permalink
Merge branch 'master' into client-preset-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
beerose authored Oct 23, 2023
2 parents f2a3ec4 + 80e4a5b commit d29094c
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-forks-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': patch
---

Avoid reading from null values when selection sets only contain fragments.
11 changes: 11 additions & 0 deletions dev-test/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ const config: CodegenConfig = {
preset: 'client',
presetConfig: { fragmentMasking: true },
},
'./dev-test/test-null-value/result.d.ts': {
schema: './dev-test/test-null-value/schema.graphql',
documents: ['./dev-test/test-null-value/query.ts'],
plugins: ['typescript', 'typescript-operations'],
config: {
// The combination of these two flags caused the following issue:
// https://github.com/dotansimha/graphql-code-generator/pull/9709
skipTypename: true,
mergeFragmentTypes: true,
},
},
},
};

Expand Down
16 changes: 16 additions & 0 deletions dev-test/test-null-value/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const MY_QUERY = /* GraphQL */ `
fragment CartLine on CartLine {
id
quantity
}
query Test {
cart {
lines {
nodes {
...CartLine
}
}
}
}
`;
50 changes: 50 additions & 0 deletions dev-test/test-null-value/result.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
export type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: { input: string; output: string };
String: { input: string; output: string };
Boolean: { input: boolean; output: boolean };
Int: { input: number; output: number };
Float: { input: number; output: number };
};

export type BaseCartLine = {
id: Scalars['String']['output'];
quantity: Scalars['Int']['output'];
};

export type BaseCartLineConnection = {
id: Scalars['String']['output'];
nodes: Array<BaseCartLine>;
};

export type Cart = {
id: Scalars['String']['output'];
lines: BaseCartLineConnection;
};

export type CartLine = BaseCartLine & {
id: Scalars['String']['output'];
quantity: Scalars['Int']['output'];
};

export type ComponentizableCartLine = BaseCartLine & {
id: Scalars['String']['output'];
quantity: Scalars['Int']['output'];
};

export type QueryRoot = {
cart?: Maybe<Cart>;
};

export type CartLineFragment = { id: string; quantity: number };

export type TestQueryVariables = Exact<{ [key: string]: never }>;

export type TestQuery = { cart?: { lines: { nodes: Array<{ id: string; quantity: number }> } } | null };
32 changes: 32 additions & 0 deletions dev-test/test-null-value/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
schema {
query: QueryRoot
}

interface BaseCartLine {
id: String!
quantity: Int!
}

type BaseCartLineConnection {
id: String!
nodes: [BaseCartLine!]!
}

type Cart {
id: String!
lines: BaseCartLineConnection!
}

type CartLine implements BaseCartLine {
id: String!
quantity: Int!
}

type ComponentizableCartLine implements BaseCartLine {
id: String!
quantity: Int!
}

type QueryRoot {
cart: Cart
}
4 changes: 2 additions & 2 deletions packages/graphql-codegen-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
"devDependencies": {
"@graphql-tools/merge": "9.0.0",
"@parcel/watcher": "^2.1.0",
"@types/debounce": "1.2.1",
"@types/inquirer": "8.2.6",
"@types/debounce": "1.2.3",
"@types/inquirer": "8.2.9",
"@types/is-glob": "4.0.2",
"@types/js-yaml": "4.0.5",
"@types/micromatch": "^4.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,6 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
return (
Object.keys(grouped)
.map(typeName => {
const hasUnions = grouped[typeName].filter(s => typeof s !== 'string' && s.union).length > 0;
const relevant = grouped[typeName].filter(Boolean);

if (relevant.length === 0) {
Expand All @@ -729,6 +728,8 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
})
.join(' & ');

const hasUnions = grouped[typeName].filter(s => typeof s !== 'string' && s.union).length > 0;

return relevant.length > 1 && !hasUnions ? `( ${res} )` : res;
})
.filter(Boolean)
Expand Down
4 changes: 2 additions & 2 deletions packages/presets/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"test": "jest --no-watchman --config ../../../jest.config.js"
},
"devDependencies": {
"@types/babel__helper-plugin-utils": "7.10.0",
"@types/babel__template": "7.4.1"
"@types/babel__helper-plugin-utils": "7.10.2",
"@types/babel__template": "7.4.3"
},
"dependencies": {
"@babel/helper-plugin-utils": "^7.20.2",
Expand Down
7 changes: 4 additions & 3 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"devDependencies": {
"@theguild/algolia": "1.1.9",
"@theguild/tailwind-config": "0.3.0",
"@types/dedent": "0.7.0",
"@types/dedent": "0.7.1",
"@types/jsonpath": "0.2.0",
"@types/node": "18.17.11",
"@types/react": "18.2.21",
Expand All @@ -25,6 +25,7 @@
"@graphql-codegen/c-sharp": "4.3.1",
"@graphql-codegen/c-sharp-operations": "2.3.1",
"@graphql-codegen/cli": "5.0.0",
"@graphql-codegen/client-preset": "4.1.0",
"@graphql-codegen/core": "4.0.0",
"@graphql-codegen/flow": "2.3.6",
"@graphql-codegen/flow-operations": "2.3.6",
Expand Down Expand Up @@ -52,7 +53,7 @@
"@graphql-codegen/typescript-graphql-request": "5.0.0",
"@graphql-codegen/typescript-mongodb": "2.4.6",
"@graphql-codegen/typescript-msw": "1.1.6",
"@graphql-codegen/typescript-nhost": "0.0.1",
"@graphql-codegen/typescript-nhost": "0.0.2",
"@graphql-codegen/typescript-operations": "4.0.1",
"@graphql-codegen/typescript-react-apollo": "3.3.7",
"@graphql-codegen/typescript-react-query": "4.1.0",
Expand All @@ -65,8 +66,8 @@
"@graphql-codegen/typescript-vue-apollo-smart-ops": "2.3.6",
"@graphql-codegen/typescript-vue-urql": "2.3.6",
"@graphql-codegen/urql-introspection": "2.2.1",
"@graphql-codegen/client-preset": "4.1.0",
"@mendable/search": "0.0.155",
"@mizdra/graphql-codegen-typescript-fabbrica": "^0.3.0",
"@monaco-editor/react": "4.5.2",
"@theguild/components": "^6.0.1",
"classnames": "2.3.2",
Expand Down
1 change: 1 addition & 0 deletions website/src/category-to-packages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const CategoryToPackages = {
'typescript-apollo-client-helpers',
'typescript-apollo-next',
'typescript-document-nodes',
'typescript-fabbrica',
'typescript-generic-sdk',
'typescript-graphql-files-modules',
'typescript-graphql-request',
Expand Down
6 changes: 6 additions & 0 deletions website/src/lib/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ export const PACKAGES: Record<
icon: 'typescript',
tags: ['plugin', 'typescript'],
},
'typescript-fabbrica': {
title: 'TypeScript Mock Data Factory',
npmPackage: '@mizdra/graphql-codegen-typescript-fabbrica',
icon: 'typescript',
tags: ['plugin', 'typescript'],
},
'typescript-generic-sdk': {
title: 'TypeScript Generic SDK',
npmPackage: '@graphql-codegen/typescript-generic-sdk',
Expand Down
1 change: 1 addition & 0 deletions website/src/pages/plugins/typescript/_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default {
'typescript-apollo-client-helpers': 'apollo-client-helpers',
'typescript-apollo-next': 'apollo-next',
'typescript-document-nodes': 'document-nodes',
'typescript-fabbrica': 'fabbrica',
'typescript-generic-sdk': 'generic-sdk',
'typescript-graphql-files-modules': 'graphql-files-modules',
'typescript-graphql-request': 'graphql-request',
Expand Down
11 changes: 11 additions & 0 deletions website/src/pages/plugins/typescript/typescript-fabbrica.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: typescript-fabbrica
description: GraphQL Code Generator Plugin to define mock data factory.
---

import { PluginApiDocs, PluginHeader } from '@/components/plugin'
import { pluginGetStaticProps } from '@/lib/plugin-get-static-props'
export const getStaticProps = pluginGetStaticProps(__filename)

<PluginHeader />
<PluginApiDocs />
65 changes: 39 additions & 26 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2258,16 +2258,16 @@
change-case-all "1.0.14"
tslib "~2.4.0"

"@graphql-codegen/[email protected].1":
version "0.0.1"
resolved "https://registry.yarnpkg.com/@graphql-codegen/typescript-nhost/-/typescript-nhost-0.0.1.tgz#81a222ed9b03b6265c94b23f11e2cfd3ac567544"
integrity sha512-ISVfpk7PYqWEhcA5Ypky7X9Fyosb9etxyGkT85rW4AK9VrshwN32osbA1Zkmsx3WhoZxIBpGzUk1Vf4YNYSurg==
"@graphql-codegen/[email protected].2":
version "0.0.2"
resolved "https://registry.yarnpkg.com/@graphql-codegen/typescript-nhost/-/typescript-nhost-0.0.2.tgz#e29bd26073c2bac548d67b42899f6992d7a53646"
integrity sha512-DhBqEyTZti3MARYXKezgX2TgG0aTM5AgEuGm96sWHR7OxkrXx73VX+lsC2xARI/vue7liqxjJLDOxBjV5CpqjQ==
dependencies:
"@graphql-codegen/plugin-helpers" "^4.0.0"
"@graphql-codegen/typescript" "^3.0.0"
"@graphql-codegen/visitor-plugin-common" "3.0.0"
"@urql/introspection" "^1.0.0"
tslib "~2.5.0"
tslib "~2.6.0"

"@graphql-codegen/[email protected]":
version "3.3.7"
Expand Down Expand Up @@ -3389,6 +3389,14 @@
html-react-parser "^4.2.0"
posthog-js "^1.45.1"

"@mizdra/graphql-codegen-typescript-fabbrica@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@mizdra/graphql-codegen-typescript-fabbrica/-/graphql-codegen-typescript-fabbrica-0.3.0.tgz#783ffc7c4ade7b844a6c65cc1fb7bac340a2f42d"
integrity sha512-ShPEpC6GrzL1fqXqx4Ua1heid8ExrK/evJI1vozVPt6mZWebwKG0tf26v9KwQ0F8HZBufnq4BURUMXobWi9ckQ==
dependencies:
"@graphql-codegen/plugin-helpers" "^5.0.1"
"@graphql-codegen/visitor-plugin-common" "^4.0.1"

"@monaco-editor/loader@^1.3.3":
version "1.3.3"
resolved "https://registry.yarnpkg.com/@monaco-editor/loader/-/loader-1.3.3.tgz#7f1742bd3cc21c0362a46a4056317f6e5215cfca"
Expand Down Expand Up @@ -4161,17 +4169,17 @@
dependencies:
"@babel/types" "^7.0.0"

"@types/[email protected].0":
version "7.10.0"
resolved "https://registry.yarnpkg.com/@types/babel__helper-plugin-utils/-/babel__helper-plugin-utils-7.10.0.tgz#dcd2416f9c189d5837ab2a276368cf67134efe78"
integrity sha512-60YtHzhQ9HAkToHVV+TB4VLzBn9lrfgrsOjiJMtbv/c1jPdekBxaByd6DMsGBzROXWoIL6U3lEFvvbu69RkUoA==
"@types/[email protected].2":
version "7.10.2"
resolved "https://registry.yarnpkg.com/@types/babel__helper-plugin-utils/-/babel__helper-plugin-utils-7.10.2.tgz#d733528f1181c606e92f1a38f18e95575518254b"
integrity sha512-Sa17cG0SKMedlH5bEozh0eXo/54iWpSxbxCoqknRJY0oGGTwO9/SCfIx1taDnG0dvkJnYW+/7tv+PTSFaQsRNA==
dependencies:
"@types/babel__core" "*"

"@types/babel__template@*", "@types/[email protected].1":
version "7.4.1"
resolved "https://registry.yarnpkg.com/@types/babel__template/-/babel__template-7.4.1.tgz#3d1a48fd9d6c0edfd56f2ff578daed48f36c8969"
integrity sha512-azBFKemX6kMg5Io+/rdGT0dkGreboUVR0Cdm3fz9QJWpaQGJRQXl7C+6hOTCZcMll7KFyEQpgbYI2lHdsS4U7g==
"@types/babel__template@*", "@types/[email protected].3":
version "7.4.3"
resolved "https://registry.yarnpkg.com/@types/babel__template/-/babel__template-7.4.3.tgz#db9ac539a2fe05cfe9e168b24f360701bde41f5f"
integrity sha512-ciwyCLeuRfxboZ4isgdNZi/tkt06m8Tw6uGbBSBgWrnnZGNXiEyM27xc/PjXGQLqlZ6ylbgHMnm7ccF9tCkOeQ==
dependencies:
"@babel/parser" "^7.1.0"
"@babel/types" "^7.0.0"
Expand Down Expand Up @@ -4212,10 +4220,10 @@
resolved "https://registry.yarnpkg.com/@types/d3-time/-/d3-time-3.0.0.tgz#e1ac0f3e9e195135361fa1a1d62f795d87e6e819"
integrity sha512-sZLCdHvBUcNby1cB6Fd3ZBrABbjz3v1Vm90nysCQ6Vt7vd6e/h9Lt7SiJUoEX0l4Dzc7P5llKyhqSi1ycSf1Hg==

"@types/[email protected].1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@types/debounce/-/debounce-1.2.1.tgz#79b65710bc8b6d44094d286aecf38e44f9627852"
integrity sha512-epMsEE85fi4lfmJUH/89/iV/LI+F5CvNIvmgs5g5jYFPfhO2S/ae8WSsLOKWdwtoaZw9Q2IhJ4tQ5tFCcS/4HA==
"@types/[email protected].3":
version "1.2.3"
resolved "https://registry.yarnpkg.com/@types/debounce/-/debounce-1.2.3.tgz#52c50549708d403684b29e42d14235276517765f"
integrity sha512-97mx7gWt4e+kd0wPa1pNEvE4tYGhgBVa4ExWOLcfFohAjF9wERfJ+3qrn7I1e76oHupOvRs4UyYe9xzy0i4TUw==

"@types/debug@^4.0.0":
version "4.1.7"
Expand All @@ -4224,10 +4232,10 @@
dependencies:
"@types/ms" "*"

"@types/[email protected].0":
version "0.7.0"
resolved "https://registry.yarnpkg.com/@types/dedent/-/dedent-0.7.0.tgz#155f339ca404e6dd90b9ce46a3f78fd69ca9b050"
integrity sha512-EGlKlgMhnLt/cM4DbUSafFdrkeJoC9Mvnj0PUCU7tFmTjMjNRT957kXCx0wYm3JuEq4o4ZsS5vG+NlkM2DMd2A==
"@types/[email protected].1":
version "0.7.1"
resolved "https://registry.yarnpkg.com/@types/dedent/-/dedent-0.7.1.tgz#05a6af9dbe7d3c927f3f6f54082fc69157a31a69"
integrity sha512-NKo033xLkoVCgUobq+FS1S3J6kwWeOcyqEBc2r2oi9J/dKncY11J9VgS0Giv7T4rxVt/MajUVcFppT2TPJA2Cg==

"@types/estree-jsx@^1.0.0":
version "1.0.0"
Expand Down Expand Up @@ -4262,10 +4270,10 @@
dependencies:
"@types/unist" "*"

"@types/[email protected].6":
version "8.2.6"
resolved "https://registry.yarnpkg.com/@types/inquirer/-/inquirer-8.2.6.tgz#abd41a5fb689c7f1acb12933d787d4262a02a0ab"
integrity sha512-3uT88kxg8lNzY8ay2ZjP44DKcRaTGztqeIvN2zHvhzIBH/uAPaL75aBtdNRKbA7xXoMbBt5kX0M00VKAnfOYlA==
"@types/[email protected].9":
version "8.2.9"
resolved "https://registry.yarnpkg.com/@types/inquirer/-/inquirer-8.2.9.tgz#bb29e7d2358e3af7d9f4d6c6410320498b428d48"
integrity sha512-5IEO2PwCy4NZDgj977dho4Qbdiw6dJZJzD4ZaB/9j7dfppf7z0xxFPKZz/FtTAUQbDjmWHJ6Jlz/gn0YzWHPsw==
dependencies:
"@types/through" "*"
rxjs "^7.2.0"
Expand Down Expand Up @@ -8617,11 +8625,16 @@ [email protected]:
lru-cache "^10.0.0"
tslib "^2.5.2"

[email protected], graphql@^16.0.0, graphql@^16.6.0:
[email protected], graphql@^16.6.0:
version "16.8.0"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.8.0.tgz#374478b7f27b2dc6153c8f42c1b80157f79d79d4"
integrity sha512-0oKGaR+y3qcS5mCu1vb7KG+a89vjn06C7Ihq/dDl3jA+A8B3TKomvi3CiEcVLJQGalbu8F52LxkOym7U5sSfbg==

graphql@^16.0.0:
version "16.8.1"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.8.1.tgz#1930a965bef1170603702acdb68aedd3f3cf6f07"
integrity sha512-59LZHPdGZVh695Ud9lRzPBVTtlX9ZCV150Er2W43ro37wVof0ctenSaskPPjN7lVTIN8mSZt8PHUNKZuNQUuxw==

gray-matter@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-4.0.3.tgz#e893c064825de73ea1f5f7d88c7a9f7274288798"
Expand Down

0 comments on commit d29094c

Please sign in to comment.