Skip to content

Conversation

@emmatown
Copy link
Member

@emmatown emmatown commented Feb 26, 2025

The Key type parameter on Field has been replaced with a new type parameter (SourceAtKey) and represents essentially Source[Key] instead of Key. For example, a field like this can be written:

const field = g.field({
  type: g.String,
});

and field will be usable like this:

const Something = g.object<{
  name: string
}>({
  name: "Something"
  fields: {
    name: field,
    // @ts-expect-error
    other: field
  },
});

The field is usable at name since the source type has an object with a name property that's a string but using it at other will result in a type error since the source type doesn't have a other property.

Previously, using g.field outside a g.object/g.fields call would require specifying a resolver and fields written within g.fields would be bound to be used at a specific key rather than the new behaviour of any key with the right type.

This also reduces the need for g.fields. For example, the example given in the previous JSDoc for g.fields:

const nodeFields = g.fields<{ id: string }>()({
  id: g.field({ type: g.ID }),
});
const Node = g.interface<{ id: string }>()({
  name: "Node",
  fields: nodeFields,
});
const Person = g.object<{
  __typename: "Person";
  id: string;
  name: string;
}>()({
  name: "Person",
  interfaces: [Node],
  fields: {
    ...nodeFields,
    name: g.field({ type: g.String }),
  },
});

Now the g.fields call is unnecessary and writing nodeFields will no longer error at the g.field call and will instead work as expected.

const nodeFields = {
  id: g.field({ type: g.ID }),
};

There is still some use to g.fields for when you want to define a number of shared fields with resolvers and specify the source type just once in the g.fields call rathe than in every resolver.

This change is unlikely to break existing code except where you explicitly use the Field type or explicitly pass type parameters to g.field (the latter of which you likely shouldn't do) but since it changes the meaning of a type parameter of Field, it's regarded as a breaking change.

@emmatown emmatown requested a review from Copilot February 27, 2025 03:01
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

PR Overview

This pull request refactors the type signatures for Field definitions by replacing the generic Key parameter with one that better reflects its role on the field, aligning with the PR title. Key changes include adjustments in the Field type definition and related type utility functions, as well as corresponding updates in test files and API exports.

  • Updated Field type definitions to replace the Key parameter with a new parameter.
  • Adjusted dependent types and utility functions in the schema and extend packages.
  • Modified tests and API definitions to align with the updated type signatures.

Reviewed Changes

File Description
packages/schema/src/output.ts Updated Field type signature and helper types to use a new parameter instead of Key.
test-project/index.test-d.ts Adjusted test expectations to reflect updated Field typings.
packages/extend/src/index.ts Revised field key type in the index signature for Field usage.
packages/schema/src/schema-api.ts Modified Field type export to replace the Key parameter with the new one.
test-project/schema-api/index.ts Updated Field type export to match schema-api changes.
packages/schema/src/index.ts Removed legacy export types conflicting with the new definitions.

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (4)

packages/schema/src/output.ts:147

  • The new parameter 'SourceOnField' is introduced later to replace the previous 'Key' parameter; however, the PR title suggests using 'FieldOnSource'. Consider renaming it to 'FieldOnSource' for consistency with the PR title and documentation.
Args extends { [Key in keyof Args]: Arg<InputType> },

packages/schema/src/schema-api.ts:201

  • The replacement of the 'Key' parameter with 'SourceOnField' here may be confusing given the PR title; aligning the parameter name to 'FieldOnSource' could improve clarity.
SourceOnField

test-project/schema-api/index.ts:22

  • As with the primary schema API changes, consider renaming 'SourceOnField' to 'FieldOnSource' in this file to ensure consistent naming across the codebase.
SourceOnField

packages/extend/src/index.ts:379

  • Changing the field key type from 'string' to 'unknown' may affect type inference or downstream usage; please verify that this change aligns with the intended design and that usage sites can handle an 'unknown' key type.
[key: string]: Field<unknown, any, OutputType<any>, unknown, any>;

@emmatown emmatown force-pushed the field-key-to-source-on-field branch 3 times, most recently from 77601d6 to 1df4859 Compare February 27, 2025 03:38
@emmatown emmatown changed the title Change Key param on Field to FieldOnSource Change Key param on Field to SourceAtKey Feb 27, 2025
@emmatown emmatown force-pushed the field-key-to-source-on-field branch 3 times, most recently from 204b978 to 518102f Compare February 27, 2025 03:46
@emmatown emmatown force-pushed the field-key-to-source-on-field branch 14 times, most recently from 265cbe8 to a5b5609 Compare March 3, 2025 01:50
@emmatown emmatown marked this pull request as ready for review March 3, 2025 02:58
@emmatown emmatown force-pushed the field-key-to-source-on-field branch 5 times, most recently from 17fe2ce to 63aca19 Compare March 7, 2025 00:42
@emmatown emmatown force-pushed the field-key-to-source-on-field branch from 63aca19 to b9aacb9 Compare March 7, 2025 01:46
@emmatown emmatown merged commit a0d9ea2 into main Mar 11, 2025
7 checks passed
@emmatown emmatown deleted the field-key-to-source-on-field branch March 11, 2025 06:04
@github-actions github-actions bot mentioned this pull request Mar 11, 2025
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.

2 participants