Skip to content

Commit

Permalink
wip: add change from 226 to support nestd field resolvers
Browse files Browse the repository at this point in the history
Copied from apollographql#226
  • Loading branch information
JeremyJonas committed May 9, 2018
1 parent 404b6a1 commit 41e5fc4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
80 changes: 80 additions & 0 deletions packages/apollo-link-state/src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,86 @@ it('runs resolvers for client queries', done => {
}, done.fail);
});

it('runs resolvers for nested client queries', done => {
const nestedQuery = gql`
query NestedQuery {
foo @client {
bar
nestedBar {
baz
}
}
}
`;
const getNestedBarById = id =>
id === '42'
? {
baz: true,
}
: null;

const client = withClientState({
resolvers: {
Foo: {
nestedBar: ({ nestedBar }) => getNestedBarById(nestedBar),
},
Query: {
foo: () => ({ bar: true, nestedBar: '42', __typename: 'Foo' }),
},
},
});
execute(client, { query: nestedQuery }).subscribe(({ data }) => {
expect(data).toEqual({ foo: { bar: true, nestedBar: { baz: true } } });
done();
}, done.fail);
});

it('runs resolvers for missing nested client queries with server data', done => {
const nestedQuery = gql`
query MixedNestedQuery {
foo @client {
bar
nestedBar {
baz
}
}
bar {
baz
}
}
`;
const getNestedBarById = id =>
id === '42'
? {
baz: true,
}
: null;

const sample = new ApolloLink(() =>
Observable.of({ data: { bar: { baz: false } } }),
);
const client = withClientState({
resolvers: {
Foo: {
nestedBar: ({ nestedBar }) => getNestedBarById(nestedBar),
},
Query: {
foo: () => ({ bar: true, nestedBar: '42', __typename: 'Foo' }),
},
},
});
execute(client.concat(sample), { query: nestedQuery }).subscribe(
({ data }) => {
expect(data).toEqual({
foo: { bar: true, nestedBar: { baz: true } },
bar: { baz: true },
});
done();
},
done.fail,
);
});

it('runs resolvers for missing client queries with server data', done => {
const query = gql`
query Mixed {
Expand Down
15 changes: 13 additions & 2 deletions packages/apollo-link-state/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,26 @@ export const withClientState = (
//https://github.com/apollographql/apollo-client/tree/master/packages/graphql-anywhere#resolver-info
const fieldValue = rootValue[info.resultKey];

//If fieldValue is defined, server returned a value
if (fieldValue !== undefined) return fieldValue;
const useServerReturnedValue =
fieldValue !== undefined &&
((rootValue as any).__typename || type) == 'Query';

//If fieldValue is defined and we are at the Query level, server returned a value so we return it
if (useServerReturnedValue) {
return fieldValue;
}

// Look for the field in the custom resolver map
const resolverMap = resolvers[(rootValue as any).__typename || type];
if (resolverMap) {
const resolve = resolverMap[fieldName];
if (resolve) return resolve(rootValue, args, context, info);
if (fieldValue !== undefined) return fieldValue;
}
if (fieldValue !== undefined) {
return fieldValue;
}

//TODO: the proper thing to do here is throw an error saying to
//add `client.onResetStore(link.writeDefaults);`
//waiting on https://github.com/apollographql/apollo-client/pull/3010
Expand Down

0 comments on commit 41e5fc4

Please sign in to comment.