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

Allow interface to contain implementation #60

Open
tonyxiao opened this issue Dec 15, 2021 · 1 comment
Open

Allow interface to contain implementation #60

tonyxiao opened this issue Dec 15, 2021 · 1 comment
Labels
question Further information is requested

Comments

@tonyxiao
Copy link

Here's an example of how we we gqtx today to be able to avoid duplicating code between implementers of the ILedger interface.

const LedgerInterface = t.interfaceType<Raw.Ledger>({
  name: 'ILedger',
  fields: () => [
    t.abstractField({name: 'id', type: t.String}),
    t.abstractField({name: 'name', type: t.String}),
    t.abstractField({name: 'users', type: t.List(LedgerUserType)}),
  ],
})

/**
 * Way to share implemention of the LedgerInterface
 * Need to figure out how to share the `users` implementation between books and tabs
 * Or perhaps better to not differentiate at all?
 */
function makeLedgerSubtype(options: {
  name: string
  isTypeOf: (l: Raw.Ledger) => boolean
  fields: () => Array<Field<GQLContext, Raw.Ledger, unknown, {}>>
}) {
  return t.objectType<Raw.Ledger>({
    name: options.name,
    interfaces: [LedgerInterface],
    isTypeOf: options.isTypeOf,
    fields: () => [
      t.field({name: 'id', type: t.String}),
      t.field({name: 'name', type: StringType}),
      t.field({
        name: 'users',
        type: t.List(UserType),
//        resolve: (ledger, __, ctx) =>
      }),
      ...options.fields(),
    ],
  })
}

It's a bit cumbersome, but still better than duplicating the code. In an ideal world the LedgerInterface would just be able to contain the implementation details (optionally) and avoid needing to duplicate all together.

@sikanhe
Copy link
Owner

sikanhe commented Jan 13, 2022

I think makeLedgerSubtype is a perfect way of using code level abstractions for shared implementations.

Relay for example use similar helper functions to create types.

@sikanhe sikanhe added the question Further information is requested label Jan 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants