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

graphql-needle

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-needle

Stitch your GraphQL schemas in a declarative way by using annotations

  • 0.0.10
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

graphql-needle

Stitch your GraphQL schemas in a declarative way by using annotations (aka directives).

See Apollo schema stitching documentation

Example

Instead of generating a stitched schema like so:

import fetch from 'node-fetch';
import { HttpLink } from 'apollo-link-http';
import { introspectSchema, makeRemoteExecutableSchema, mergeSchemas } from 'graphql-tools';

const authorLink = new HttpLink({ uri: 'https://8xz15vx5q.lp.gql.zone/graphql', fetch });
const authorSchema = await introspectSchema(authorLink);
const authorExecutableSchema = makeRemoteExecutableSchema({ schema: authorSchema, link: authorLink });

const chirpLink = new HttpLink({ uri: 'https://vpzl4vxr3.lp.gql.zone/graphql', fetch });
const chirpSchema = await introspectSchema(chirpLink);
const chirpExecutableSchema = makeRemoteExecutableSchema({ schema: chirpSchema, link: chirpLink });

const linkTypeDefs = `
  extend type User {
    chirps: [Chirp]
  }
  extend type Chirp {
    author: User
  }
`;

const executableSchema = mergeSchemas({
  schemas: [authorExecutableSchema, chirpExecutableSchema, linkTypeDefs],
  resolvers: mergeInfo => ({
    User: {
      chirps: {
        fragment: `fragment UserFragment on User { id }`,
        resolve(parent, args, context, info) {
          const authorId = parent.id;
          return mergeInfo.delegate(
            'query',
            'chirpsByAuthorId',
            {
              authorId,
            },
            context,
            info,
          );
        },
      },
    },
    Chirp: {
      author: {
        fragment: `fragment ChirpFragment on Chirp { authorId }`,
        resolve(parent, args, context, info) {
          const id = parent.authorId;
          return mergeInfo.delegate(
            'query',
            'userById',
            {
              id,
            },
            context,
            info,
          );
        },
      },
    },
  }),
});

You can do it like so:

import { makeAnnotatedExecutableSchema } from 'graphql-needle';

const authorSchemaUri = 'https://8xz15vx5q.lp.gql.zone/graphql'; // Apollo Launchpad https://launchpad.graphql.com/vpzl4vxr3
const chirpSchemaUri = 'https://vpzl4vxr3.lp.gql.zone/graphql'; // Apollo Launchpad https://launchpad.graphql.com/8xz15vx5q
const annotatedSchema = `
    extend type User @schema(uri: "${authorSchemaUri}") {
      chirps: [Chirp] @stitch(keyField: "id", queryField: "chirpsByAuthorId", queryParameter: "authorId")
    }
    extend type Chirp @schema(uri: "${chirpSchemaUri}") {
      author: User @stitch(keyField: "authorId", queryField: "userById", queryParameter: "id")
    }
`;


const executableSchema = await makeAnnotatedExecutableSchema(annotatedSchema);

See running example

Keywords

FAQs

Package last updated on 16 Oct 2017

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