apollographql / apollographql/federation

Default resolveReference with federated schema

Open
#398 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
725
Forks
276
Avg merge
1h 47m
Merged PRs (30d)
1

Description

Similar to the way a default `fieldResolver` function can be defined when creating a `new Apollo Server` it would be useful to be able to define a default `__resolveReference` function when using a federated schema.

A use case would be when wanting to resolve multiple references using the same function:

```js
const resolvers = {
Post: {
__resolveReference(reference, context) {
return context.models[reference.__typename].fetchItem(reference.id);
}
},
Comment: {
__resolveReference(reference, context) {
return context.models[reference.__typename].fetchItem(reference.id);
}
},
Review: {
__resolveReference(reference, context) {
return context.models[reference.__typename].fetchItem(reference.id);
}
}
};
```

Of course, one could simply create and use shared function…

```js
function __resolveReference(reference, context) {
return context.models[reference.__typename].fetchItem(reference.id);
}

const resolvers = {
Post: {
__resolveReference
},
Comment: {
__resolveReference
},
Review: {
__resolveReference
}
};
```

…but still, this is quite verbose compared to being able to define a default.

The current federated schema builder uses a default function if no `resolveReference` is defined for an entity:
https://github.com/apollographql/apollo-server/blob/a28d0ac08b47cd79926f7a82a9599a0b9d0b0f10/packages/apollo-federation/src/types.ts#L91-L95

The aim would be to allow for customising this default, maybe by passing one to `buildFederatedSchema`? For example…

```js
// My own default resolveReference function
function resolveReference(reference, context, info) {
return context.models[reference.__typename].fetchItem(reference.id);
}

const schema = buildFederatedSchema({ modules, resolveReference });
```

Contributor guide

Open the contributing guide

Research direction

Start by reading packages/apollo-federation/src/types.ts around lines 91-95 and trace how buildFederatedSchema supplies the current default resolveReference behavior. The change is complete when buildFederatedSchema can accept a configurable default while an entity-specific __resolveReference still takes precedence.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.