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

1

9

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?
Euthanasia answered 26/6, 2016 at 22:22 Comment(0)
I
7
  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.
Incubator answered 17/9, 2016 at 20:34 Comment(1)
Thanks for clarification.I meanwhile realized that the schema.json is needed in transpilation process when building the frontend code from GraphQL queries.Euthanasia

© 2022 - 2024 — McMap. All rights reserved.