apollographql / apollo-feature-requests

🧑‍🚀 Apollo Client Feature Requests | (no 🐛 please).
Other
130 stars 7 forks source link

feat(local-state): support extending all GraphQL types (not only root ones) #90

Closed playerx closed 5 years ago

playerx commented 5 years ago

Problem

We need to extend GraphQL types on client side and resolve them, here is an example:

   typeDefs: gql`
      extend type Query {
         testData: String
      }
      extend type State {
         draft: String
      }
   `,
   resolvers: {
      Query: {
         testData: () => 'Hello X',
         State: {
            draft: ({id}) => {
               console.log('draft resolver')
               return {
                  __typename: 'State',
                  id,
                  draft: "client-side data",
               }
            },
         },
      },
   },

for this client schema if we will run query:

query Test {
  testData @client
  state(id: "cjqm6vtcz0pb40881ofh9eeg0") {
    id
    name
    draft @client
  }
}

testData will be resolved, but State.draft will not. It will be useful to have such ability to extend any graphql type on client side

Workaround

I don't know workaround yet

Solution

I don't know source code in details, but solution (in theory) is to make recursive call, check all @client directives on the tree and call their resolvers, instead of making call only on root level

playerx commented 5 years ago

There was a silly mistake in my example, resolver should look like this:

   resolvers: {
      Query: {
         testData: () => 'Hello X',
      },
      State: {
            draft: ({id}) => {
               console.log('draft resolver')
               return {
                  __typename: 'State',
                  id,
                  draft: "client-side data",
               }
            },
      },