question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

document how to use persisted queries

See original GitHub issue

Story

I want to be able to only send an id instead of a full GraphQL document to the server and then read the actual GraphQL document from a hash table (maybe even asynchronous).


Edit: https://github.com/enisdenjo/graphql-transport-ws/issues/36 showed that this is already possible. It just needs to be documented.

In my Socket.io GraphQL layer I choose to omit passing a context, schema, execute, subscribe etc parameters on a server factory level, but instead choose that a getParameter function must be passed to the server factory. That function is invoked for each GraphQL operation executed with the socket and the sent payload. The user can then decide to map the provided GraphQL Payload to anything else and providing a different context/schema/execute/subscribe based on that.

It has the following type signature

export type GetParameterFunctionParameter = {
  socket: SocketIO.Socket;
  graphQLPayload: {
    source: string;
    variableValues: { [key: string]: any } | null;
    operationName: string | null;
  };
};

export type GetParameterFunction = (
  parameter: GetParameterFunctionParameter
) => PromiseOrPlain<{
  graphQLExecutionParameter: {
    schema: GraphQLSchema;
    contextValue?: unknown;
    rootValue?: unknown;
    // These will overwrite the initials if provided; Useful for persisted queries etc.
    operationName?: string;
    source?: string;
    variableValues?: { [key: string]: any } | null;
  };
  execute?: typeof execute;
  subscribe?: typeof subscribe;
}>;

Which then allows doing stuff like this:

import socketIO from "socket.io";

const persistedOperations = {
    "1": "query { ping }"
    "2": "mutation { ping }"
}

const socketServer = socketIO();

const graphqlServer  = registerSocketIOGraphQLServer({
  socketServer,
  getParameter: ({ socket, graphQLPayload }) => ({
    /* The paramaters used for the operation execution. */
    graphQLExecutionParameter: {
      schema,
      rootValue:,
      contextValue: {
        socket,
      },
      // client source is just the id instead of a full document.
      // we map the id to the actual document.
      source: persistedOperations[graphQLPayload.source]
    },
  }),
});

(This design could also address https://github.com/enisdenjo/graphql-transport-ws/issues/36 as the getParameter function could throw an error or return a rejected Promise etc).

graphql-transport-ws might get some inspiration from this.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
enisdenjocommented, Oct 23, 2020

Good idea! We should offer a handler like that.

However, onOperation is a better suited name for when an operation resolves, and onSubscribe for immediately when the user requests a subscription (operation execution).

The current onSubscribe has to change. Even if its a breaking change, its worthwhile! Check #40 out.

0reactions
enisdenjocommented, May 31, 2021

Quick heads up, @n1ru4l, graphql-ws@v4.7.0 just landed, adding the extensions field in the subscribe message payload.

This means that you now can pass along the persistedQuery through that field on each subscribe as various other libraries/frameworks suggest. And of course, extend with your custom requirements. 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Persisted Queries Link - Apollo GraphQL Docs
The persisted query link requires using the HttpLink . The easiest way to use them together is to concat them into a single...
Read more >
Understanding persisted queries - Documentation - Brightspot
This topic explains how to configure persisted queries on your endpoint and the protocol for using them. Introduction to GraphQL persisted queries
Read more >
Using Persisted Queries · - OneGraph
You can create persisted queries from the "Persisted queries" tab of the dashboard. Persisted queries as mini serverless functions. OneGraph's ...
Read more >
Persisted Queries - Relay
Relay guide to persisted queries.
Read more >
Creating a persisted query - GraphQL API for WordPress
Use the "Excerpt" field, from the Document settings panel, to give a description to the persisted query. Find more information in guide Adding...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found