Giter Site home page Giter Site logo

mmgerald / relay Goto Github PK

View Code? Open in Web Editor NEW

This project forked from graphql-dotnet/relay

0.0 0.0 0.0 282 KB

A toolset for creating Relay.js compatible GraphQL servers in dotnet.

License: MIT License

HTML 20.97% C# 55.14% JavaScript 15.13% CSS 4.55% TypeScript 4.21%

relay's Introduction

GraphQL.Relay

A collection of classes, tools, and utilities for creating Relay.js compatible GraphQL servers in dotnet.

Usage

Setup

Add the Nuget package:

$> dotnet add package GraphQL.Relay

Ensure your resolver for GraphQL can resolve:

  • ConnectionType<>
  • EdgeType<>
  • NodeInterface
  • PageInfoType

If you're using the resolver from MVC (IServiceProvider), that might look like this:

services.AddTransient(typeof(ConnectionType<>));
services.AddTransient(typeof(EdgeType<>));
services.AddTransient<NodeInterface>();
services.AddTransient<PageInfoType>();

GraphTypes

Included are a few GraphQL types and helpers for creating Connections, Mutations, and Nodes

QueryGraphType

A top level Schema query type, which defines the required node root query;

public class StarWarsQuery : QueryGraphType
{
  public StarWarsQuery() {
    Field<DroidType>(
      "hero",
      resolve: context => new Droid { DroidId = "1", Name = "R2-D2" }
    );
  }
}

public class StarWarsSchema : Schema
{
  public StarWarsSchema() {
    Query = new StarWarsQuery();
  }
}

NodeGraphType<TSource, TOut>, NodeGraphType<TSource>, NodeGraphType

NodeTypes, are ObjectGraphTypes that implement the NodeInterface and provide a GetById method, which allows Relay (via the node() Query) to refetch nodes when it needs to.

Nodes, also provide a convenient Id() method for defining global id fields, derived from the type Name and Id. If your underlying type has a name conflict on the field id, the "local" Id will be prefixed with the type name, e.g. Droid.Id -> droidId

public class Droid
{
  public string DroidId { get; set; }
  public string Name { get; set; }
}

public class DroidType : NodeGraphType<Droid>
{
  public DroidType()
  {
    Name = "Droid";

    Id(d => d.DroidId); // adds an id Field as well as the `Droid` id field

    Field<StringGraphType>("name", "The name of the droid.");
  }

  public override Droid GetById(string droidId) {
    return StarWarsData.GetDroidByIdAsync(droidId);
  }
}

Mutations

Relay mutations specify a few constraints on top of the general GraphQL mutations. To accompodate this, there are mutation specific GraphTypes provided.

MutationGraphType

The top level mutation type attached to the GraphQL Schema

public class StarWarsMutation : MutationGraphType
{
  public StarWarsMutation() {

    Mutation<CreateDroidInput, CreateDroidPayload>("createDroid");
    Mutation<DeleteDroidInput, DeleteDroidPayload>("deleteDroid");
  }
}

public class StarWarsSchema : Schema
{
  public StarWarsSchema() {
    Query = new StarWarsQuery();
    Mutation = new StarWarsMutation();
  }
}

MutationInputGraphType

An simple base class that defines a clientMutationId field. functionally identical to InputObjectGraphType otherwise

MutationPayloadGraphType<TSource, TOut>, MutationPayloadGraphType<TSource>, MutationPayloadGraphType

The output ObjectGraphType containing the mutation payload, functionally similar to an ObjectGraphType with the addition of requiing a MutateAndGetPayload() method used to resolve the payload from the inputs.

public class CreateDroidPayload : MutationPayloadGraphType<DroidPayload, Task<DroidPayload>>
{
  public CreateDroidPayload()
  {
    Name = "CreateDroidPayload";

    Field(
      name: "droid",
      type: typeof(Droid));
  }

  public override async Task<DroidPayload> MutateAndGetPayload(MutationInputs inputs)
  {
    string name = inputs.Get<string>("name");

    Droid newDroid = await StarWarsData.CreateDroidAsync(name)

    return new DroidPayload {
      Droid = newDroid
    };
  }
}

Connections

Luckily GraphQL-dotnet already provides helpful utilities for creating connection fields, on GraphTypes. In addition included here are a few more helpful methods for creating relay compatible Connections from IEnumerables.

Connection.ToConnection(IEnumerable items, ResolveConnectionContext context)

Creates a connection from an existing list of objects.

public class Droid
{
  public string DroidId { get; set; }
  public string Name { get; set; }
  public IEnumerable<Droid> Friends { get; set; }
}

public class DroidType : ObjectGraphType<Droid>
{
  public DroidType()
  {
    Name = "Droid";

    Field<StringGraphType>("name", "The name of the droid.");

    Connection<DroidType>()
      .Name("friends")
      .Resolve(context =>
        Connection.ToConnection(c.Source.Friends, context));
  }
}

Connection.ToConnection(IEnumerable items, ResolveConnectionContext context, int sliceStartIndex, int totalCount)

Similar to the above, but creates a connection with the correct cursors, when you only have a slice of the entire set of items. Windowing the items based on the arguments.

Connection.CursorToOffset(string cursor)

Convert a connection item cursor to the int index of the item in the set.

Connection.OffsetToCursor(int offset)

Convert an index offset to a connection cursor.

relay's People

Contributors

aloker avatar jphenow avatar jquense avatar kiwijus avatar martindawson avatar maximeplante avatar mmgerald avatar

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.