Child relationship mapping schema NReco.GraphQL documentation


It is possible to define a single child object (one-to-one) or multiple child objects (one-to-many) with RelatedObjects section in of the object's schema. Nested query to child objects may include a filter, sort, aggregate fields, and pagination parameters. In case of multiple queries, N+1 problem is solved with DataLoader that reduces number of SQL queries (see when DataLoader is used for loading nested objects).

{
 "SchemaObjects": [
  {
   "SingleName": "company",
   "ListName": "companies",
   "Table": "Companies",
   "Description": "Companies table - contains data about the best companies ever",
   "Fields": [
    {
     "Name": "id",
     "Column": "ID",
     "DataType": "string",
     "Description": "Unique identificator - using also to connect with contact"
    },
    {
     "Name": "title",
     "DataType": "string"
    }
   ],
   "RelatedObjects": [
    {
     "Name": "contact", // refers to the name of related object (single)
     "Relex": "Contacts(company_id=\"id\":var)[*]" // query used to load related object
    },
	{
     "Name": "contacts", // refers to the name of related objects (an array)
     "Relex": "Contacts(company_id=\"id\":var)[*]" // query used to load related object
    }
   ]
  },
  {
   "SingleName": "contact",
   "ListName": "contacts",
   "Table": "Contacts",
   "Fields": [
    {
     "Name": "id",
     "Column": "ID",
     "DataType": "int32"
    },
    {
     "Name": "first_name",
     "Column": "FirstName",
     "DataType": "string"
    },
    {
     "Name": "last_name",
     "DataType": "string"
    }
   ]
  }
 ]
}
Property Purpose
Name Refers to the name of related object (see ObjectSchema -> SingleName or ObjectSchema -> ListName).
QueryFieldName

Alternative schema object name (instead of "Name"). It will be available in GraphQL IDE (optional).

Used when we have two (or more) relations to the same object name, but connected with different Relex queries.

Relex

Defines relations between current schema and related schema. Syntax based on Relex

Parent schema object and current schema object arguments are allowed to use.

Fetching child (nested) objects

																			
query {
	company {
		id
		contact {
			id
			first_name
		}
	}
}
{
	"data": {
		"company": {
			"id": 1,
			"title": "NReco Inc.",
			"contact": {
				"id": 1,
				"first_name": "Dave"
			}
		}
	}
}