Skip to content

Commit

Permalink
run schemaLevelResolveFunction once per tick (#91)
Browse files Browse the repository at this point in the history
* run schemaLevelResolveFunction once per tick

* update Changelog
  • Loading branch information
helfer authored Aug 4, 2016
1 parent 9b09d20 commit 59f2c59
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

### v0.5.3

* Fix a bug with addSchemaLevelResolveFunction. It now runs once per tick (PR #91)


### v0.5.2

* Add addSchemaLevelResolveFunction to exports
Expand Down
8 changes: 6 additions & 2 deletions src/schemaGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ function addSchemaLevelResolveFunction(schema, fn) {
schema.getMutationType(),
schema.getSubscriptionType(),
]).filter(x => !!x);
const rootResolveFn = runAtMostOnce(fn);
// XXX this should run at most once per request to simulate a true root resolver
// for graphql-js this is an approximation that works with queries but not mutations
const rootResolveFn = runAtMostOncePerTick(fn);
rootTypes.forEach((type) => {
const fields = type.getFields();
Object.keys(fields).forEach((fieldName) => {
Expand Down Expand Up @@ -356,13 +358,15 @@ function decorateToCatchUndefined(fn, hint) {
};
}

function runAtMostOnce(fn) {
// XXX this function needs a shim to work in the browser
function runAtMostOncePerTick(fn) {
let count = 0;
let value;
return (...args) => {
if (count === 0) {
value = fn(...args);
count += 1;
process.nextTick(() => { count = 0; });
}
return value;
};
Expand Down
45 changes: 44 additions & 1 deletion test/testSchemaGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ describe('Attaching connectors to schema', () => {
});
});

it('runs only once', () => {
it('runs only once per query', () => {
const simpleResolvers = {
RootQuery: {
usecontext: (r, a, ctx) => ctx.usecontext,
Expand Down Expand Up @@ -731,6 +731,49 @@ describe('Attaching connectors to schema', () => {
});
});

it('runs twice for two queries', () => {
const simpleResolvers = {
RootQuery: {
usecontext: (r, a, ctx) => ctx.usecontext,
useTestConnector: (r, a, ctx) => ctx.connectors.TestConnector.get(),
useContextConnector: (r, a, ctx) => ctx.connectors.ContextConnector.get(),
species: (root, { name }) => root.species + name,
},
};
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: simpleResolvers });
let count = 0;
const rootResolver = () => {
if (count === 0) {
count += 1;
return { stuff: 'stuff', species: 'some ' };
}
if (count === 1) {
count += 1;
return { stuff: 'stuff2', species: 'species2 ' };
}
return { stuff: 'EEE', species: 'EEE' };
};
addSchemaLevelResolveFunction(jsSchema, rootResolver);
const query = `{
species(name: "strix")
stuff
}`;
const expected = {
species: 'some strix',
stuff: 'stuff',
};
const expected2 = {
species: 'species2 strix',
stuff: 'stuff2',
};
return graphql(jsSchema, query).then((res) => {
expect(res.data).to.deep.equal(expected);
return graphql(jsSchema, query).then((res2) =>
expect(res2.data).to.deep.equal(expected2)
);
});
});

it('can attach things to context', () => {
const jsSchema = makeExecutableSchema({ typeDefs: testSchema, resolvers: testResolvers });
const rootResolver = (o, a, ctx) => {
Expand Down

0 comments on commit 59f2c59

Please sign in to comment.