Skip to content

Conversation

@emmatown
Copy link
Member

@emmatown emmatown commented Mar 10, 2025

This is an alternative to #41 attempting to solve the same problem.

Instead of the big type definition of g, g is defined as "get the return type of the function but if the function takes no arguments and the function returns a function that does take arguments, get the return type of that function". So this can be used with the functions like g.object + instantiation expressions. This is definitely a much neater design than #40 and nicely has less API going on but it has some problems:

  • Defining circular input object types does not work (this is the main problem)
  • This doesn't include something to easily get the types like OutputType/etc.
  • If defining circular input object types did work, the extra typeof and g. and <...> syntax clutter up definitions a lot

While these problems aren't great, I think they're probably the right trade-offs and for cases where g<typeof g.something<...>> don't work, people can use the exports from @graphql-ts/schema especially since those cases are much more advanced use cases.

Here's the same example as in #41 but using this

import { initG } from "@graphql-ts/schema";
import { GraphQLSchema, graphql } from "graphql";

type Context = {
  loadPerson: (id: string) => Person | undefined;
  loadFriends: (id: string) => Person[];
};
const g = initG<Context>();
type g<T> = initG<T>;

type Person = {
  id: string;
  name: string;
};

const Person: g<typeof g.object<Person>> = g.object<Person>()({
  name: "Person",
  fields: () => ({
    id: g.field({ type: g.nonNull(g.ID) }),
    name: g.field({ type: g.nonNull(g.String) }),
    friends: g.field({
      type: g.list(g.nonNull(Person)),
      resolve(source, _, context) {
        return context.loadFriends(source.id);
      },
    }),
  }),
});

@emmatown emmatown force-pushed the easier-context-binding-alternative branch from 7b6cc46 to acc5be9 Compare March 10, 2025 01:12
@emmatown emmatown changed the title Easier context binding alternative Easier context binding with instantiation expressions Mar 10, 2025
@emmatown emmatown force-pushed the easier-context-binding-alternative branch from acc5be9 to f7302fe Compare March 10, 2025 01:18
@emmatown emmatown force-pushed the use-real-graphql-js-types branch from 1d51000 to 663b3d2 Compare March 10, 2025 06:08
@emmatown emmatown force-pushed the easier-context-binding-alternative branch 3 times, most recently from 40a79d8 to d4a7a5d Compare March 10, 2025 23:40
@emmatown emmatown force-pushed the use-real-graphql-js-types branch from 663b3d2 to 5eafeea Compare March 10, 2025 23:40
@emmatown emmatown force-pushed the easier-context-binding-alternative branch from d4a7a5d to e647ef6 Compare March 11, 2025 06:00
@emmatown emmatown force-pushed the use-real-graphql-js-types branch from 5eafeea to b502533 Compare March 11, 2025 06:05
@emmatown emmatown force-pushed the easier-context-binding-alternative branch 2 times, most recently from affb9c2 to 43657cc Compare March 11, 2025 06:06
@emmatown emmatown marked this pull request as ready for review March 11, 2025 06:21
@emmatown emmatown force-pushed the easier-context-binding-alternative branch from 43657cc to fc72620 Compare March 11, 2025 06:28
@emmatown emmatown force-pushed the use-real-graphql-js-types branch from b502533 to 083a8e5 Compare March 11, 2025 06:28
@emmatown emmatown force-pushed the easier-context-binding-alternative branch 2 times, most recently from 21d867e to 45879c5 Compare March 11, 2025 06:55
@socket-security
Copy link

socket-security bot commented Mar 11, 2025

Updated dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@types/node@22.10.1022.13.10 None +1 2.4 MB types

View full report↗︎

@emmatown emmatown force-pushed the easier-context-binding-alternative branch from 45879c5 to 5d6be9c Compare March 11, 2025 07:08
@emmatown emmatown force-pushed the easier-context-binding-alternative branch 2 times, most recently from 68ba251 to 802c741 Compare March 11, 2025 07:23
@emmatown emmatown force-pushed the easier-context-binding-alternative branch from 802c741 to 4f3ab67 Compare March 11, 2025 22:42
Base automatically changed from use-real-graphql-js-types to main March 11, 2025 23:06
@emmatown emmatown force-pushed the easier-context-binding-alternative branch 2 times, most recently from 2ac2e9c to 81768d4 Compare March 11, 2025 23:10
@emmatown emmatown changed the base branch from main to initG March 11, 2025 23:10
@emmatown emmatown force-pushed the easier-context-binding-alternative branch 2 times, most recently from 7fe3d80 to 41a7d27 Compare March 11, 2025 23:25
@emmatown emmatown force-pushed the easier-context-binding-alternative branch 3 times, most recently from 21e0807 to 4c7902e Compare March 11, 2025 23:36
@emmatown emmatown force-pushed the easier-context-binding-alternative branch from 4c7902e to e957227 Compare March 11, 2025 23:38
@emmatown emmatown force-pushed the easier-context-binding-alternative branch from e957227 to 5fb2a5e Compare March 11, 2025 23:41
Base automatically changed from initG to main March 11, 2025 23:55
@emmatown emmatown force-pushed the easier-context-binding-alternative branch from 5fb2a5e to 00af7c4 Compare March 11, 2025 23:55
@emmatown emmatown enabled auto-merge (squash) March 11, 2025 23:56
@emmatown emmatown merged commit c80239f into main Mar 11, 2025
6 checks passed
@emmatown emmatown deleted the easier-context-binding-alternative branch March 11, 2025 23:56
@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