-
I'm coming from Nexus and i was using factories like below to create similar input types: const makeAddressMutationsInputTypes = () => {
return ['create', 'update'].map((operation) =>
inputObjectType({
name: operation === 'create' ? 'AddressCreateInput' : 'AddressUpdateInput',
definition(t) {
if (operation === "update") {
t.string('id');
}
t.string("address1", {
required: operation === "create",
});
t.string('address2');
t.string("city", {
required: operation === "create",
});
t.string("postal", {
required: operation === "create",
});
t.string("country", {
required: operation === "create",
});
t.string('firstName');
t.string('lastName');
t.string("company");
t.string("phone");
t.float("latitude");
t.float("longitude");
t.boolean('isCommercial');
t.boolean('isDefault');
},
})
);
};
export const AddressMutationsInputTypes = makeAddressMutationsInputTypes(); I can't figure out how to do something like this with Pothos. Types are not accurate... I tried to do something like this, but how can I add fields conditionally with some parameters? function createAddressMutationsInputFields<T extends boolean>(
t: InputObjectFieldBuilder,
options: {
required: T;
}
) {
return {
address1: t.string({ required: options.required, defaultValue: undefined }),
city: t.string({ required: options.required }),
country: t.string({ required: options.required }),
postal: t.string({ required: options.required }),
address2: t.string({ required: false }),
company: t.string({ required: false }),
firstName: t.string({ required: false }),
lastName: t.string({ required: false }),
phone: t.string({ required: false }),
latitude: t.float({ required: false }),
longitude: t.float({ required: false }),
isCommercial: t.boolean({ required: true, defaultValue: false }),
isDefault: t.boolean({ required: true, defaultValue: false }),
};
}
export const AddressCreateInput = builder.inputType("AddressCreateInput", {
fields: (t) => ({
...createAddressMutationsInputFields(t, { required: true }),
}),
});
export const AddressUpdateInput = builder.inputType('AddressUpdateInput', {
fields: (t) => ({
...createAddressMutationsInputFields(t, { required: false }),
}),
}); |
Beta Was this translation helpful? Give feedback.
Answered by
hayes
Nov 14, 2022
Replies: 1 comment 1 reply
-
This is definitely a little harder in pothos. What I would probably do is create something that has the input types for both, but separate them into separate objects so they can be typed independently: |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
durdenx
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is definitely a little harder in pothos.
What I would probably do is create something that has the input types for both, but separate them into separate objects so they can be typed independently:
https://stackblitz.com/edit/typescript-tnqzux?file=index.ts