Wrong/Missing Typescript type for `__resolveReference`
See original GitHub issueThis 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);
};
- A simple, runnable reproduction! Please make a GitHub repository that anyone can clone and run and see the problem. Other great ways to demonstrate a reproduction are using our CodeSandbox template (https://codesandbox.io/s/apollo-server), or re-mixing our Glitch template (https://glitch.com/~apollo-launchpad). –>
Issue Analytics
- State:
- Created 4 years ago
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That’s because
__resolveReference
isn’t aGraphQLFieldResolver
, it’s aGraphQLReferenceResolver
(as defined in @apollo/federation), but I don’t know if that’s exported anywhere in a consumable way.Oh, my bad. I need to define an
interface
for eachtype
:I expected this to be done automatically. Anyway, sorry for hijacking the threat.