apollo-link-state icon indicating copy to clipboard operation
apollo-link-state copied to clipboard

Nesting resolvers for client state data

Open vegetablesalad opened this issue 7 years ago • 6 comments

How can I add local resolvers to fetch data from client state if scheme is nested ?

schema {
  query: RootQuery
  mutation: RootMutation
}

type RootQuery {
  profile: Profile
}

type Profile {
  guid: String!
  user(idProviderName: String): User
  users: [User]
}

I'm trying this to define resolver, but with no luck:

const stateLink = withClientState({
            cache,
            resolvers: {
                Queries: {
                    Profile:{
                        user:(_:any, data:any, { cache }:any):User=>{
                            return null;
                        }
                    }
                }
            }
        });

What is the correct way of doing this?

vegetablesalad avatar May 28 '18 11:05 vegetablesalad

It should be resolvers: { Query: ... } instead of resolvers: { Queries: ... }

jsslai avatar May 29 '18 06:05 jsslai

So that would look like this?

const stateLink = withClientState({
            cache,
            resolvers: {
                Mutation:{
                    ...
                },
                Query:{
                    Profile:{
                        user: ():any => {
                            ...
                        }
                    }
                }
            },
              defaults: {
                profile:{
                    __typename: "Profile",
                    user:{
                        __typename: "User",
                        guid:"0"
                    }
                }
            },
        });

I get result from defaults, but the user() function is never called

vegetablesalad avatar May 29 '18 07:05 vegetablesalad

Any chance you can provide a small runnable reproduction that shows what you're trying and isn't working? There are a few important details missing here that will help with troubleshooting (like what the query you're trying looks like, etc.). Thanks!

hwillson avatar Jun 20 '18 16:06 hwillson

I’m having the same issue. When working with Apollo Server graphql-rolls is used to make an executable Schema which allows you to define resolvers for each Type in the Schema so nested fields that resolve to another Type just pass their data down to the resolver for said type.

It seems as though link-state only is aware of the top-level resolvers and doesn’t have an easy way to define a resolver for a Type that is nested in the Schema.

It would be sweet if link-state made use of graphql-tools allowing for nested Type resolvers to easily be defined.

Perhaps there’s already a way to do this that I’m just missing through?

jasonbahl avatar Jun 30 '18 13:06 jasonbahl

Any updates?

fletcherist avatar Sep 12 '18 15:09 fletcherist

Any updates on this?

anwilson514 avatar Feb 07 '19 15:02 anwilson514