Tutorial: Are functions necessary? Or could the same thing be accomplished with custom resolvers?

I am following along with the web tutorial for the task app.

I notice that some fetching and updating uses functions and React state instead of Apollo GraphQL, such as fetching a user’s projects, fetching a project’s contributors, and adding a contributor to a project.

I’d like to avoid this pattern and route all client-server interactions through GraphQL. Is this possible on the server side with custom resolvers? Was there a reason functions were chosen for the tutorial?

For example, say the user selects a project, I’d expect to be able to execute a cohesive query such as the following in order to get more details about a project:

project( id: $id ) {
  id
  name
  contributors {
    id
    name
  }
  tasks {
    id
    name
    ...
  }
}

I’m aware I could use local-only fields and a client-side schema and transform the results of the functions on the client. Though this adds unnecessary weight and complexity to the frontend, so I’d like to avoid it.

Hey Andrew,

Welcome to the forums! I wrote the web tutorial so hopefully I can clear things up for you. In general YES you can use GraphQL custom resolvers instead of direct function calls.

The primary reason we don’t currently do this is for interoperability between the various tutorial clients, e.g. the Android app doesn’t use GraphQL so we just call the functions directly. That said it should be pretty straightforward to create custom resolvers that wrap these functions. You can use function context to call the existing functions from your custom resolver functions.

This is a great idea - I’ll look into updating the web tutorial to do this since I think it makes sense for everyone to use it. In the meantime please let me know if you hit any trouble implementing the custom resolvers!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.