The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Use Relay’s Input Object Mutations

Use Relay’s Input Object Mutations

4:59
In order to support mutations in Relay, there is a requirement that the GraphQL Server exposes mutation fields in a standardized way. This standard includes a way for mutations to accept and emit an identifier string, allowing Relay to track mutations and responses. In this video, we’ll learn how to use a helper available to us through `graphql-relay` to create Mutation fields that accept `clientMutationId`’s.
Watch this lesson now
Avatar
egghead.io

In order to support mutations in Relay, there is a requirement that the GraphQL Server exposes mutation fields in a standardized way. This standard includes a way for mutations to accept and emit an identifier string, allowing Relay to track mutations and responses. In this video, we’ll learn how to use a helper available to us through graphql-relay to create Mutation fields that accept clientMutationId’s.

Avatar
Austin Witherow

Just finished this course. Thank you very much for the overview of GraphQL and Relay :)

A few things that I am wondering now that I am done...

1) What sort of "folder structure" is effective for GraphQL/Relay architecture?
2) Can I simply start using graphql-relay to begin rendering my data into a React/RN application?
3) Is there a recommended method to test my relay architecture? It seems like the foundation of using schemas really does this to begin with, but you know how engineers like to make sure there are no possible leaks :)

Avatar
Josh Black

Hi Austin! So glad to hear that you finished! I hope it was useful.

To answer some of your questions:

What sort of "folder structure" is effective for GraphQL/Relay architecture?

The best part of this is that your folder structure can vary depending on your needs, and if you're adding to an existing Node.js-based server, or starting from fresh. The pattern that I typically use is as follows:

src
├── index.js
├── loaders
│   ├── company.js
│   ├── employees.js
│   ├── index.js
└── schema
    ├── index.js
    ├── middleware
    │   ├── compose.js
    │   ├── index.js
    │   ├── logger.js
    │   ├── profile.js
    │   └── utilities
    │       └── trace.js
    ├── types
    │   ├── company.js
    │   ├── competitors.js
    │   ├── employee.js
    │   ├── node.js

Where schema holds all the information for the GraphQL Schema, including types, mutations, subscriptions, fields, etc. If you're interested in monitoring/profiling your resolve statements, it's also helpful to implement some form of middleware pattern on your resolve statements, for actions like logging, profiling, permissions checking, etc.

In addition, I leverage a project called DataLoader from Facebook for defining how to actually resolve each field. Typically, I locate all of the loaders that I've defined then in their own folder.

Can I simply start using graphql-relay to begin rendering my data into a React/RN application?

On the server-side, that's definitely all you need! For your React applications, it's helpful to use the client version of Relay called react-relay which provides helpful utilities for defining the queries that each component needs satisfied in order to render.

Is there a recommended method to test my relay architecture?

That's a great question, especially when it comes to testing out mutations. Unfortunately, since GraphQL can sit in a variety of places in your stack it makes recommendations challenging.

After going through some projects now with GraphQL, integration tests can definitely be some of the more valuable tests that you can write to verify that your server is behaving the way that you expect it to. For example, when writing Mutations you might want to verify the behavior of your system when the client sends bad/no arguments, or what to do if internal errors occur.

Hope the above answers help, and thanks again for watching the course!

In reply to Austin Witherow
Avatar
EBIA

One of the best Egghead videos, great work, thank you. The clear pronunciation and the fast typing of repetitive source code make this video really appreciative (I hope, that's the correct translation of "genüsslich" from German).

Using GraphQL on production servers (security & co) would be a fantastic addition,..

Avatar
Josh Black

Hi EBIA! Just wanted to say thank you for watching, and thank you so much for the kind words! I'm glad it was helpful 😄

Definitely agree on the topic suggestion, we'll see what we can do!

In reply to EBIA
Avatar
mobility-team

Thank you so much for this great course! I've read a book on Relay and GraphQL, but this series of videos let you up and running for a day! I see you pass a clientMutationId in the input variable, but it isn't defined in the inputFields. What's the purpose of this one? I can execute the mutation without it... If I execute the mutation with the clientMutationId being equal to another node id, a node will be added and not updated. I don't get the purpose of this parameter.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?