Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Relay / GraphQL 'resolve' works?

I am trying out Relay and GraphQL. When I am doing the schema I am doing this:

let articleQLO = new GraphQLObjectType({
  name: 'Article',
  description: 'An article',
  fields: () => ({
    _id: globalIdField('Article'),
    title: {
      type: GraphQLString,
      description: 'The title of the article',
      resolve: (article) => article.getTitle(),
    },
    author: {
      type: userConnection,
      description: 'The author of the article',
      resolve: (article) => article.getAuthor(),
    },
  }),
  interfaces: [nodeInterface],
})

So, when I ask for an article like this:

{
  article(id: 1) {
    id,
    title,
    author
  }
}

Will it do 3 queries to the database? I mean, each field has a resolve method (getTitle, getAuthor, etc.) which does a request to the database. Am I doing this wrong?

This is an example of getAuthor (I use mongoose):

articleSchema.methods.getAuthor = function(id){
  let article = this.model('Article').findOne({_id: id})
  return article.author
}
like image 553
Facundo Matteo Avatar asked Aug 26 '15 12:08

Facundo Matteo


People also ask

How are GraphQL queries resolved?

Each field on each type is backed by a function called the resolver which is provided by the GraphQL server developer. When a field is executed, the corresponding resolver is called to produce the next value. If a field produces a scalar value like a string or number, then the execution completes.

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.

How does relay cache work?

Relay combines your React components with that's needed specifically for that component and renders it optimally to your frontend app. Now, the cache works by associating each piece of data with a unique ID.

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.


1 Answers

If the resolve method is passed the article, can't you just access the property?

let articleQLO = new GraphQLObjectType({
  name: 'Article',
  description: 'An article',
  fields: () => ({
    _id: globalIdField('Article'),
    title: {
      type: GraphQLString,
      description: 'The title of the article',
      resolve: (article) => article.title,
    },
    author: {
      type: userConnection,
      description: 'The author of the article',
      resolve: (article) => article.author,
    },
  }),
  interfaces: [nodeInterface],
})

Since Schema.methods in Mongoose defines methods on the model, it wouldn't take an ID for the article (because you call it on an article instance). So, if you wanted to keep the method, you would just do:

articleSchema.methods.getAuthor = function() {
  return article.author;
}

If it was something you need to look up e.g. in another collection, then you'd need to do a separate query (assuming you're not using refs):

articleSchema.methods.getAuthor = function(callback) {
  return this.model('Author').find({ _id: this.author_id }, cb);
}
like image 148
Michelle Tilley Avatar answered Sep 28 '22 18:09

Michelle Tilley