question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong/Missing Typescript type for `__resolveReference`

See original GitHub issue

This bug report should include:

  • A short, but descriptive title. The title doesn’t need “Apollo” in it.
  • The package name and version of Apollo showing the problem.

apollo-graphql, @apollo/federation

  • If applicable, the last version of Apollo where the problem did not occur.
  • The expected behavior.

__resolveReference can be typed with GraphQLFieldResolver (or another new type?) from the graphql library.

  • The actual behavior.

When using Typescript, resolvers passed into buildFederatedSchema can be typed with the GraphQLResolverMap interface defined from apollo-graphql.

e.g.

import { GraphQLResolverMap } from 'apollo-graphql';

/**
 * service graphql resolvers.
 */
const resolvers: GraphQLResolverMap<Context> = {
  Query,
  Mutation,
  Foo,
  DateTime,
};

When looking at the type for GraphQLResolverMap you can see that resolvers can be typed with GraphQLFieldResolver. This type does not work correctly with __resolveReference though and can cause unintentional errors.

When looking at the api reference of __resolveReference, context is the second argument when in GraphQLFieldResolver, context is the third argument.

e.g.

import { GraphQLFieldResolver } from 'graphql';

import * as Models from '../../db/models';
import * as Schema from '../generated/schema-types';
import { Context } from '../helpers/context';

/**
 * Given the id, resolves the Foo model.
 */
// NOTICE: This function is broken.
// GraphQLFieldResolver usage is wrong! Notice `context` should be second argument.
const __resolveReference: GraphQLFieldResolver<{ id: Schema.Foo['id'] }, Context, null> = async (
  reference,
  _args,
  context
): Promise<Models.Foo> => {
  return await context.fooLoader.load(reference.id);
};

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
dncrewscommented, Jan 16, 2020

That’s because __resolveReference isn’t a GraphQLFieldResolver, it’s a GraphQLReferenceResolver (as defined in @apollo/federation), but I don’t know if that’s exported anywhere in a consumable way.

0reactions
DarkLite1commented, Jun 2, 2020

Oh, my bad. I need to define an interface for each type:

interface UserInterface {
  id: string
  name: string
  username: string
  birthDate: string
}
// resolvers
User: {
    __resolveReference(object: UserInterface) {
      return users.find((user) => user.id === object.id)
    },
  },

I expected this to be done automatically. Anyway, sorry for hijacking the threat.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Advanced Types - TypeScript
This page lists some of the more advanced ways in which you can model types, it works in tandem with the Utility Types...
Read more >
Documentation - Type Compatibility - TypeScript
Type compatibility in TypeScript is based on structural subtyping. Structural typing is a way of relating types based solely on their members.
Read more >
Documentation - Utility Types - TypeScript
Utility Types. TypeScript provides several utility types to facilitate common type transformations. These utilities are available globally. Awaited< ...
Read more >
Handbook - Unions and Intersection Types - TypeScript
Intersection and Union types are one of the ways in which you can compose types. Union Types. Occasionally, you'll run into a library...
Read more >
Documentation - Mapped Types - TypeScript
Key Remapping via as. In TypeScript 4.1 and onwards, you can re-map keys in mapped types with an as clause in a mapped...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found