From ed0c144af9d0a80c2999d970c5de41453e9c955c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Vl=C4=8Dek?= Date: Tue, 15 Aug 2017 07:50:56 +0200 Subject: [PATCH] Simplification of Relay Modern Example (#1757) (#2776) --- examples/with-relay-modern/.graphqlconfig | 8 ++++++ examples/with-relay-modern/README.md | 9 ++---- .../lib/createRelayEnvironment.js | 18 ++++++------ examples/with-relay-modern/package.json | 7 +++-- .../with-relay-modern/scripts/getSchema.js | 28 ------------------- 5 files changed, 23 insertions(+), 47 deletions(-) create mode 100644 examples/with-relay-modern/.graphqlconfig delete mode 100644 examples/with-relay-modern/scripts/getSchema.js diff --git a/examples/with-relay-modern/.graphqlconfig b/examples/with-relay-modern/.graphqlconfig new file mode 100644 index 00000000..fadb4e08 --- /dev/null +++ b/examples/with-relay-modern/.graphqlconfig @@ -0,0 +1,8 @@ +{ + "schemaPath": "schema/schema.graphql", + "extensions": { + "endpoints": { + "dev": "https://api.graph.cool/relay/v1/next-js-with-relay-modern-example" + } + } +} \ No newline at end of file diff --git a/examples/with-relay-modern/README.md b/examples/with-relay-modern/README.md index cf3a36bf..9759952f 100644 --- a/examples/with-relay-modern/README.md +++ b/examples/with-relay-modern/README.md @@ -16,15 +16,10 @@ Install it: npm install ``` -Setup Graphcool project +Download schema introspection data from configured Relay endpoint ```bash -npm run graphcool-init -``` - -After successful initialization, copy Graphcool Relay Endpoint URL from console and store it into `.env` file -```bash -RELAY_ENDPOINT=your relay endpoint here +npm run schema ``` Run Relay ahead-of-time compilation (should be re-run after any edits to components that query data with Relay) diff --git a/examples/with-relay-modern/lib/createRelayEnvironment.js b/examples/with-relay-modern/lib/createRelayEnvironment.js index e10d952a..ce93bd2d 100644 --- a/examples/with-relay-modern/lib/createRelayEnvironment.js +++ b/examples/with-relay-modern/lib/createRelayEnvironment.js @@ -1,15 +1,15 @@ import { Environment, Network, RecordSource, Store } from 'relay-runtime' -import fetch from 'isomorphic-unfetch' +import fetch from 'isomorphic-fetch' let relayEnvironment = null // Define a function that fetches the results of an operation (query/mutation/etc) // and returns its results as a Promise: function fetchQuery ( - operation, - variables, - cacheConfig, - uploadables, + operation, + variables, + cacheConfig, + uploadables, ) { return fetch(process.env.RELAY_ENDPOINT, { method: 'POST', @@ -25,12 +25,12 @@ function fetchQuery ( } export default function initEnvironment ({ records = {} } = {}) { - // Create a network layer from the fetch function + // Create a network layer from the fetch function const network = Network.create(fetchQuery) const store = new Store(new RecordSource(records)) - // Make sure to create a new Relay environment for every server-side request so that data - // isn't shared between connections (which would be bad) + // Make sure to create a new Relay environment for every server-side request so that data + // isn't shared between connections (which would be bad) if (!process.browser) { return new Environment({ network, @@ -38,7 +38,7 @@ export default function initEnvironment ({ records = {} } = {}) { }) } - // reuse Relay environment on client-side + // reuse Relay environment on client-side if (!relayEnvironment) { relayEnvironment = new Environment({ network, diff --git a/examples/with-relay-modern/package.json b/examples/with-relay-modern/package.json index b7abfd7d..06ff36b9 100644 --- a/examples/with-relay-modern/package.json +++ b/examples/with-relay-modern/package.json @@ -8,22 +8,23 @@ "build": "next build", "start": "next start", "relay": "relay-compiler --src ./ --exclude **/.next/** **/node_modules/** **/test/** **/__generated__/** --schema ./schema/schema.graphql", - "schema": "node scripts/getSchema.js" + "schema": "graphql get-schema dev" }, "author": "", "license": "ISC", "dependencies": { "dotenv": "^4.0.0", "dotenv-webpack": "^1.5.4", - "isomorphic-unfetch": "^2.0.0", + "isomorphic-fetch": "^2.2.1", "next": "^3.0.3", "react": "^15.6.1", "react-dom": "^15.6.1", "react-relay": "^1.2.0-rc.1" }, "devDependencies": { + "graphql-cli": "^1.0.0-beta.4", "babel-plugin-relay": "^1.1.0", "graphcool": "^1.2.1", "relay-compiler": "^1.2.0-rc.1" } -} \ No newline at end of file +} diff --git a/examples/with-relay-modern/scripts/getSchema.js b/examples/with-relay-modern/scripts/getSchema.js deleted file mode 100644 index d2940073..00000000 --- a/examples/with-relay-modern/scripts/getSchema.js +++ /dev/null @@ -1,28 +0,0 @@ -var fetch = require('node-fetch') -var fs = require('fs') -var path = require('path') - -require('dotenv').config() - -const { - buildClientSchema, - introspectionQuery, - printSchema -} = require('graphql/utilities') - -console.log(introspectionQuery) - -fetch(process.env.RELAY_ENDPOINT, { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ 'query': introspectionQuery }) -}) - .then(res => res.json()) - .then(res => { - console.log(res) - const schemaString = printSchema(buildClientSchema(res.data)) - fs.writeFileSync(path.join(__dirname, '..', 'schema', 'schema.graphql'), schemaString) - })