Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

(fix) - respect alternative rootkeys in query #87

Merged
merged 1 commit into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/operations/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ const TODO_QUERY = gql`
`;

describe('Query', () => {
let schema, store;
let schema, store, alteredRoot;
const spy: { console?: any } = {};

beforeAll(() => {
schema = require('../test-utils/simple_schema.json');
alteredRoot = require('../test-utils/altered_root_schema.json');
});

afterEach(() => {
Expand Down Expand Up @@ -140,4 +141,35 @@ describe('Query', () => {
todos: [{ __typename: 'Todo', id: '0', text: 'Solve bug' }],
});
});

it('should respect altered root types', () => {
const QUERY = gql`
query getTodos {
todos {
id
text
}
}
`;

const store = new Store(new SchemaPredicates(alteredRoot));

let { data } = query(store, { query: QUERY });
expect(data).toEqual(null);

write(
store,
{ query: QUERY },
{
todos: [{ __typename: 'Todo', id: '0', text: 'Solve bug' }],
__typename: 'query_root',
}
);

({ data } = query(store, { query: QUERY }));
expect(data).toEqual({
__typename: 'query_root',
todos: [{ __typename: 'Todo', id: '0', text: 'Solve bug' }],
});
});
});
2 changes: 1 addition & 1 deletion src/operations/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const read = (

let data = input || Object.create(null);
data =
rootKey !== 'Query'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 👏

rootKey !== ctx.store.getRootKey('query')
? readRoot(ctx, rootKey, rootSelect, data)
: readSelection(ctx, rootKey, rootSelect, data);

Expand Down
Loading