New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

@graphql-tools/executor

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@graphql-tools/executor

Fork of GraphQL.js' execute function

  • 2.0.0-alpha-20240811040531-75e5235c2b3f426a6a5bfc4a7e98478e9e311c02
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.3M
increased by7.15%
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

FAQs

Package last updated on 11 Aug 2024

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