Skip to content

Commit

Permalink
Merge branch 'master' into docs-enhance-error-message
Browse files Browse the repository at this point in the history
  • Loading branch information
saihaj authored May 12, 2023
2 parents 8f6976f + 5f0eb97 commit 99a5f51
Show file tree
Hide file tree
Showing 213 changed files with 8,132 additions and 2,142 deletions.
7 changes: 7 additions & 0 deletions .changeset/@graphql-codegen_cli-9281-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@graphql-codegen/cli": patch
---
dependencies updates:
- Updated dependency [`@graphql-tools/github-loader@^7.3.28` ↗︎](https://www.npmjs.com/package/@graphql-tools/github-loader/v/7.3.28) (from `^7.3.20`, in `dependencies`)
- Updated dependency [`@graphql-tools/prisma-loader@^7.2.69` ↗︎](https://www.npmjs.com/package/@graphql-tools/prisma-loader/v/7.2.69) (from `^7.2.49`, in `dependencies`)
- Updated dependency [`@graphql-tools/url-loader@^7.17.17` ↗︎](https://www.npmjs.com/package/@graphql-tools/url-loader/v/7.17.17) (from `^7.13.2`, in `dependencies`)
5 changes: 0 additions & 5 deletions .changeset/big-bottles-bow.md

This file was deleted.

10 changes: 10 additions & 0 deletions .changeset/eleven-news-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@graphql-codegen/typed-document-node': minor
'@graphql-codegen/visitor-plugin-common': minor
'@graphql-codegen/typescript-operations': minor
'@graphql-codegen/typescript': minor
'@graphql-codegen/typescript-resolvers': minor
'@graphql-codegen/client-preset': minor
---

Add `@defer` directive support
5 changes: 0 additions & 5 deletions .changeset/fast-candles-sort.md

This file was deleted.

5 changes: 5 additions & 0 deletions .changeset/four-buckets-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/client-preset': patch
---

Improve isFragmentReady utility function to work with noUncheckedIndexedAccess TSC setting
5 changes: 5 additions & 0 deletions .changeset/khaki-dolphins-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/plugin-helpers': patch
---

Add handleAsSDL property to UrlSchemaOptions type
27 changes: 27 additions & 0 deletions .changeset/long-owls-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
'@graphql-codegen/visitor-plugin-common': minor
'@graphql-codegen/typescript-resolvers': minor
---

Add excludeTypes config to resolversNonOptionalTypename

This disables the adding of `__typename` in resolver types for any specified typename. This could be useful e.g. if you're wanting to enable this for all new types going forward but not do a big migration.

Usage example:

```typescript
const config: CodegenConfig = {
schema: 'src/schema/**/*.graphql',
generates: {
'src/schema/types.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
resolversNonOptionalTypename: {
unionMember: true,
excludeTypes: ['MyType'],
}
}
},
},
};
```
8 changes: 8 additions & 0 deletions .changeset/long-pears-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@graphql-codegen/visitor-plugin-common': minor
'@graphql-codegen/typescript-resolvers': minor
---

Use generic to simplify ResolversUnionTypes

This follows the `ResolversInterfaceTypes`'s approach where the `RefType` generic is used to refer back to `ResolversTypes` or `ResolversParentTypes` in cases of nested Union types
5 changes: 5 additions & 0 deletions .changeset/modern-clouds-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/client-preset': patch
---

Pass `emitLegacyCommonJSImports` and `isStringDocumentMode` to the client preset config
5 changes: 5 additions & 0 deletions .changeset/orange-tables-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/client-preset': minor
---

Implement the ability the specify the hash algorithm used for persisted documents via `persistedDocuments.hashAlgorithm`
5 changes: 5 additions & 0 deletions .changeset/quiet-rocks-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/cli': patch
---

Fixed option ignoreNoDocuments when using graphql configs
6 changes: 0 additions & 6 deletions .changeset/strange-years-hope.md

This file was deleted.

5 changes: 5 additions & 0 deletions .changeset/swift-files-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/cli': patch
---

Trigger rebuilds in watch mode while respecting rules of precedence and negation, both in terms of global (top-level) config vs. local (per-output target) config, and in terms of watch patterns (higher priority) vs. documents/schemas (lower priority). This fixes an issue with overly-aggressive rebuilds during watch mode.
5 changes: 5 additions & 0 deletions .changeset/thin-berries-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': patch
---

Output valid type names with mergeFragmentTypes
100 changes: 100 additions & 0 deletions .changeset/tricky-swans-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
'@graphql-codegen/visitor-plugin-common': minor
'@graphql-codegen/typescript-resolvers': minor
---

Extract interfaces to ResolversInterfaceTypes and add to resolversNonOptionalTypename

1. `ResolversInterfaceTypes` is a new type that keeps track of a GraphQL interface and its implementing types.

For example, consider this schema:

```graphql
extend type Query {
character(id: ID!): CharacterNode
}

interface CharacterNode {
id: ID!
}

type Wizard implements CharacterNode {
id: ID!
screenName: String!
spells: [String!]!
}

type Fighter implements CharacterNode {
id: ID!
screenName: String!
powerLevel: Int!
}
```

The generated types will look like this:

```ts
export type ResolversInterfaceTypes<RefType extends Record<string, unknown>> = {
CharacterNode: Fighter | Wizard;
};

export type ResolversTypes = {
// other types...
CharacterNode: ResolverTypeWrapper<ResolversInterfaceTypes<ResolversTypes>["CharacterNode"]>;
Fighter: ResolverTypeWrapper<Fighter>;
Wizard: ResolverTypeWrapper<Wizard>;
// other types...
};

export type ResolversParentTypes = {
// other types...
CharacterNode: ResolversInterfaceTypes<ResolversParentTypes>["CharacterNode"];
Fighter: Fighter;
Wizard: Wizard;
// other types...
};
```

The `RefType` generic is used to reference back to `ResolversTypes` and `ResolversParentTypes` in some cases such as field returning a Union.

2. `resolversNonOptionalTypename` also affects `ResolversInterfaceTypes`

Using the schema above, if we use `resolversNonOptionalTypename` option:

```typescript
const config: CodegenConfig = {
schema: 'src/schema/**/*.graphql',
generates: {
'src/schema/types.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
resolversNonOptionalTypename: true // Or `resolversNonOptionalTypename: { interfaceImplementingType: true }`
}
},
},
};
```

Then, the generated type looks like this:

```ts
export type ResolversInterfaceTypes<RefType extends Record<string, unknown>> = {
CharacterNode: (Fighter & { __typename: "Fighter" }) | (Wizard & { __typename: "Wizard" });
};

export type ResolversTypes = {
// other types...
CharacterNode: ResolverTypeWrapper<ResolversInterfaceTypes<ResolversTypes>["CharacterNode"]>;
Fighter: ResolverTypeWrapper<Fighter>;
Wizard: ResolverTypeWrapper<Wizard>;
// other types...
};

export type ResolversParentTypes = {
// other types...
CharacterNode: ResolversInterfaceTypes<ResolversParentTypes>["CharacterNode"];
Fighter: Fighter;
Wizard: Wizard;
// other types...
};
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file should be excluded by all dev-test/codegen.ts patterns that otherwise
# include files from dev-test-outer-dir, so that when running `yarn watch:examples`,
# updating this file should _never_ trigger rebuild
query NothingShouldUseThisQuery {
currentUser {
login
avatar_url
}
}
16 changes: 15 additions & 1 deletion dev-test/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ const config: CodegenConfig = {
},
'./dev-test/githunt/graphql-declared-modules.d.ts': {
schema: './dev-test/githunt/schema.json',
documents: ['./dev-test/githunt/**/*.graphql', './dev-test-outer-dir/githunt/**/*.graphql'],
documents: [
'./dev-test/githunt/**/*.graphql',
'./dev-test-outer-dir/githunt/**/*.graphql',
'!**/nothing-should-use-this-query.graphql',
],
plugins: ['typescript-graphql-files-modules'],
},
'./dev-test/githunt/typed-document-nodes.ts': {
Expand Down Expand Up @@ -121,6 +125,16 @@ const config: CodegenConfig = {
documents: './dev-test/star-wars/**/*.graphql',
plugins: ['typescript', 'typescript-operations'],
},
'./dev-test/star-wars/types.excludeQueryAlpha.ts': {
schema: './dev-test/star-wars/schema.json',
documents: ['./dev-test/star-wars/**/*.graphql', '!./dev-test/star-wars/**/ExcludeQueryAlpha.graphql'],
plugins: ['typescript', 'typescript-operations'],
},
'./dev-test/star-wars/types.excludeQueryBeta.ts': {
schema: './dev-test/star-wars/schema.json',
documents: ['./dev-test/star-wars/**/*.graphql', '!./dev-test/star-wars/**/ExcludeQueryBeta.graphql'],
plugins: ['typescript', 'typescript-operations'],
},
'./dev-test/star-wars/types.preResolveTypes.ts': {
schema: './dev-test/star-wars/schema.json',
documents: './dev-test/star-wars/**/*.graphql',
Expand Down
2 changes: 2 additions & 0 deletions dev-test/githunt/typed-document-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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: string;
Expand Down
2 changes: 2 additions & 0 deletions dev-test/githunt/types.avoidOptionals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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: string;
Expand Down
2 changes: 2 additions & 0 deletions dev-test/githunt/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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: string;
Expand Down
2 changes: 2 additions & 0 deletions dev-test/githunt/types.enumsAsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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: string;
Expand Down
2 changes: 2 additions & 0 deletions dev-test/githunt/types.flatten.preResolveTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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: string;
Expand Down
2 changes: 2 additions & 0 deletions dev-test/githunt/types.immutableTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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: string;
Expand Down
2 changes: 2 additions & 0 deletions dev-test/githunt/types.preResolveTypes.onlyOperationTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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: string;
Expand Down
2 changes: 2 additions & 0 deletions dev-test/githunt/types.preResolveTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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: string;
Expand Down
2 changes: 2 additions & 0 deletions dev-test/githunt/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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: string;
Expand Down
22 changes: 20 additions & 2 deletions dev-test/gql-tag-operations-masking/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ResultOf, DocumentTypeDecoration } from '@graphql-typed-document-node/core';
import { ResultOf, DocumentTypeDecoration, TypedDocumentNode } from '@graphql-typed-document-node/core';
import { FragmentDefinitionNode } from 'graphql';
import { Incremental } from './graphql.js';

export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> =
TDocumentType extends DocumentTypeDecoration<infer TType, any>
? TType extends { ' $fragmentName'?: infer TKey }
? [TType] extends [{ ' $fragmentName'?: infer TKey }]
? TKey extends string
? { ' $fragmentRefs'?: { [key in TKey]: TType } }
: never
Expand Down Expand Up @@ -46,3 +48,19 @@ export function makeFragmentData<F extends DocumentTypeDecoration<any, any>, FT
): FragmentType<F> {
return data as FragmentType<F>;
}
export function isFragmentReady<TQuery, TFrag>(
queryNode: DocumentTypeDecoration<TQuery, any>,
fragmentNode: TypedDocumentNode<TFrag>,
data: FragmentType<TypedDocumentNode<Incremental<TFrag>, any>> | null | undefined
): data is FragmentType<typeof fragmentNode> {
const deferredFields = (queryNode as { __meta__?: { deferredFields: Record<string, (keyof TFrag)[]> } }).__meta__
?.deferredFields;

if (!deferredFields) return true;

const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined;
const fragName = fragDef?.name?.value;

const fields = (fragName && deferredFields[fragName]) || [];
return fields.length > 0 && fields.every(field => data && field in data);
}
2 changes: 2 additions & 0 deletions dev-test/gql-tag-operations-masking/gql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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: string;
Expand Down
Loading

0 comments on commit 99a5f51

Please sign in to comment.