-
-
Notifications
You must be signed in to change notification settings - Fork 677
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
Using @Extensions and Object Inheritance problem #776
Comments
have you tried |
Thanks for the answer. If I switch to BaseClass, it says, that extendedField cannot be null (because the field is not being resolved at all). |
@Resolver((of) => BaseClass)
class BaseResolver {
@Extensions({ trace: true })
@FieldResolver((returns) => Int)
async baseField() {
...
}
}
@Resolver((of) => ExtendedClass)
class ExtendedResolver {
@Extensions({ trace: true })
@FieldResolver((returns) => Int)
async extendedField() {
...
}
} Looks like you need to read all the docs again cause you still don't know what the |
I do know how inheritance works and when to use it. Maybe a little bit more about my use case: I use I took the example for extending classes with ObjectType annotation from the docs here: https://typegraphql.com/docs/inheritance.html I thought this is the way to define the base fields in a reusable manner. So, resolving works properly, only when I introduced extension params via The only other way to approach this use case, could be https://typegraphql.com/docs/interfaces.html. Is this what you would suggest? |
I still don't understand your use case neither what you're trying to achieve. If you define extensions on child class field resolver, it won't automagically register for base class field. You can put |
Ok, let's make it more specific. I have 3 user models, which I want to use in my schema: @ObjectType()
class BaseUser {
@Field()
pictureCount: number
@Field()
pictureCount: Picture
}
@ObjectType()
class User extends BaseUser {
@Field()
isFavorite: boolean
}
@ObjectType()
class ActiveUser extends BaseUser {
@Field()
isOnboarding: boolean
}
@ObjectType()
class AdminUser extends BaseUser {
@Field()
lastLogin: number
} BaseUser should just define the common properties of the 3 other classes, but is not part of the schema itself and does not have a corresponding resolver. Then I have created the resolvers for @Resolver((of) => User)
class UserResolver {
@Extensions({ trace: true })
@FieldResolver((returns) => Int)
async pictureCount() {
//fetch pictureCount from DB
}
@Extensions({ trace: true })
@FieldResolver((returns) => Int)
async isFavorite() {
// fetch favorite relation from DB
}
} I want to control, which field resolver executions are being traced and which are not for
Actually it works for the inherited fields, but not for the fields defined in the child class. In this example, the trace flag is correctly applied to the metainformation of
I want to be able to make the decision for each child class seperately, this is why I don't want to define it on the base class, also it increases readability, as I mostly only want to trace async field resolvers. Does it make more sense now? |
Ok, now it makes sense and now I get it. I was able to reproduce the issue: describe.only("Fields with field resolvers", () => {
beforeAll(async () => {
getMetadataStorage().clear();
@ObjectType()
@Extensions({ parentClass: true })
class Parent {
@Field()
@Extensions({ parentField: true })
parentField!: string;
}
@Extensions({ childClass: true })
@ObjectType()
class Child extends Parent {
@Field()
@Extensions({ childField: true })
childField!: string;
}
@Resolver(of => Child)
class ChildResolver {
@Query()
sampleQuery(): Child {
return {} as Child;
}
@Extensions({ childFieldResolver: true })
@FieldResolver()
childField(): string {
return "childField";
}
}
schema = await buildSchema({
resolvers: [ChildResolver],
orphanedTypes: [Parent],
});
});
it("should merge field level with field resolver level extensions", () => {
const childObjectType = schema.getType("Child") as GraphQLObjectType;
expect(childObjectType.getFields().childField.extensions).toEqual({
childField: true,
childFieldResolver: true,
});
});
}); Thanks for the report, gonna fix that soon 😉 |
Fixed by 73736e2 🔒 |
Awesome! Thanks and btw. this is an awesome project, I really like to use it in my projects and appreciate your effort. |
Describe the Bug
I use
@Extensions
annotation on field resolver to define custom tracing instructions as I only want to trace some field resolver executions. I added the annotation to several field resolvers and some are working as expected and some are failing.I could identify a pattern, where the extensions are passed properly on fields, which are defined in the base class, but not on fields, that are defined in the inherited class.
To Reproduce
Expected Behavior
Accessing the Metadata in my plugin via
should return true for both field resolvers, but it only does for
baseField
Logs
Not applicable
Environment (please complete the following information):
Additional Context
The text was updated successfully, but these errors were encountered: