GithubHelp home page GithubHelp logo

lampidudelj / hotchocolate.apollofederationextension Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 47 KB

An extension adding GraphQL federation support to HotChocolate framework based on Apollo Federation Spec

License: MIT License

C# 100.00%
graphql hotchocolate federation federated-graphql apollo-federation graphql-dotnet dotnet dotnet-core graphql-server graphql-schema

hotchocolate.apollofederationextension's Introduction

HotChocolate Apollo Federation Extension

CI

An extension adding GraphQL federation support to HotChocolate framework based on Apollo Federation Spec

Getting Started

Add the federated extension to the service configuration by adding the following code to the ConfigureServices method in the Startup.cs.

   services
       .AddGraphQLServer()
       .AddFederationExtensions()
       .AddQueryType<Query>()

   services.AddHttpContextAccessor();

This will extend your server and schema with Fetch Service capability and custom federation scalars, unions and directives

Using annotation to extend types

The extension provides both Object and Field level directives to generate federation spec compliant schema For example, following class will generate:

    [FederationObjectDirective(Name = "key", Fields = "id")]
    [FederationObjectDirective(Name = "extends")]
    public record Hotel([property: FederationFieldDirective(Name = "external")]
                        [property: GraphQLType(typeof(NonNullType<IdType>))] 
                        int Id, 
                        Destination Destination) : IEntityUnionType
    {
        [GraphQLIgnore]
        public IEntityUnionType ResolveReference(KeyValuePair<string, object> primaryKey)
        {
            if (primaryKey.Key == "id")
            {
                return GetById((int)primaryKey.Value);
            }

            return null;
        }
    }

following schema

    type Hotel @key(fields: "id") @extends {
        id: ID! @external
    }

    union _Entity = Hotel

Key points

  • any type annotated with the @key directive, including both types native to the schema and extended types, must implement IEntityUnionType interface as it serves as a representation of the federation UnionEntity
  • public IEntityUnionType ResolveReference(KeyValuePair<string, object> primaryKey) method is a resolver for a query across service boundaries. It should return an entity based on the key specified in the schema and provided as an input parameter. More on how this works
  • [GraphQLIgnore] attribute must annotate ResolveReference method. HotChocolate's schema builder engine can not serialize KeyValuePair into a GraphQL scalar and will crash trying.

TODO

  • implement @provides and @requires directives.
  • validation on _Any and _FieldSet scalars
  • better solution to getting schema for _service resolver. Cache?
  • input type for KeyValuePair to get around having to use [GraphQLIgnore] attribute_

hotchocolate.apollofederationextension's People

Contributors

lampidudelj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

aanodin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.