Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link schema in "GraphQL for .NET" and "Relay"?

I want to build a web app with Javascript for the front end and C# for the back end and I want to determine the value of GraphQL.

  • For my C# back end I use a GraphQL implementation named GraphQL for .NET.
  • For my front end, I would like to use Relay as it plays well with ReactJS.

Now for my back end, I implemented a sample schema like in one of the examples which looks like this:

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

In my front end, I now need to tell Relay somehow about this schema. At least this is what I understood when walking through the tutorials, because for some reason the GraphQL queries needs to be transpiled. This is an example as how I would like to load all Droids:

class Content extends React.Component<ContentProps, { }> {
    ...
}

export default Relay.createContainer(Content, {
    fragments: {
        viewer: () => Relay.QL`
            fragment on User {
                query HeroNameQuery {
                    droids {
                        id
                        name
                    }
                }
            }
        `,
    }
});

In one of the examples for Relay, I have seen that the babel-relay-plugin is used for conversion. It gets a schema file (JSON). The Getting Started Guide of Relay shows, how to create such a schema with graphql-js and graphql-relay-js.

Now my questions:

  1. Do I really need to create schemas on the front and on the back end?
  2. What is the point of teaching Relay my schema, as the back end already uses the schema to return the well formed data?
  3. What is the benefit at all from using Relay in this scenario? What would I lose when I would just access the backend via a regular REST endpoint along with a GraphQL query as a parameter?
like image 561
Michael Hilus Avatar asked Jun 26 '16 22:06

Michael Hilus


People also ask

Should I use Relay for GraphQL?

Relay is recommended to use in the frontend to have more structured, modular, future-proofed applications that can scale easily to millions of users. The first thing that needs to be implemented in order to use Relay is to make a Relay-compatible GraphQL server.

How do I fetch a schema in GraphQL?

How To Get The Schema — Introspection Queries. Some GraphQL servers don't provide a convenient GraphQL API explorer. Instead, to get the schema we need to send a HTTP request to the GraphQL server endpoint asking for the GraphQL schema. This type of HTTP request is called a GraphQL introspection query.

What is Relay in GraphQL?

Relay is a JavaScript framework for fetching and managing GraphQL data in React applications that emphasizes maintainability, type safety and runtime performance. Relay achieves this by combining declarative data fetching and a static build step.


1 Answers

  1. You need schema only on the backend and you can download schema.json from your backend using example at the bottom part of this document: https://facebook.github.io/relay/docs/guides-babel-plugin.html
  2. Relay need schema to correctly construct queries and understand types of returned data.
  3. Relay wraps your React components and fetch/provide all necessary data for rendering. Have many features out of the box like data caching, query consolidation, so with Relay you don't need to fetch anything and worry about how data provided to your component, you just need to write queries and components.
like image 171
dangh Avatar answered Oct 30 '22 01:10

dangh