-
-
Notifications
You must be signed in to change notification settings - Fork 674
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
Fields decorator to inject parsed AST info #10
Comments
Is there a (possibly hacky) way to achieve this behaviour currently? It is something I'd really like to be able to use but seems like it's not going to be implemented for a while yet. Or, if it's straight forward enough, would making a PR for it be suitable? |
For anyone else looking for this, it wasn't documented too much but I found the For example: async deliveries(@Arg("filters") filters: DeliveryFilters, @Ctx() context: AuthenticatedContext, @Info() info: GraphQLResolveInfo) { |
Yes, you can always fallback to the pure |
Thanks to #329, now it's possible to implement this feature 🎉 If someone want to try, please discuss the API and interfaces here before submitting the PR 😉 |
@MichalLytek I'm kinda not sure there is a need for lib decorator when small custom function suffice. But I'm coming from FP, and trying to get used to DI so there's that :) Parsing ResolveInfo into single sql query is something I'm trying right now so - I can help with this PR. Regarding
Both options do exactly that. With one in the end.
It's just
Do you mean like transform/ rename option? const transform = {
graphqlFieldName: "entityFieldName"
}
myField(@InfoFields({ transform } fields: InfoFields) {
...
} Should it be global or scoped with lodash paths? On both sides? // scoped with lodash paths on both sides
const transform = {
"graphqlFieldName.Type.OtherType": "entityFieldName.RenamedType.OtherType"
}
Proposed API interface InfoFieldsOptions {
// renames field (possibly as lodash paths)
transform?: { [field: string]: string }
// returns array of top level fields overload
asArray?: boolean
// (optional, esspecially with `graphql-fields-list`)
// scope and pick nested tree branch with lodash path
path?: string
// ignore (skip?) specified fieldnames
ignore?: string[]
}
myField(@InfoFields({ options }) fields: InfoFieldsProjection ) {
...
} Sorry for that many questions - I just need to know requirements, so I can get it to your style.
Cheers 🙃 |
No, we have a rename field syntax: @Field({ name: "bar" })
foo!: string; It's designed for abstract generic types and resolvers but we should support that mapping in the
It's burdensome in maintenance, we should just use the existing tools and only apply some transformations on the results, not dealing with the AST, fragments and variables by our own. |
Here's a very simple decorator using graphql-parse-resolve-info instead of graphql-fields: import {parseResolveInfo, ResolveTree} from "graphql-parse-resolve-info";
import {createParamDecorator} from "type-graphql";
function Fields(): ParameterDecorator {
return createParamDecorator(
({info}): ResolveTree => {
const parsedResolveInfoFragment = parseResolveInfo(info);
if (!parsedResolveInfoFragment) {
throw new Error("Failed to parse resolve info.");
}
return parsedResolveInfoFragment;
}
);
}
export {Fields}; Usage: @Query(() => [User])
public async users(@Fields() fields: ResolveTree): Promise<User[]> {
console.dir(fields);
} Mapping GraphQL field names to class property names is blocked by #123. You'll probably also want to simplify the structure, depending on your database API. |
I would like to share the Field Decorator I just made: in combination with mongoose, it takes a field reference and returns the object I need with the minimum operations.
Maybe someone will find that useful.
|
Some people instead of batching and caching separate resolvers (#51) prefer to load all the data manually. So when they resolve the query, they parse the resolve info object to get a list/structure of fields to fetch from DB or API and then create specific query to DB/API.
Also it might be useful for selecting fields/columns from DB, e.g. when the GraphQL query requires only a single field but our database SQL query has to be universal so it has
*
so it returns a lot of unnecessary data, like other 30 columns/fields.It would be nice to have a decorator for that with nice integration with
graphql-fields
. It should convert the returned object to a mongo-like convention (robrichard/graphql-fields#5) and have an ability to return array of keys in first level (without nesting) for sql queries optimization.It should also take care about mapping the GraphQL field names (provided with
{ name: "fieldName" }
option) to the TS property names.The text was updated successfully, but these errors were encountered: