federation icon indicating copy to clipboard operation
federation copied to clipboard

[docs] Add __typename example to advanced entity docs

Open rhino88 opened this issue 2 years ago • 6 comments
trafficstars

In some cases it may be useful to define an entity as a singleton. The @key directive supports the __typename meta field to declare an entity as a singleton.

In the following example, the Meta entity can be uniquely identified by its __typename (because there is only ever one instance):

type Meta @key(fields: "__typename") {
    version: String!
}

I was able to confirm this works as expected in a repro: https://codesandbox.io/p/sandbox/tender-engelbart-wympgk?file=%2Ftsconfig.json%3A1%2C1

rhino88 avatar Oct 25 '23 17:10 rhino88

@rhino88: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

apollo-cla avatar Oct 25 '23 17:10 apollo-cla

Deploy request for apollo-federation-docs pending review.

Visit the deploys page to approve it

Name Link
Latest commit cd503cc133697180da21a36b9927a0f2c2ebc2df

netlify[bot] avatar Oct 25 '23 17:10 netlify[bot]

⚠️ No Changeset found

Latest commit: cd503cc133697180da21a36b9927a0f2c2ebc2df

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Oct 25 '23 17:10 changeset-bot[bot]

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

codesandbox-ci[bot] avatar Oct 25 '23 17:10 codesandbox-ci[bot]

Thanks for this @rhino88 ! The team is discussing whether this is a pattern we want to promote, we'll keep you updated.

Meschreiber avatar Oct 31 '23 23:10 Meschreiber

Thanks @Meschreiber.

This is something we've found useful for a number of scenarios in production (e.g HomePage entities). The only current alternative we found would be to introduce another dummy field to use as a @key, which isn't really desirable.

rhino88 avatar Nov 01 '23 13:11 rhino88