void ExecuteSelectionSet(ModifiableSelectionSet ss, __Type objectType, Object objectValue, Dictionary <string, Object> variableValues) { var visitedFragments = new Dictionary <string, GraphQLParser.FragmentDefinitionContext>(); var groupedFieldSet = CollectFields(objectType, ss, variableValues, visitedFragments); var resultMap = new OrderedDictionary <string, object>(); foreach (string responseKey in groupedFieldSet.Keys) { var fields = groupedFieldSet[responseKey] as List <GraphQLParser.FieldContext>; var fieldName = fields.First().fieldName().GetText(); var field = objectType.GetField((x) => x.name == fieldName); if (field == null) { if (fieldName == "__schema") { IsSchemaQuery = true; field = schema.GetType("__SchemaContainer").GetField((x) => x.name == "__schema"); objectValue = schema; objectType = schema.GetType("__SchemaContainer"); } else { continue; } } var fieldType = field.type; if (objectType.kind == __TypeKind.UNION) { if (field.parentType.dotNetType != objectValue.GetType()) { continue; } ExecuteField(fields.First(), field.parentType, objectValue, fieldType, fields, variableValues, visitedFragments); } else { ExecuteField(fields.First(), objectType, objectValue, fieldType, fields, variableValues, visitedFragments); } } }
ModifiableSelectionSet MergeSelectionSets(List <GraphQLParser.FieldContext> fields) { if (fields.Count() == 1 && fields.First().selectionSet() != null) { return(new ModifiableSelectionSet(fields.First().selectionSet())); } ModifiableSelectionSet ms = new ModifiableSelectionSet(); foreach (var c in fields) { if (c.selectionSet() != null) { foreach (var g in c.selectionSet().selection()) { ms.selections.Add(g); } } } return(ms); }
public OrderedDictionary <string, List <GraphQLParser.FieldContext> > CollectFields(__Type objectType, ModifiableSelectionSet ss, Dictionary <string, Object> variableValues, Dictionary <string, GraphQLParser.FragmentDefinitionContext> visitedFragments) { var groupedFields = new OrderedDictionary <string, List <GraphQLParser.FieldContext> >(); foreach (var s in ss.selections) { if (s.field() != null) { if (skipDirective(s.field())) { continue; } if (!includeDirective(s.field())) { continue; } var responseKey = s.field().fieldName().GetText(); if (groupedFields.ContainsKey(responseKey) == false) { groupedFields[responseKey] = new List <GraphQLParser.FieldContext>(); } groupedFields[responseKey].Add(s.field()); } else if (s.fragmentSpread() != null) { var fragmentSpreadName = s.fragmentSpread().fragmentName().GetText(); if (visitedFragments.ContainsKey(fragmentSpreadName)) { continue; } var fragment = GetFragments(x => x.fragmentName().GetText() == fragmentSpreadName).FirstOrDefault(); if (fragment == null) { continue; } var fragmentTypeName = fragment.typeCondition().typeName().GetText(); var fragmentType = schema.GetType(fragmentTypeName); if (!DoesFragmentTypeApply(objectType, fragmentType, fragment)) { continue; } var fragmentSelectionSet = new ModifiableSelectionSet(fragment.selectionSet()); var fragmentGroupedFieldSet = CollectFields(objectType, fragmentSelectionSet, variableValues, visitedFragments); foreach (string responseKey in fragmentGroupedFieldSet.Keys) { if (groupedFields.ContainsKey(responseKey) == false) { groupedFields[responseKey] = new List <GraphQLParser.FieldContext>(); } groupedFields[responseKey].AddRange(fragmentGroupedFieldSet[responseKey]); } } else if (s.inlineFragment() != null) { var fragmentType = schema.GetType(s.inlineFragment().typeCondition().GetText()); if (fragmentType != null && !DoesFragmentTypeApply(objectType, fragmentType, s.inlineFragment())) { continue; } var fragmentSelectionSet = new ModifiableSelectionSet(s.inlineFragment().selectionSet()); var fragmentGroupedFieldSet = CollectFields(objectType, fragmentSelectionSet, variableValues, visitedFragments); foreach (string responseKey in fragmentGroupedFieldSet.Keys) { if (groupedFields.ContainsKey(responseKey) == false) { groupedFields[responseKey] = new List <GraphQLParser.FieldContext>(); } groupedFields[responseKey].AddRange(fragmentGroupedFieldSet[responseKey]); } } } return(groupedFields); }