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

Extending objects #935

Open
mmahalwy opened this issue Jun 13, 2023 · 2 comments
Open

Extending objects #935

mmahalwy opened this issue Jun 13, 2023 · 2 comments

Comments

@mmahalwy
Copy link
Contributor

This could be placed in the "Patterns" section of the doc but curious if there is best practices or potential future support for extending an object (which would create a new one) instead of using interfaces. My thinking is:

const UserObject = builder.objectRef<{
  email: string;
  address?: string
}>('User').implement({
  fields: t => ({
    email: t.exposeString('email'),
    address: t.exposeString('address', { nullable: true })
  })
});


const UserWithAddressObject = UserObject.extend<{
  address: string;
}>('UserWithAddress').implement({
  fields: t => ({
    address: t.exposeString('address')
  })
});

As a result, the UserWithAddressObject would interface with the previous one. On a graphql schema, these are 2 separate objects. This is a bit different from graphql interfaces which tend to have the common fields on the interface and have objects extend. This can be achieved with interfaces with:

const UserInterface = builder.interfaceRef<{email: string}>('UserInterface').implement({
  fields: t => ({
    email: t.exposeString('email')
  })
})

const UserObject = builder.objectRef<{
  email: string;
  address?: string
}>('User').implement({
  interfaces: [UserInterface],
  fields: t => ({
    address: t.exposeString('address', { nullable: true })
  })
});

const UserObjectWithAddress = builder.objectRef<{
  email: string;
  address: string;
}>('UserWithAddress').implement({
  interfaces: [UserInterface],
  fields: t => ({
    address: t.exposeString('address')
  })
});
@hayes
Copy link
Owner

hayes commented Jun 13, 2023

Patterns like this will be easier to build in v4, which is in progress, but I haven't had much time lately to work on it. V4 pushes all of the configs and field definitions into the refs, which makes it easier to add methods on refs that allow extending or mutating them.

There aren't any concrete plans for a feature like this, but making things more modular and extensible was part of the goal for v4

@mmahalwy
Copy link
Contributor Author

Oh interesting. Didnt know there's a v4 in progress. Will check it out!

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

No branches or pull requests

2 participants