Socket
Socket
Sign inDemoInstall

graphql-compose

Package Overview
Dependencies
Maintainers
1
Versions
269
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-compose

GraphQL schema builder from different data sources with middleware extensions.


Version published
Weekly downloads
299K
decreased by-16.95%
Maintainers
1
Weekly downloads
 
Created

What is graphql-compose?

graphql-compose is a toolkit for generating and working with GraphQL schemas in a more flexible and composable way. It provides utilities to create GraphQL types, resolvers, and schemas with ease, allowing for a more modular and maintainable approach to building GraphQL APIs.

What are graphql-compose's main functionalities?

Schema Creation

This feature allows you to create a GraphQL schema by adding fields to the Query type. The example demonstrates adding a simple 'hello' field that returns a string.

const { schemaComposer } = require('graphql-compose');

schemaComposer.Query.addFields({
  hello: {
    type: 'String',
    resolve: () => 'Hello, world!',
  },
});

const schema = schemaComposer.buildSchema();

Type Composers

Type Composers allow you to define GraphQL object types and their fields in a modular way. The example shows how to create a 'User' type and add a custom resolver to it.

const { schemaComposer } = require('graphql-compose');

const UserTC = schemaComposer.createObjectTC({
  name: 'User',
  fields: {
    id: 'ID!',
    name: 'String',
  },
});

UserTC.addResolver({
  name: 'findById',
  type: 'User',
  args: { id: 'ID!' },
  resolve: ({ source, args, context, info }) => {
    // resolver logic here
  },
});

Resolver Composition

Resolver Composition allows you to add multiple resolvers to a type, making it easier to manage complex query logic. The example shows how to add 'findById' and 'findByName' resolvers to the 'User' type.

const { schemaComposer } = require('graphql-compose');

const UserTC = schemaComposer.getOrCreateOTC('User', {
  fields: {
    id: 'ID!',
    name: 'String',
  },
});

UserTC.addResolver({
  name: 'findById',
  type: UserTC,
  args: { id: 'ID!' },
  resolve: ({ source, args, context, info }) => {
    // resolver logic here
  },
});

UserTC.addResolver({
  name: 'findByName',
  type: UserTC,
  args: { name: 'String' },
  resolve: ({ source, args, context, info }) => {
    // resolver logic here
  },
});

Other packages similar to graphql-compose

Keywords

FAQs

Package last updated on 19 Dec 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc