-
Notifications
You must be signed in to change notification settings - Fork 66
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
Proposal: Provide a Way to Access the GraphQL Field Information from the Resolver Functions #3893
Proposal: Provide a Way to Access the GraphQL Field Information from the Resolver Functions #3893
Comments
Field
Object
A very good proposal! I am not sure about the order of arguments though. It is a guide line but not a hard and fast rule right. |
Just to understand why |
So you mean, we remove the rule altogether or make it possible to use the
This is an already existing object. It is used to pass |
For the moment, we decided to go ahead with the
Therefore, we are going ahead with this proposal. Hence, marking the proposal as accepted. |
Summary
When a GraphQL field is processed, it is helpful to have additional information about the field such as the child fields. This proposal is to provide a way for the user to access the additional information using the
graphql:Field
object.Goals
Non-Goals
Motivation
In GraphQL, the client requests the field they require, and the server is guaranteed to return only the fields the client requested. In most cases, this functionality is used to optimize the backend database queries so that the database is accessed to retrieve the values requested by the client. In Ballerina, currently, this optimization is not possible because the developer does not have access to this information. This proposal is to provide a way for the developer to access this information so that they can optimize their database queries and execution plans.
Description
The
graphql:Field
ObjectCurrently, the Ballerina GraphQL package has a
graphql:Field
object, which exposes the field name and the alias (if there's any). Thegraphql:Field
object has the following APIs:getName()
This API returns the name of the field as defined in the GraphQL document.
getAlias()
This API returns the effective alias of the field. This means if the document has an alias defined for the field, this will return the alias. Otherwise, it will return the field name.
In addition to these existing APIs, the following APIs are proposed to introduce.
getSubfieldNames()
This API returns
string[]
that contains the names of the child fields of the current field. This will include the subfields of the field if the field is a GraphQLOBJECT
type. Otherwise, it will return an empty array. The resulting array includes the subfields including the fragment fields if there are any. Following is the definition of the method:getPath()
This API returns the current path of the field from the root operation. If the field is a part of an array, the array index is also included. Following is the definition of the method:
getType()
This API returns a
graphql:__Type
record that contains the type information of the field. Following is the definition of the method:Accessing the Field Object
Accessing the
graphql:Field
is currently supported in interceptors, using theexecute
method. But for most use cases, theField
object should be accessed, even without an interceptor. Therefore, this proposal proposes to add thegraphql:Field
object as an input parameter for theresource
orremote
methods that represents a GraphQL field. This is similar to how the context is accessed.With this proposal, we are removing the limitations for the
graphql:Context
andgraphql:Field
parameter definition rules in theresource
andremote
methods inside the Ballerina GraphQL services. This means, we no longer validate the parameter position of thegraphql:Context
or thegraphql:Field
object in aresource
or aremote
method. They can be defined anywhere in the parameter list. But it is recommended to use them as the first and the second parameter in a function for better readability in the code.Example: Accessing the Field Object
Counter Example: Field Defined in Invalid Locations
Alternatives
Instead of adding the
graphql:FIeld
as an input parameter in aresource
or aremote
method, an alternative approach would be to add thegraphql:Field
to thegraphql:Context
object, and introduce an API,getField()
in thegraphql:Context
object. This approach was considered first, but it has a couple of drawbacks.graphql:Context
object whenever they need to access thegraphql:Field
, even though thegraphql:Context
might not needed.graphql:Context
and thegraphql:Field
does not seem a good idea as they are designated for two different things:graphql:Context
is to transfer/access meta-information per-request.graphql:Field
is to access meta-information per-field.Due to the above reasons, this proposal is going forward with the approach mentioned in the above Accessing the Field Object section.
The text was updated successfully, but these errors were encountered: