question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apollo Client Field Resolution

See original GitHub issue

I am using Apollo Client 2.5 with local state and trying to return a class from the resolvers but it appears that Apollo Client doesn’t resolve the returned fields if they are methods/functions. I’ve demonstrated below what I’m talking about.

This same schema and resolver setup works fine in Apollo Server and I’m curious if this will be supported in Apollo Client local state? Or maybe it does work and there is something I’m doing wrong with the setup?

Here is a link to the same code in a Codesandbox https://codesandbox.io/s/n4rom4jkpp

This code should output {"user":{"name":"Tyler","nameLength":5,"__typename":"User"}} But instead it is outputing: {"user":{"name":"Tyler","__typename":"User"}}

You’ll see it is missing the nameLength field as that field is a method on the User class and not a static value.

import React from "react";
import ReactDOM from "react-dom";
import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { ApolloProvider, Query } from "react-apollo";
import gql from "graphql-tag";

function App() {
  const GET_USER = gql`
    query getUser {
      user @client {
        name
        nameLength
      }
    }
  `;

  const render = ({ loading, error, data }) => {
    if (loading) return <h4>Loading...</h4>;
    if (error) return <h4>Error...</h4>;
    return <div className="App">{JSON.stringify(data)}</div>;
  };

  return <Query query={GET_USER}>{render}</Query>;
}

class User {
  constructor({ name }) {
    this.__typename = "User";
    this.name = name;
  }

  nameLength() {
    return this.name.length;
  }
}

const client = new ApolloClient({
  cache: new InMemoryCache(),
  resolvers: {
    Query: {
      user: () => new User({ name: "Tyler" })
    }
  },
  typeDefs: gql`
    type Query {
      user: User
    }
    type User {
      name: String
      nameLength: Int
    }
  `
});

ReactDOM.render(
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>,
  document.getElementById("root")
);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tylerbucheacommented, Apr 29, 2019

Hey, benjamn thanks for the replies. I think I already ended up doing what you suggested in your last paragraph:

resolvers: {
  User: {
    nameLength: parent => parent.name.length
  },
  Query: {
    user: () => new User({ name: "Tyler" })
  }
},

You can try my original code with nameLength as a getter but that breaks it even more.

This is no longer an issue for me, but I guess my original point was it’s strange that my class resolver methods work in apollo-server but not with Apollo Local State. If that provides any value for you or the team then great if not then feel free to close the issue.

0reactions
hwillsoncommented, Aug 13, 2020

Sounds like this was resolved - closing, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Local-only fields in Apollo Client
A field policy specifies custom logic for how a single GraphQL field is fetched from and written to your Apollo Client cache. You...
Read more >
Error: While trying to resolve module @apollo/client React ...
Also I tried to specify in metro.config.js to resolve "cjs" type of file (@apollo/client/main.cjs), but nothing. Error error: ...
Read more >
Managing State with Field Policies – Angular
Your Apollo Client queries can include local-only fields that aren't defined in your GraphQL server's schema. The values for these fields ...
Read more >
Computed local-only fields in Apollo Client
One valuable feature of Apollo Client is local-only fields. These fields are redacted from an operation that is sent to the server by...
Read more >
@apollo/client | Yarn - Package Manager
Apollo Client is a fully-featured caching GraphQL client with integrations for React, Angular, and more. It allows you to easily build UI components...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found