Skip to content
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

Is it possible to retrieve the child nodes when only the parent field is sent in the query? #3919

Open
Archana2105 opened this issue Feb 22, 2024 · 7 comments

Comments

@Archana2105
Copy link

I am using .Net core 6 web api to retrieve data from MongoDB. I have a collection with multiple multi level nested objects. So, when the query is formed it has multi level objects and fields. The requirement would be simplifying the query. Is it possible to retrieve the child node values by sending only the parent fields in the Query?

@Shane32
Copy link
Member

Shane32 commented Feb 22, 2024

I’m not quite sure I understand the question. Can you give a bit of an example of what you’re trying to accomplish ?

@Archana2105
Copy link
Author

Archana2105 commented Feb 22, 2024

If I have a Query like this (ORIGINAL STRUCTURE)

passengersInfo 
			{
				id 
				lastName 
				firstName 
				title 
				originalFirstName 
				originalLastName 
				nameSequenceID 
				nameStatus 
				serviceRequests 
					{
						serviceType 
						serviceCode 
						freeText 
						statusCode 
					}
				ticketDetails 
					{
						ticketNumber 
						agentSine 
						isPaperTicket 
						originalTicketDetails 
						
					}
							
				passengerDocument 
				{
					travelDocuments 
						{
							firstName 
							middleName 
							lastName 
							dateOfBirth 
							gender 
							documentType 
							cityOfIssuance 
							countryOfCitizenship 
							countryOfIssuance 
							documentExpirationDate 
							documentNumber 
							status 
							visaApplicableCountry 
							isInfant 
						}
					
					residenceAddress
						{
							country 
						}
					redress 
						{
							redressNumber 
						}
					destinationAddress 
						{
							address1 
							city 
							country 
							state 
							zipCode 
						}
					loyaltyInfo 
						{
							loyaltyNumber 
							loyaltyAirlineCode 
							tierStatus 
							tierStatusDetail 
							status 
							searchableLoyaltyNumber  
						}
					bookingCustomerMasterLink_id 
				}

Is it possible to pass the query as below (REQUIRED STRUCTURE)

passengersInfo
{
	id 
	lastName 
	firstName 
	title 
	originalFirstName 
	originalLastName 
	nameSequenceID 
	nameStatus 
	serviceRequests {}
	ticketDetails {}
	passengerDocument
	{
		travelDocuments {}
		residenceAddress
		redress{}
		destinationAddress{}
		loyaltyInfo{}
	}
}

Is there any functionality which would help me to send the query (REQUIRED STRUCTURE) and retreive all the fields under the empty braces sub child nodes of Child nodes indicated with {}

@Shane32
Copy link
Member

Shane32 commented Feb 22, 2024

Send to where and retrieve from where?

Are you trying to create a new GraphQL query from an existing one to send to another server ? Or to analyze an incoming request to be able to pull more information at once from your database to fulfill the request ?

@Archana2105
Copy link
Author

Archana2105 commented Feb 23, 2024

I am sending it to the MongoDB to retrieve the data from one of its collections.

I am trying to create a new GraphQL query for a multi level nested document inside a collection.

Since the objects are nested at multilevels, the query seems to be so complicated. So, inorder to make it simple and crisp for the consumers at the frontend, I am trying to simplify the query.

I am planning to implement Unions so that the child level objects can be grouped together and could be queried at once using the single Union name.

For example,

public sealed class PassengerDocumentUnion : UnionGraphType
{
    public PassengerDocumentUnion()
    {
        Type<TravelDocumentType>();
        Type<ResidenceAddressType>();
        Type<RedressType>();           
        Type<LoyaltyInfoType>();

    }
}

So, the Query can be sent as follows:

passengersInfo
{
id
lastName
firstName
title
originalFirstName
originalLastName
nameSequenceID
nameStatus
serviceRequests {}
ticketDetails {}
passengerDocument
{
[PassengerDocumentUnion]
}
}

Please help me how to resolve or implement this Unions or let me know if there are any other way to retrieve the child level objects without sending them in the Query.

@Shane32
Copy link
Member

Shane32 commented Feb 25, 2024

I'm sorry I don't understand. I've never used MongoDB or other document-orientated NoSQL databases, so maybe that's why I'm not grasping what you're trying to accomplish.

I can help you:

  • Implement a union
  • Parse a GraphQL request AST
  • Understand how requests are processed internally to GraphQL.NET

For my own SQL based databases, I've spent a lot of time trying to take a GraphQL request and convert it to a SQL command to be executed. Perhaps you are attempting to accomplish something similar. If that is the case, I can say that you may run into complications when it comes to proper implementation of GraphQL's interface, union and fragment support. The issue stems from the fact that the execution engine won't know what child fields need to be resolved until the returned type is determined. For example, consider the below query:

query {
  person (id: "5") {
    firstName
    lastName
    pets {
      name
      ... on Bird {
        wingSpan
      }
      ...dogFields
    }
  }
}

fragment dogFields on Dog {
  breed
}

So if you query GraphQL.NET for the subfields for pets, it won't provide an answer, because it may differ based on the resolved type of each pet.

@CarsonTolleshaug
Copy link

I'm just an outsider here who uses this package and happened to stumble upon this thread, please ignore if this does not help.

I think what @Archana2105 is trying to do is allow the client of their API to request all subfields of an object property, without specifying any of the subfields explicitly in the request. This is not possible in graphQL, I think mainly because it kind of goes against the core philosophy of GraphQL. The client needs to specify what it wants and only what it wants so that the server can perform the minimum amount of work needed to fill the request. If your desire is to have the client make blanket / catchall requests, you might consider making a REST or RPC API instead.

See relevant GraphQL spec discussion: graphql/graphql-spec#127

@Shane32
Copy link
Member

Shane32 commented May 18, 2024

@Archana2105 does this answer your question?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants