Last active
March 6, 2019 08:11
-
-
Save ebicoglu/b396d685bd9d6a2eb00fc53b2e99b275 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I made a super cool extension method that returns whether the selection is included in a GraphQL query. | |
--- | |
## Extension Method | |
using System; | |
using System.Linq; | |
using GraphQL.Language.AST; | |
using GraphQL.Types; | |
public static class ContextExtensions | |
{ | |
/// <summary> | |
/// Returns true if the given fieldSelector exists in the selection of the query. | |
/// </summary> | |
/// <param name="context">The working context</param> | |
/// <param name="fieldSelector">The query of the field selector. For example items:organizationUnits:displayName</param> | |
/// <param name="namespaceSeperator">The seperator character of the fieldSelector. Default is :</param> | |
/// <returns></returns> | |
public static bool HasSelectionField(this ResolveFieldContext<object> context, string fieldSelector, char namespaceSeperator = ':') | |
{ | |
if (string.IsNullOrWhiteSpace(fieldSelector)) | |
{ | |
return false; | |
} | |
if (context.SubFields == null) | |
{ | |
return false; | |
} | |
var fragments = fieldSelector.Split(new[] { namespaceSeperator }, StringSplitOptions.RemoveEmptyEntries); | |
if (fragments.Length == 1) | |
{ | |
return context.SubFields.ContainsKey(fragments[0]); | |
} | |
if (context.SubFields[fragments[0]] == null) | |
{ | |
return false; | |
} | |
if (context.SubFields[fragments[0]].SelectionSet == null) | |
{ | |
return false; | |
} | |
if (context.SubFields[fragments[0]].SelectionSet.Selections == null) | |
{ | |
return false; | |
} | |
var selections = context.SubFields[fragments[0]].SelectionSet.Selections; | |
for (var i = 1; i < fragments.Length; i++) | |
{ | |
if (selections == null) | |
{ | |
return false; | |
} | |
var field = selections.Select(selection => (Field)selection).FirstOrDefault(f => f.Name == fragments[i]); | |
if (field == null) | |
{ | |
return false; | |
} | |
if (i == fragments.Length - 1) | |
{ | |
return true; | |
} | |
selections = field.SelectionSet?.Selections; | |
} | |
return true; | |
} | |
} | |
--- | |
## Usage | |
protected override async Task<PagedResultDto<UserDto>> Resolve(ResolveFieldContext<object> context) | |
{ | |
var total_count_exists = context.HasSelectionField("totalCount"); //true | |
var items_name_exists = context.HasSelectionField("items:name"); //true | |
var items_roles_name_exists = context.HasSelectionField("items:roles:name"); //true | |
var items_organizationUnits_displayName_exists = context.HasSelectionField("items:organizationUnits:displayName"); //true | |
var items_organizationUnits_xyz_exists = context.HasSelectionField("items:organizationUnits:xyz"); //false | |
} | |
--- | |
## Sample Query | |
query MyQuery { | |
users(id: 1) { | |
totalCount | |
items { | |
name | |
surname | |
roles { | |
id | |
name | |
displayName | |
} | |
organizationUnits { | |
id | |
code | |
displayName | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment