public void AddQueryConnectionField <TSource, TReturn>(
            ObjectGraphType <TSource> graph,
            string name,
            Func <ResolveEfFieldContext <TDbContext, TSource>, IQueryable <TReturn> > resolve,
            Type?itemGraphType = null,
            IEnumerable <QueryArgument>?arguments = null,
            int pageSize = 10)
            where TReturn : class
        {
            Guard.AgainstNull(nameof(graph), graph);
            Guard.AgainstNull(nameof(resolve), resolve);

            var connection = BuildQueryConnectionField(name, resolve, pageSize, itemGraphType);

            var field = graph.AddField(connection.FieldType);

            field.AddWhereArgument(arguments);
        }
Exemplo n.º 2
0
        public void AddQueryConnectionField <TSource, TReturn>(
            ObjectGraphType <TSource> graph,
            string name,
            Func <ResolveEfFieldContext <TDbContext, TSource>, IQueryable <TReturn> > resolve,
            Type?graphType = null,
            IEnumerable <QueryArgument>?arguments = null,
            int pageSize = 10)
            where TReturn : class
        {
            Guard.AgainstNull(nameof(graph), graph);
            //build the connection field
            var connection = BuildQueryConnectionField(name, resolve, pageSize, graphType);
            //add the field to the graph
            var field = graph.AddField(connection.FieldType);

            //append the optional where arguments to the field
            field.AddWhereArgument(arguments);
        }
Exemplo n.º 3
0
        public FieldType AddNavigationListField <TSource, TReturn>(
            ObjectGraphType <TSource> graph,
            string name,
            Func <ResolveEfFieldContext <TDbContext, TSource>, IEnumerable <TReturn> > resolve,
            Type graphType = null,
            IEnumerable <QueryArgument> arguments = null,
            IEnumerable <string> includeNames     = null)
            where TReturn : class
        {
            Guard.AgainstNull(nameof(graph), graph);
            //graphType should represent the graph type of the enumerated value, not the list graph type

            //build the navigation field
            var field = BuildNavigationField(graphType, name, resolve, includeNames, arguments);

            //add it to the graph
            return(graph.AddField(field));
        }
        public void AddFields(ObjectGraphType objectGraphType)
        {
            objectGraphType.AddField(new EventStreamFieldType
            {
                Name      = "terminalEquipmentConnectivityUpdated",
                Type      = typeof(TerminalEquipmentAZConnectivityViewModelType),
                Resolver  = new FuncFieldResolver <TerminalEquipmentAZConnectivityViewModel>(context => context.Source as TerminalEquipmentAZConnectivityViewModel),
                Arguments = new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                    Name = "routeNodeId"
                },
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                    Name = "terminalEquipmentOrRackId"
                }
                    ),
                Subscriber = new EventStreamResolver <TerminalEquipmentAZConnectivityViewModel>(context =>
                {
                    var messageHandlingContext = (MessageHandlingContext)context.UserContext;
                    var graphQLUserContext     = messageHandlingContext.Get <GraphQLUserContext>("GraphQLUserContext");

                    if (_authSetting.Enable && !graphQLUserContext.User.Identity.IsAuthenticated)
                    {
                        context.Errors.Add(new ExecutionError("Not authorized"));
                        return(null);
                    }

                    var routeNodeId = context.GetArgument <Guid>("routeNodeId");
                    if (routeNodeId == Guid.Empty)
                    {
                        context.Errors.Add(new ExecutionError($"nameof(routeNodeId) guid cannot be empty."));
                        return(null);
                    }

                    var terminalEquipmentOrRackId = context.GetArgument <Guid>("terminalEquipmentOrRackId");
                    if (terminalEquipmentOrRackId == Guid.Empty)
                    {
                        context.Errors.Add(new ExecutionError($"{nameof(terminalEquipmentOrRackId)} guid cannot be empty."));
                        return(null);
                    }

                    return(_terminalEquipmentConnectivityObserver.WhenViewNeedsUpdate(routeNodeId, terminalEquipmentOrRackId));
                }),
            });
        }
Exemplo n.º 5
0
        public void prints_enum_default_args()
        {
            var root = new ObjectGraphType {
                Name = "Query"
            };

            var f = new FieldType
            {
                Name      = "bestColor",
                Arguments = new QueryArguments(new QueryArgument <RgbEnum>
                {
                    Name         = "color",
                    DefaultValue = "RED"
                }),
                Type = typeof(RgbEnum)
            };

            root.AddField(f);
            var schema = new Schema {
                Query = root
            };

            schema.RegisterType <RgbEnum>();
            var expected = new Dictionary <string, string>
            {
                {
                    "Query",
                    @"type Query {
  bestColor(color: RGB = RED): RGB
}"
                },
                {
                    "RGB",
                    @"enum RGB {
  RED
  GREEN
  BLUE
}"
                },
            };

            AssertEqual(print(schema), expected);
        }
Exemplo n.º 6
0
        public DataGraphType(Builder builder, SchemaInfo schemaInfo)
        {
            Name = schemaInfo.DataType;

            foreach (var fieldInfo in schemaInfo.Fields)
            {
                var(resolvedType, resolver, args) = builder.GetGraphType(fieldInfo);

                if (resolver != null)
                {
                    var fieldGraphType = new ObjectGraphType
                    {
                        Name = fieldInfo.LocalizedType
                    };

                    var partitioning = builder.ResolvePartition(((RootField)fieldInfo.Field).Partitioning);

                    foreach (var partitionKey in partitioning.AllKeys)
                    {
                        fieldGraphType.AddField(new FieldType
                        {
                            Name         = partitionKey.EscapePartition(),
                            Arguments    = args,
                            ResolvedType = resolvedType,
                            Resolver     = resolver,
                            Description  = fieldInfo.Field.RawProperties.Hints
                        }).WithSourceName(partitionKey);
                    }

                    fieldGraphType.Description = $"The structure of the {fieldInfo.DisplayName} field of the {schemaInfo.DisplayName} content type.";

                    AddField(new FieldType
                    {
                        Name         = fieldInfo.FieldName,
                        ResolvedType = fieldGraphType,
                        Resolver     = ContentResolvers.Field
                    }).WithSourceName(fieldInfo);
                }
            }

            Description = $"The structure of the {schemaInfo.DisplayName} data type.";
        }
Exemplo n.º 7
0
        public void test_outputs()
        {
            var innerObject = new ObjectGraphType();

            innerObject.AddField(new FieldType {
                Name = "test", ResolvedType = new StringGraphType(), Type = typeof(StringGraphType)
            });
            var list = new ListGraphType(innerObject);
            var obj  = new ObjectGraphType();

            obj.AddField(new FieldType {
                Name = "list", ResolvedType = list, Type = list.GetType()
            });
            var schema = new Schema
            {
                Query = obj
            };

            schema.Initialize();
        }
Exemplo n.º 8
0
 public void Activate(ObjectGraphType objectGraph, IWebHostEnvironment env, IServiceProvider sp)
 {
     objectGraph.AddField(new EventStreamFieldType
     {
         Name      = "cityAdded",
         Type      = typeof(CityAddedMessageGType),
         Resolver  = new FuncFieldResolver <CityAddedMessage>(context => context.Source as CityAddedMessage),
         Arguments = new QueryArguments(
             new QueryArgument <NonNullGraphType <StringGraphType> > {
             Name = "countryName"
         }
             ),
         Subscriber = new EventStreamResolver <CityAddedMessage>(context =>
         {
             var subscriptionServices = (ISubscriptionServices)sp.GetService(typeof(ISubscriptionServices));
             var countryName          = context.GetArgument <string>("countryName");
             return(subscriptionServices.CityAddedService.GetMessages(countryName));
         })
     });
 }
Exemplo n.º 9
0
        public void AddQueryConnectionField <TReturn>(
            ObjectGraphType graph,
            string name,
            Func <ResolveEfFieldContext <TDbContext, object>, IQueryable <TReturn> > resolve,
            Type graphType = null,
            IEnumerable <QueryArgument> arguments = null,
            int pageSize = 10)
            where TReturn : class
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            //build the connection field
            var connection = BuildQueryConnectionField(name, resolve, pageSize, graphType);
            //add the field to the graph
            var field = graph.AddField(connection.FieldType);

            //append the optional where arguments to the field
            field.AddWhereArgument(arguments);
        }
Exemplo n.º 10
0
        private ObjectGraphType CreateQuery()
        {
            var entityFields = new List <FieldType>(CreateEntityFields(_edmModel, _graphTypeBuilder));

            foreach (IEdmModel refModel in _edmModel.ReferencedModels)
            {
                if (refModel.EntityContainer != null)
                {
                    entityFields.AddRange(CreateEntityFields(refModel, _graphTypeBuilder));
                }
            }

            var query = new ObjectGraphType();

            foreach (FieldType entityField in entityFields)
            {
                _graphTypeBuilder.AddNavigationProperties(entityField);
                query.AddField(entityField);
            }
            return(query);
        }
Exemplo n.º 11
0
    public void can_have_unknown_input_types_mapped_to_auto_registering_graph()
    {
        var schema = new CustomTypesSchema();
        var query  = new ObjectGraphType();
        var field  = new FieldType()
        {
            Name      = "test",
            Type      = typeof(IntGraphType),
            Arguments = new QueryArguments
            {
                new QueryArgument(typeof(GraphQLClrInputTypeReference <CustomData>))
                {
                    Name = "arg"
                }
            }
        };

        query.AddField(field);
        schema.Query = query;
        schema.Initialize();
        schema.Query.Fields.Find("test").Arguments[0].ResolvedType.ShouldBeOfType <AutoRegisteringInputObjectGraphType <CustomData> >();
    }
        public SchemaWithDuplicateInterfaceFields()
        {
            Query = new ObjectGraphType {
                Name = "Query"
            };

            var iface = new InterfaceGraphType {
                Name = "Dup", ResolveType = _ => null
            };

            iface.AddField(new FieldType {
                Name = "field", ResolvedType = new StringGraphType()
            });
            iface.AddField(new FieldType {
                Name = "field_2", ResolvedType = new StringGraphType()
            });                                                                                       // bypass HasField check
            iface.Fields.List[1].Name = "field";

            Query.AddField(new FieldType {
                Name = "field", ResolvedType = new StringGraphType()
            });
            RegisterType(iface);
            Query.ResolvedInterfaces.Add(iface);
        }
Exemplo n.º 13
0
        private IGraphTypeOfGraphQLNet Convert(IGraphType graphType)
        {
            try
            {
                //Enumerable
                if (graphType.IsEnumerable)
                {
                    var elementGraphType = _graphTypeProvider.GetGraphType(graphType.Type, false, false);
                    var listGraphType    = new ListGraphType(Convert(elementGraphType));
                    return(graphType.IsRequired
                        ? new NonNullGraphType(listGraphType)
                        : (IGraphTypeOfGraphQLNet)listGraphType);
                }

                //Scalar
                if (!graphType.Fields.Any())
                {
                    if (graphType.IsEnum)
                    {
                        var enumGraphType = typeof(EnumerationGraphType <>).MakeGenericType(graphType.Type);
                        enumGraphType = graphType.IsRequired
                            ? typeof(NonNullGraphType <>).MakeGenericType(enumGraphType)
                            : enumGraphType;
                        return((IGraphTypeOfGraphQLNet)Activator.CreateInstance(enumGraphType));
                    }

                    if (graphType.Type == typeof(string))
                    {
                        var stringType = new StringGraphType();
                        return(graphType.IsRequired
                            ? (IGraphTypeOfGraphQLNet) new NonNullGraphType(stringType)
                            : stringType);
                    }
                    var scalarType = graphType.Type.IsGenericType && graphType.Type.GetGenericTypeDefinition() == typeof(Nullable <>)
                       ? GraphTypeTypeRegistry.Get(graphType.Type.GenericTypeArguments[0])
                       : GraphTypeTypeRegistry.Get(graphType.Type);

                    if (null != scalarType)
                    {
                        var resolvedGraphType = (IGraphTypeOfGraphQLNet)Activator.CreateInstance(scalarType);
                        return(graphType.IsRequired
                            ? new NonNullGraphType(resolvedGraphType)
                            : resolvedGraphType);
                    }
                    throw new GraphException($"Unknown GraphType '{graphType.Name}'");
                }

                //Complex
                var objectGraphType = new ObjectGraphType {
                    Name = graphType.Name
                };
                foreach (var field in graphType.Fields.Values)
                {
                    var fieldType = new FieldType
                    {
                        Name         = field.Name,
                        ResolvedType = Convert(field.GraphType)
                    };
                    if (field.Arguments.Any())
                    {
                        fieldType.Arguments = new QueryArguments();
                    }
                    foreach (var argument in field.Arguments.Values)
                    {
                        var queryArgument = new QueryArgument(Convert(argument.GraphType))
                        {
                            Name = argument.Name
                        };
                        fieldType.Arguments.Add(queryArgument);
                    }
                    objectGraphType.AddField(fieldType);
                }

                return(graphType.IsRequired
                    ? (IGraphTypeOfGraphQLNet) new NonNullGraphType(objectGraphType)
                    : objectGraphType);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Exemplo n.º 14
0
        public DataGraphType(Builder builder, SchemaInfo schemaInfo)
        {
            // The name is used for equal comparison. Therefore it is important to treat it as readonly.
            Name = schemaInfo.DataType;

            foreach (var fieldInfo in schemaInfo.Fields)
            {
                var partitioning = builder.ResolvePartition(((RootField)fieldInfo.Field).Partitioning);

                if (fieldInfo.Field.IsComponentLike())
                {
                    var fieldGraphType = new ObjectGraphType
                    {
                        // The name is used for equal comparison. Therefore it is important to treat it as readonly.
                        Name = fieldInfo.LocalizedType
                    };

                    foreach (var partitionKey in partitioning.AllKeys)
                    {
                        fieldGraphType.AddField(new FieldType
                        {
                            Name         = partitionKey.EscapePartition(),
                            Arguments    = ContentActions.Json.Arguments,
                            ResolvedType = AllTypes.Json,
                            Resolver     = FieldVisitor.JsonPath,
                            Description  = fieldInfo.Field.RawProperties.Hints
                        }).WithSourceName(partitionKey);
                    }

                    fieldGraphType.Description = $"The dynamic structure of the {fieldInfo.DisplayName} field of the {schemaInfo.DisplayName} content type.";

                    AddField(new FieldType
                    {
                        Name         = fieldInfo.FieldNameDynamic,
                        ResolvedType = fieldGraphType,
                        Resolver     = ContentResolvers.Field
                    }).WithSourceName(fieldInfo);
                }

                var(resolvedType, resolver, args) = builder.GetGraphType(fieldInfo);

                if (resolver != null)
                {
                    var fieldGraphType = new ObjectGraphType
                    {
                        // The name is used for equal comparison. Therefore it is important to treat it as readonly.
                        Name = fieldInfo.LocalizedType
                    };

                    foreach (var partitionKey in partitioning.AllKeys)
                    {
                        fieldGraphType.AddField(new FieldType
                        {
                            Name         = partitionKey.EscapePartition(),
                            Arguments    = args,
                            ResolvedType = resolvedType,
                            Resolver     = resolver,
                            Description  = fieldInfo.Field.RawProperties.Hints
                        }).WithSourceName(partitionKey);
                    }

                    fieldGraphType.Description = $"The structure of the {fieldInfo.DisplayName} field of the {schemaInfo.DisplayName} content type.";

                    AddField(new FieldType
                    {
                        Name         = fieldInfo.FieldName,
                        ResolvedType = fieldGraphType,
                        Resolver     = ContentResolvers.Field
                    }).WithSourceName(fieldInfo);
                }
            }

            Description = $"The structure of the {schemaInfo.DisplayName} data type.";
        }
Exemplo n.º 15
0
        public void Initialize(IGraphModel model, ISchemaEntity schema)
        {
            var schemaType = schema.TypeName();
            var schemaName = schema.DisplayName();

            Name = $"{schemaType}DataDto";

            foreach (var field in schema.SchemaDef.Fields.ForApi())
            {
                var(resolvedType, valueResolver) = model.GetGraphType(schema, field);

                if (valueResolver != null)
                {
                    var fieldType = field.TypeName();
                    var fieldName = field.DisplayName();

                    var fieldGraphType = new ObjectGraphType
                    {
                        Name = $"{schemaType}Data{fieldType}Dto"
                    };

                    var partition = model.ResolvePartition(field.Partitioning);

                    foreach (var partitionItem in partition)
                    {
                        var key = partitionItem.Key;

                        var partitionResolver = new FuncFieldResolver <object>(c =>
                        {
                            if (((ContentFieldData)c.Source).TryGetValue(key, out var value))
                            {
                                return(valueResolver(value, c));
                            }
                            else
                            {
                                return(null);
                            }
                        });

                        fieldGraphType.AddField(new FieldType
                        {
                            Name         = key.EscapePartition(),
                            Resolver     = partitionResolver,
                            ResolvedType = resolvedType,
                            Description  = field.RawProperties.Hints
                        });
                    }

                    fieldGraphType.Description = $"The structure of the {fieldName} field of the {schemaName} content type.";

                    var fieldResolver = new FuncFieldResolver <NamedContentData, IReadOnlyDictionary <string, IJsonValue> >(c =>
                    {
                        return(c.Source.GetOrDefault(field.Name));
                    });

                    AddField(new FieldType
                    {
                        Name         = field.Name.ToCamelCase(),
                        Resolver     = fieldResolver,
                        ResolvedType = fieldGraphType,
                        Description  = $"The {fieldName} field."
                    });
                }
            }

            Description = $"The structure of the {schemaName} content type.";
        }