{"id":19282446,"url":"https://github.com/4catalyzer/relay-network-layer","last_synced_at":"2025-04-22T01:31:42.647Z","repository":{"id":40240319,"uuid":"186469447","full_name":"4Catalyzer/relay-network-layer","owner":"4Catalyzer","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-26T05:01:33.000Z","size":1169,"stargazers_count":11,"open_issues_count":11,"forks_count":1,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-30T03:52:13.820Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/4Catalyzer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-13T17:52:09.000Z","updated_at":"2022-06-30T08:21:49.000Z","dependencies_parsed_at":"2023-07-16T10:45:58.130Z","dependency_job_id":"a9b67552-b554-418e-ac34-1fc9a91da24b","html_url":"https://github.com/4Catalyzer/relay-network-layer","commit_stats":{"total_commits":116,"total_committers":6,"mean_commits":"19.333333333333332","dds":0.6982758620689655,"last_synced_commit":"2f4251b19ca078059e76d19978b08e6ae0568091"},"previous_names":["4catalyzer/relay-auth"],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4Catalyzer%2Frelay-network-layer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4Catalyzer%2Frelay-network-layer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4Catalyzer%2Frelay-network-layer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4Catalyzer%2Frelay-network-layer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/4Catalyzer","download_url":"https://codeload.github.com/4Catalyzer/relay-network-layer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223886197,"owners_count":17219692,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T21:26:56.363Z","updated_at":"2024-11-09T21:26:56.994Z","avatar_url":"https://github.com/4Catalyzer.png","language":"TypeScript","readme":"# `relay-network-layer`\n\nA simple Relay.js network layer with support for authorization, request batching and\nsubscriptions.\n\n## Usage\n\n```sh\nyarn add relay-network-layer\n```\n\n### Quickstart\n\nAnd when creating your Relay Environment import and use the `createFetch` method\nto create a new fetch function:\n\n```js\nimport {\n  Environment,\n  RecordSource,\n  Store,\n} from 'relay-runtime';\n\nimport NetworkLayer from 'relay-network-layer';\n\nconst network = NetworkLayer.create({\n  url: '/graphql',\n\n  token: accessToken,\n}),\n\nconst store = new Store(new RecordSource());\n\nconst environment = return new Environment({\n  store,\n  network,\n});\n```\n\nIf your server supports subscriptions you can also provide the socket endpoint to listen on to configure subscriptions. See [subscriptions](#subscriptions) for more configuration options\n\n```js\nimport NetworkLayer from 'relay-network-layer';\n\nconst network = NetworkLayer.create({\n  url: '/graphql',\n  subscriptionUrl: '/subscriptions/graphql',\n  token: accessToken,\n}),\n```\n\n### Advanced setup\n\nFor more nuanced configuration, or if you only want to one of the fetch or subscribe configurations factory methods are also provided to generate functions that can be\nbased directly to the base Relay Network:\n\n```js\nimport { Network } from 'relay-runtime';\n\nimport { createFetch, createSubscribe } from 'relay-network-layer';\n\nconst network = Network.create(\n  createFetch({\n    url: '/graphql',\n    token: accessToken,\n  }),\n  createSubscribe({\n    url: '/subscriptions/graphql',\n    maxSubscriptions: 150,\n  }),\n);\n```\n\nThis way you can swap out your own subscription implementation or custom fetching logic.\n\n### Subscriptions\n\nSubscriptions are managed using `Socket.IO`'s websocket client, and implement\nthe following message flow:\n\nON `createSubscribe()`\n\n- connect\n- authenticate (if token is provided)\n- any queued subscriptions are sent\n\nON a Relay `requestSubscription` call\n\n- a `subscribe` message is sent to the server\n- slient waits for a `subscription update` message.\n\n#### Message Types\n\n**`authenticate`**\n\nCalled when a token is provided, the server is expected to use the provided token\nto authenticate subscriptions.\n\n**`subscribe`**\n\nCalled with the subscription details\n\n- `id: string`: operation id\n- `query: string`: GraphQL operation as string or parsed GraphQL document node\n- `variables?: Object`: Object with GraphQL variables\n\n**`subscription update`**\n\nSent from the server with a GQL payload for a subscription\n\n- `id: string`: operation id\n- `data: any`: Execution result\n- `errors?: Error[]`: Array of resolvers errors\n\n**`unsubscribe`**\n\nCalled when a subscription is torn down on the client\n\n- `id: string`: operation id\n\n## API\n\n### `NetworkLayer: { create(opts: NetworkOptions) =\u003e Network }`\n\nCreates a Network that can be based directly to Relay's Environment constructor.\n\n```js\nimport NetworkLayer from 'relay-network-layer';\n\nconst network = NetworkLayer.create(),\n```\n\nAnd to tear down subscription sockets call:\n\n```js\nnetwork.close();\n```\n\nCreate accepts a set of options of the type:\n\n```ts\ninterface NetworkLayerOptions {\n  /** The graphql API endpoint, provide a pathname or fully qualified url. */\n  url?: string;\n\n  /**\n   * The socket.io endpoint, provide a pathname or fully qualified url.\n   *\n   * **MUST** be provided in order to enable subscription support\n   */\n  subscriptionUrl?: string;\n\n  /**\n   * Controls the error behavior, when set, responses with an `errors` array will be turned into Errors\n   * and thrown.\n   *\n   * Defaults: `true`\n   *\n   * ref: https://github.com/facebook/relay/issues/1816#issuecomment-304492071\n   */\n  throwErrors?: boolean;\n\n  /**\n   * Batches requests within a time frame into a single request for more\n   * efficient fetching. requests are sent as a JSON array. Mutations and file uploads\n   * are NOT batched.\n   *\n   * Defaults to `true`.\n   *\n   * **Requires a Graphql server that understands batching\"\n   */\n  batch?:\n    | boolean\n    | {\n        enabled: boolean;\n\n        /**\n         * The amount of time to wait before a batch is closed and sent to the server.\n         *\n         * The default is `0ms`, or about the next tick of the event loop.\n         */\n        timeoutMs?: number;\n      };\n\n  /** The authorization configuration or token for a convenient shorthand */\n  authorization?:\n    | string\n    | {\n        token: string;\n        /** The header the `token` is sent in defaults to \"Authorization\" */\n        headerName?: string;\n\n        /** The prefix string in the auth header defaults to \"Bearer\" */\n        scheme?: string;\n      };\n\n  /**\n   * Any fetch API \"init\" details, this is based directly to the `fetch` call, see\n   * [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request) for API details.\n   */\n  init?: RequestInit | (() =\u003e RequestInit);\n\n  /**\n   * The max number of concurrent subscriptions allowed.\n   * After the number has been no more will be set to the server (a console.warn is issued informing you the limit has been reached)\n   *\n   * The default is: `200`\n   */\n  maxSubscriptions?: number;\n}\n```\n\n## `createFetch(FetchOptions)`\n\ncreate's a fetching function that can be provided Relay's `Network.create()`. Takes\nas set of options of the following type (description the same as for NetworkOptions)\n\n```ts\nexport interface FetchOptions {\n  url?: string;\n  init?: RequestInit;\n  token?: string;\n  authPrefix?: string;\n  authHeader?: string;\n  batch?: boolean;\n  batchTimeout?: number;\n}\n```\n\n## `createSubscribe(SubscribeOptions)`\n\nCreate's a subscribe function that can be provided Relay's `Network.create()`. Takes\nas set of options of the following type (description the same as for NetworkOptions)\n\n```ts\nexport interface SubscriptionOptions {\n  url?: string;\n  token?: string;\n  maxSubscriptions?: number;\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4catalyzer%2Frelay-network-layer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F4catalyzer%2Frelay-network-layer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4catalyzer%2Frelay-network-layer/lists"}