Skip to content

Instantly share code, notes, and snippets.

@voodooattack
Last active October 2, 2017 01:11
Show Gist options
  • Select an option

  • Save voodooattack/c4f7a261ea189ffb1894e9cb5e018587 to your computer and use it in GitHub Desktop.

Select an option

Save voodooattack/c4f7a261ea189ffb1894e9cb5e018587 to your computer and use it in GitHub Desktop.

Revisions

  1. voodooattack revised this gist Oct 2, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion internal-graphql.js
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,9 @@ Meteor.startup(() => {
    // My own extension to Vulcan.js.
    // It's really useful when you have access to the ApolloClient inside resolvers.
    // This makes it possible for resolvers to make their own queries.
    getRenderContext() { return renderContext; }
    getRenderContext() { return renderContext; },
    // Copy over the userID.
    userId: req.loginContext ? req.loginContext.userId : undefined
    }, GraphQLSchema.context);
    // go over context and add Dataloader to each collection
    Collections.forEach(collection => {
  2. voodooattack revised this gist Sep 25, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion internal-graphql.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ Meteor.startup(() => {
    typeDefs: GraphQLSchema.finalSchema,
    resolvers: GraphQLSchema.resolvers,
    });
    withRenderContextEnvironment(async function (renderContext, req, res, next) {
    withRenderContextEnvironment(function (renderContext, req, res, next) {
    const context = deepmerge({
    // My own extension to Vulcan.js.
    // It's really useful when you have access to the ApolloClient inside resolvers.
  3. voodooattack revised this gist Sep 24, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -3,3 +3,5 @@
    To make a GraphQL query on a Vulcan.js server, your server has to connect to itself via a new HTTP connection every time it receives a request.

    This package makes it so that Vulcan’s GraphQL queries never leave the process. All GraphQL requests are processed with no overhead.

    > See also: [webtoken-session](https://gist.github.com/voodooattack/7a02881b0c762630160424f742b6f780)
  4. voodooattack revised this gist Sep 23, 2017. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  5. voodooattack created this gist Sep 23, 2017.
    5 changes: 5 additions & 0 deletions internal-graphql_README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    # Internal GraphQL for Vulcan.js

    To make a GraphQL query on a Vulcan.js server, your server has to connect to itself via a new HTTP connection every time it receives a request.

    This package makes it so that Vulcan’s GraphQL queries never leave the process. All GraphQL requests are processed with no overhead.
    39 changes: 39 additions & 0 deletions internal-graphql_internal-graphql.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    import { withRenderContextEnvironment, GraphQLSchema, Collections } from 'meteor/vulcan:lib';
    import { makeExecutableSchema } from 'graphql-tools';
    import { graphql, print } from 'graphql';
    import DataLoader from 'dataloader';
    import deepmerge from 'deepmerge';

    async function findByIds(collection, ids, context) {
    // get documents
    const documents = await collection.find({ _id: { $in: ids } }).fetch();
    // order documents in the same order as the ids passed as argument
    return ids.map(id => _.findWhere(documents, {_id: id}));
    }

    Meteor.startup(() => {
    const schema = makeExecutableSchema({
    typeDefs: GraphQLSchema.finalSchema,
    resolvers: GraphQLSchema.resolvers,
    });
    withRenderContextEnvironment(async function (renderContext, req, res, next) {
    const context = deepmerge({
    // My own extension to Vulcan.js.
    // It's really useful when you have access to the ApolloClient inside resolvers.
    // This makes it possible for resolvers to make their own queries.
    getRenderContext() { return renderContext; }
    }, GraphQLSchema.context);
    // go over context and add Dataloader to each collection
    Collections.forEach(collection => {
    context[collection.options.collectionName].loader = new DataLoader(ids => findByIds(collection, ids, context), {
    cache: true
    });
    });
    renderContext.apolloClient.networkInterface = {
    query: (request) => {
    return graphql(schema, print(request.query), {}, context, request.variables, request.debugName);
    }
    };
    next();
    }, { order: 23, name: 'internal-graphql-middleware' });
    });
    20 changes: 20 additions & 0 deletions internal-graphql_package.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    Package.describe({
    name: 'internal-graphql',
    version: '0.0.1',
    // Brief, one-line summary of the package.
    summary: 'Prevent extraneous HTTP connections created by Vulcan to itself.',
    // URL to the Git repository containing the source code for this package.
    git: '',
    // By default, Meteor will default to using README.md for documentation.
    // To avoid submitting documentation, set this field to null.
    documentation: 'README.md'
    });

    Package.onUse(function(api) {
    api.versionsFrom('1.5.1');
    api.use([
    'ecmascript',
    'vulcan:lib'
    ]);
    api.mainModule('internal-graphql.js', ['server']);
    });