-
Notifications
You must be signed in to change notification settings - Fork 2
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
[Idea] Opinionated (graphql) transformer #34
Comments
I like the idea but I also like that all transformers have the same API no matter their type 🤔. One thing, unrelated to this, is that I wonder if we should "auto-build" nested fields if possible. That removes already quite a bit of cruft. |
I encountered a use case where having // Generator
const generator = Generator({
fields: {
name: fake(() => LocalizedString.random()),
description: fake((f) =>
f.random.arrayElement([null, LocalizedString.random()])
),
},
});
// Transformers
function omitTypename(object) {
return omitDeep(object, '__typename');
}
const transformers = {
graphql: Transformer('graphql', {
replaceFields({ fields }) {
return {
...fields,
name: buildField(fields.name, 'graphql').map(omitTypename),
description:
fields.description &&
buildField(fields.description, 'graphql').map(omitTypename),
};
},
}),
};
// Usage
const attributeGroupDraft = AttributeGroupDraft.random().buildGraphql(); However, this is not ideal as doing this will become redundant (needs to be done for each generated GraphQL data). One way to solve this is to get inspired by how Apollo GraphQL handles // Generator
const generator = Generator({
fields: {
name: fake(() => LocalizedString.random()),
description: fake((f) =>
f.random.arrayElement([null, LocalizedString.random()])
),
},
});
// Transformers
-function omitTypename(object) {
- return omitDeep(object, '__typename');
-}
const transformers = {
graphql: Transformer('graphql', {
- replaceFields({ fields }) {
- return {
- ...fields,
- name: buildField(fields.name, 'graphql').map(omitTypename),
- description:
- fields.description &&
- buildField(fields.description, 'graphql').map(omitTypename),
- };
- },
+ buildFields: ['name', 'description'],
}),
};
// Usage
- const attributeGroupDraft = AttributeGroupDraft.random().buildGraphql();
+ const attributeGroupDraft = AttributeGroupDraft.random().buildGraphql({ addTypename: false }); |
Is your feature request related to a problem? Please describe.
At the moment 98% of our graphql transformers use the
addFields
to set the__typename
.Describe the solution you'd like
Maybe we should provide a preconfigured graphql transformer that always sets the
__typename
.For example:
Describe alternatives you've considered
Instead of
Transformer.graphql
, we can have aGraphqlTransformer
.The text was updated successfully, but these errors were encountered: