
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
@threads/apollo-resolver-types
Advanced tools
Strong typing for apollo resolvers
To setup a graphql server with strong types you will need the following:
ApolloResolverTypes
to mark any necessary resolvers as required.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 withNODE_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.
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
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
.
FAQs
Strong typing for apollo resolvers
The npm package @threads/apollo-resolver-types receives a total of 1 weekly downloads. As such, @threads/apollo-resolver-types popularity was classified as not popular.
We found that @threads/apollo-resolver-types demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 19 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.