npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@threads/apollo-resolver-types

v3.5.0

Published

Strong typing for apollo resolvers

Downloads

383

Readme

apollo-resolver-types

Strong typing for apollo resolvers

Usage

To setup a graphql server with strong types you will need the following:

  • src/graphql/schema.graphql - The GraphQL schema defining the types & fields visible to the client
  • src/graphql/codegen.yml - a config file for the code generator. This should generate two outputs. The first is the type definitions for TypeScript, the second is the schema prepared for apollo to consume it. This file defines:
    • where to get the ResolverContext type
    • what type is used for any custom scalars (within the server, they may have a different type on the client side).
    • where any enums are defined - you should make sure that the values of your TypeScript enums match the values they have in the GraphQL schema
    • the type "mappings" for the server side type of each object - this is the type that you will fetch when asked for this object in a resolver. If the type of fields on the object match those you expose, you will not need to add a resolver.
  • src/graphql/ResolverContext.ts - It's generally best to define a class for the ResolverContext, so you can extend it with any additional properties you want later. This is where you would add the logger, authenticated user, etc.
  • src/graphql/ResolverTypes.ts - here you combine the generated types with ApolloResolverTypes to mark any necessary resolvers as required.
  • src/graphql/index.ts - The koa apollo server. This is where you combine all your resolvers together. Note that using koa-compress is essential for good performance in GraphQL (without gzip, gql is a very inefficient format).

Since the type definitions for GraphQL can get pretty big, you might find TypeScript runs out of memory. This can usually be solved by replacing your tsc call with NODE_OPTIONS=\"--max-old-space-size=2048\" tsc.

./src/graphql/ResolverTypes.ts should look like:

import ApolloResolverTypes from '@threads/apollo-resolver-types';
import * as GeneratedTypes from './__generated__/types';

type ResolverTypes = ApolloResolverTypes<GeneratedTypes.Resolvers>;
export default ResolverTypes;

You can then refer to ResolverTypes['Query']['contactById'] to get the type of the contactById resolver or ResolverTypes['Contact']['accounts'] to get the accounts resolver on the Contact object.

CLI Usage

To generate the types from your schema, run: apollo-resolver-types src/graphql/codegen.yml. If you've configured it as per the example, this will output two files in the src/graphql/__generated__ directory. For a full list of available config options, see https://graphql-code-generator.com/

apollo-resolver-types codegen-config.yml [options]

Options:
  -w, --watch          Watch for changes and execute generation automatically

Debugging

Sometimes you might be unclear on why a given resolver is required. You might think it should be optional, but apollo-resolver-types says it's required. To resolve this you can try:

import * as GeneratedTypes from './__generated__/types';
import {DebugResolver} from '@threads/apollo-resolver-types';

type X = DebugResolver<GeneratedTypes.Resolvers['Contact'], 'accounts'>;

You can then hover over X and you should see something like:

type X = {
  requiredValue: Account[];
  implicitValue: undefined;
};

The requiredValue is the type defined in the GraphQL schema. The implicitValue is the value in TypeScript. If the implicitValue is assignable to the requiredValue, no resolver should be required. In the above example, undefined cannot be assigned to Account[], so you will need a resolver for Contact.accounts.