-
Would it help to add an example of objects or inputs that share fields? For example, create and update inputs share similar fields, with |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 3 replies
-
More docs are always good! This isn't something I've looked into yet. If you have a good pattern (or suggestion for an API addition that would make it easier) I'm definitely open to it! |
Beta Was this translation helpful? Give feedback.
-
@hayes given this example: const objectAFields = <T>(t: T) => ({
foo: t.exposeString('foo', {}),
});
class ObjectA {
foo: string;
}
const ObjectAObject = builder.objectType(ObjectA, {
name: 'ObjectA',
fields: objectAFields<SomeGiraphqlHelper>,
});
class ObjectB extends ObjectA {
bar: string;
}
const ObjectBObject = builder.objectType(ObjectB, {
name: 'ObjectA',
fields: (t) => ({
...objectAFields(t),
bar: t.exposeString('bar', {}),
}),
}); Ideally, there is a helper to pass in the type for |
Beta Was this translation helpful? Give feedback.
-
@mmahalwy added this for you https://giraphql.com/guide/patterns#sharing-fields-between-types |
Beta Was this translation helpful? Give feedback.
-
@hayes this is awesome. Thanks for sharing. I really like the pattern for common args and common inputs. Going to use the inputs especially as I have many Lastly, I'd imagine it's not possible to have common fields across input and object? I find myself having to repeat the fields for input and object often. This might be a bit more of an edge-case but here is an example: export const UserObject = builder
.objectRef<User>('User')
.implement({
fields: (t) => ({
id: t.exposeID('id', {}),
firstName: t.exposeString('content', { nullable: true }),
email: t.exposeString('content'),
}),
});
const CreateUserInput = builder.inputType('CreateUserInput', {
fields: (t) => ({
firstName: t.string({}),
email: t.string({ required: true }),
}),
}); Just a thought there :) |
Beta Was this translation helpful? Give feedback.
-
This might be something you could create a plugin for. You could add a For the object fields pattern, you can definitely use the same pattern, it is just easier (fewer types) to explain without the pattern in the docs. function createObjectFieldsFromBuilder(
t: GiraphQLSchemaTypes.ObjectFieldBuilder<TypesWithDefault, { id: string }>,
) {
return {
id: t.exposeID('id', {}),
idLength: t.int({
resolve: (parent) => parent.id.length,
}),
};
} |
Beta Was this translation helpful? Give feedback.
-
imho, I'd lead with this as an example in the docs! I love this pattern :) |
Beta Was this translation helpful? Give feedback.
@mmahalwy added this for you https://giraphql.com/guide/patterns#sharing-fields-between-types