Skip to content

Commit

Permalink
fix: only optimistically resolve nested entities
Browse files Browse the repository at this point in the history
We have this optimization that nested entities will resolve id if it's
only requested field without making actual database query.

This might not be ideal, if relation is broken (for example Space references
not existing SpaceMetadata), but can save some queries, so it's mostly
just a convention.

However this ended up also short-circuting requests like this:
```gql
query {
  space(id: "eth:doesnotexist") {
    id
  }
}
```

This commit prevents this from happening, by checking if it was resolved
from parent.
  • Loading branch information
Sekhmet committed Jun 12, 2024
1 parent 572a6b8 commit 8d4f74c
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,29 +194,28 @@ export async function querySingle(
const returnType = getNonNullType(info.returnType) as GraphQLObjectType;
const jsonFields = getJsonFields(returnType);

const currentValue = parent?.[info.fieldName];
const parentResolvedValue = parent?.[info.fieldName];

if (currentValue === null) return null;
const alreadyResolvedInParent = typeof currentValue === 'object';
if (parentResolvedValue === null) return null;
const alreadyResolvedInParent = typeof parentResolvedValue === 'object';
if (alreadyResolvedInParent) {
return {
...formatItem(currentValue, jsonFields),
...formatItem(parentResolvedValue, jsonFields),
_args: { block }
};
}

const id = currentValue || args.id;

const parsed = parseResolveInfo(info);
if (parsed) {
if (parsed && parentResolvedValue) {
// @ts-ignore
const simplified = simplifyParsedResolveInfoFragmentWithType(parsed, returnType);

if (Object.keys(simplified.fields).length === 1 && simplified.fields['id']) {
return { id, _args: { block } };
return { id: parentResolvedValue, _args: { block } };
}
}

const id = parentResolvedValue || args.id;
const items = await context.getLoader(returnType.name.toLowerCase(), 'id', block).load(id);
if (items.length === 0) {
throw new Error(`Row not found: ${id}`);
Expand Down

0 comments on commit 8d4f74c

Please sign in to comment.