Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@graphql-tools/executor

Package Overview
Dependencies
Maintainers
3
Versions
354
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@graphql-tools/executor

Fork of GraphQL.js' execute function

  • 0.0.3-alpha-20221101031016-9252015c
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.3M
decreased by-6.71%
Maintainers
3
Weekly downloads
 
Created

What is @graphql-tools/executor?

@graphql-tools/executor is a package that provides tools for executing GraphQL operations. It is part of the larger GraphQL Tools ecosystem, which offers a variety of utilities for building and managing GraphQL schemas and operations.

What are @graphql-tools/executor's main functionalities?

Basic Execution

This feature allows you to execute a basic GraphQL query against a schema. The code sample demonstrates creating a simple schema with a single query and executing it.

const { execute } = require('@graphql-tools/executor');
const { makeExecutableSchema } = require('@graphql-tools/schema');

const typeDefs = `
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

const schema = makeExecutableSchema({ typeDefs, resolvers });

const query = `{
  hello
}`;

execute({ schema, document: query }).then(result => {
  console.log(result);
});

Custom Execution Context

This feature allows you to pass a custom context to the execution. The code sample demonstrates how to use a context value in a resolver.

const { execute } = require('@graphql-tools/executor');
const { makeExecutableSchema } = require('@graphql-tools/schema');

const typeDefs = `
  type Query {
    hello(name: String): String
  }
`;

const resolvers = {
  Query: {
    hello: (_, { name }) => `Hello ${name || 'world'}!`,
  },
};

const schema = makeExecutableSchema({ typeDefs, resolvers });

const query = `{
  hello(name: "Alice")
}`;

const context = { user: 'Alice' };

execute({ schema, document: query, contextValue: context }).then(result => {
  console.log(result);
});

Subscription Execution

This feature allows you to execute GraphQL subscriptions. The code sample demonstrates setting up a subscription and publishing an event.

const { subscribe } = require('@graphql-tools/executor');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { PubSub } = require('graphql-subscriptions');

const pubsub = new PubSub();

const typeDefs = `
  type Query {
    _empty: String
  }
  type Subscription {
    messageSent: String
  }
`;

const resolvers = {
  Subscription: {
    messageSent: {
      subscribe: () => pubsub.asyncIterator(['MESSAGE_SENT']),
    },
  },
};

const schema = makeExecutableSchema({ typeDefs, resolvers });

const subscriptionQuery = `
  subscription {
    messageSent
  }
`;

(async () => {
  const iterator = await subscribe({ schema, document: subscriptionQuery });
  for await (const result of iterator) {
    console.log(result);
  }
})();

// Simulate sending a message
setTimeout(() => {
  pubsub.publish('MESSAGE_SENT', { messageSent: 'Hello world!' });
}, 1000);

Other packages similar to @graphql-tools/executor

Keywords

FAQs

Package last updated on 01 Nov 2022

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